This site requires JavaScript, please enable it in your browser!
Greenfoot back
roonie01
roonie01 wrote ...

2018/1/26

help with exam plz due monday

1
2
roonie01 roonie01

2018/1/26

#
for some reason it dose not change direction when my actor reaches bottom of screen
public class GiantRobot extends Actor
{
    // GLOBAL DECLARATIONS
    // -------------------
    int time=0;
    int Xaxis=3;
    int Yaxis=3;
    // CONSTRUCTOR   // runs one time only
    // -----------
    public GiantRobot()    
    {
    
    } // end constructor
    
    
    
    // ACT METHOD   // runs repeatedly
    // ----------
    public void act() 
    {
        
            setLocation(getX()+Xaxis,getY()+Yaxis);
            if(getX()==599&&getY()==799)
            {
                setLocation(0,0);
            }
            if(getX()==599)
             {   
                Xaxis=Xaxis*(-1);
            }
            else if(getX()==0)
            {
               Xaxis=Xaxis*(-1);  
            }
         if(getY()==799)
            {
                Yaxis=Yaxis*(-1);
            }
            else if(getY()==0)
            {
               Yaxis=Yaxis*(-1); 
            }
            
    } // end act
 }//end class
roonie01 roonie01

2018/1/26

#
and I am also having trouble with my code for changing one of my other actors image to the second image for the right or left
public class Player1 extends Actor
{
    // GLOBAL DECLARATIONS
    // -------------------
    int walkRate = 0;
    GreenfootImage Right=new GreenfootImage("mega1.png");
    GreenfootImage Left=new GreenfootImage("mega1b.png");
    GreenfootImage RightA=new GreenfootImage("mega2b.png");
    GreenfootImage LeftA=new GreenfootImage("mega2.png");

    int move=3;
    // CONSTRUCTOR   // runs one time only
    // -----------
    public Player1()    
    {
        setImage(Right);

    } // end constructor

    // ACT METHOD   // runs repeatedly
    // ----------
    public void act() 
    {
        setLocation(getX(),getY());
        checkForDirectionalControll();
        walkRate++;
    } // end act

    public void checkForDirectionalControll()
    {
        walkRate++;
        if (Greenfoot.isKeyDown("up"))
        { 
            //says to check for key up and if condition is true move up 3 pixel's 
            setLocation(getX(),getY()-3);
        }//end if
        if (Greenfoot.isKeyDown("down"))
        {
            //says to check for key down and if condition is true move down 3 pixel's
            setLocation(getX(),getY()+3);
        }//end if
        if(Greenfoot.isKeyDown("left")&&getImage()==Right||getImage()==RightA&&walkRate==10)
        {
            //says to set the image to the left image when the left arrow is pressed
            setImage(Left);

            LeftImages();

            walkRate=0;
        }
        if  (Greenfoot.isKeyDown("left"))
        { 
            //says to check for key left and if condition is true move left 3 pixel's 

            setLocation(getX()-3,getY());
        }//endif
        if(Greenfoot.isKeyDown("right")&&getImage()==Left||getImage()==LeftA&&walkRate ==10)
        {
            //says to set the image to right image when the right arrow is pressed
            //and the walkRate variable = 20
            setImage(Right);

            RightImages();

            walkRate=0;
        }
        if (Greenfoot.isKeyDown("right"))
        {

            //says to check for key right and if condition is true move right 3 pixel's

            setLocation(getX()+3,getY());
        }//end if
        //end checkForDirectionalControll
    } 

    public void RightImages()
    {
       
        if(getImage()==Right&&walkRate==10)
        {
            setImage(RightA);
            walkRate=0;
        }
        else if(walkRate<10)
        {
            setImage(Right);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setImage(Left);  
        }
        
    }

    public void LeftImages()
    {

        if(getImage()==Left&&walkRate==10)
        {
            setImage(LeftA);
            walkRate=0;
        }

        else if(walkRate<10)
        {
            setImage(Left);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setImage(Right);  
        }

    }
} // end class
danpost danpost

2018/1/26

#
What is the size of your world?
Vercility Vercility

2018/1/26

#
if(getX()==599)
             {   
                Xaxis=Xaxis*(-1);
            }
You move by multiplicatives of 3, im assuming x is never 599 depending on where you create the actor. Use >= 599 in this case
danpost danpost

2018/1/26

#
If the world constructor super call contains '(600, 800, 1)' or '(600, 800, 1, true)' (order and magnitude of values is important), then try just removing lines 23 through 26 from the GiantRobot class.
roonie01 roonie01

2018/1/26

#
my world is (800,600,1)
roonie01 roonie01

2018/1/26

#
and can I get help with the images from my Player1 class
danpost danpost

2018/1/27

#
roonie01 wrote...
my world is (800,600,1)
Then all your upper limit checks are wrong.
roonie01 roonie01

2018/1/28

#
can I get help with changing images for my player1 class it is supposed to change the image after 10 act cycles to the second image like Right image changes to RightA image
Vercility Vercility

2018/1/28

#
private int cycles = 1;

public void act() {
    if(cycles = 10) {
//switch image}

    else{
cycles++
  }
//
}
danpost danpost

2018/1/28

#
A few things to start with: (1) remove line 26 (line 31 does that). (2) remove line 89 through 92 and lines 109 thrrough 112 (this is attempted to be done in the 'checkForDirectionalControll' method). (3) place the two 'or'ed conditions on lines 42 and 57 in parenthesis (order of operations is importantt)
if (conditionA && (conditionB || conditionC))
Next, your animation cycle takes a total of 20 act cycles (10 for each of the two images in either direction). Therefore, the counter should run to 20. The image should change at values of 10 and 20 (which is when the value is reset to 0). There is a one-to-one correspondence between the changing values and the image that should be used so it should not be too difficult to realize.
roonie01 roonie01

2018/1/28

#
so like this
 public void checkForDirectionalControll()
    {
        walkRate++;
        if (Greenfoot.isKeyDown("up"))
        { 
            //says to check for key up and if condition is true move up 3 pixel's 
            setLocation(getX(),getY()-3);
        }//end if
        if (Greenfoot.isKeyDown("down"))
        {
            //says to check for key down and if condition is true move down 3 pixel's
            setLocation(getX(),getY()+3);
        }//end if
        if(Greenfoot.isKeyDown("left")&&(getImage()==Right||getImage()==RightA))
        {
            //says to set the image to the left image when the left arrow is pressed
            setImage(Left);
            
            
            LeftImages();
           
            walkRate=0;
        }
        if  (Greenfoot.isKeyDown("left"))
        { 
            //says to check for key left and if condition is true move left 3 pixel's 

            setLocation(getX()-3,getY());
        }//endif
        if(Greenfoot.isKeyDown("right")&&(getImage()==Left||getImage()==LeftA)&&walkRate ==10)
        {
            //says to set the image to right image when the right arrow is pressed
            //and the walkRate variable = 20
            setImage(Right);

            RightImages();

            walkRate=0;
        }
        if (Greenfoot.isKeyDown("right"))
        {

            //says to check for key right and if condition is true move right 3 pixel's

            setLocation(getX()+3,getY());
        }//end if
        //end checkForDirectionalControll
    } 

    public void RightImages()
    {
       
        if(getImage()==Right&&walkRate==10)
        {
            setImage(RightA);
            walkRate=0;
        }
        else if(walkRate<10)
        {
            setImage(Right);
        }
       
        
    }

    public void LeftImages()
    {

        if(getImage()==Left&&walkRate==10)
        {
            setImage(LeftA);
            walkRate=0;
        }

        else if(walkRate<10)
        {
            setImage(Left);
        }
        

    }
danpost danpost

2018/1/28

#
I just noticed the extra conditions to the right (outside the code viewporr) on the two lines. Remove the '&& walkRate == 10' which is still at the end of your new line 30. The only condition that you need for calling 'LeftImages' and 'RightImages' is that the corresponding movement key is down. So, move those calls to where you call 'setLocation' (where the only condition is that the key is down). You can remove lines 17 and 34 as the two image methods will do that for you (once fixed properly). The condition on lines 58 and 75 should be 'walkRate == 0 || walkRate == 20'. Move lines 56 and 72 into these 'else' blocks. You need the value of walkRate to reach 20 before resetting int.
roonie01 roonie01

2018/1/28

#
ok did that
roonie01 roonie01

2018/1/28

#
but now my actor only faces right
There are more replies on the next page.
1
2