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

2012/5/28

Actor Movement Help

1
2
DarrenM2012 DarrenM2012

2012/5/28

#
I'm trying to move my actor one space up, then back down, then down again and finally up and then continue (Y-1,Y+1,Y+1,Y-1). I have the following code atm but it doesn't work any help would be grateful.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Mummie1 here.
 * 
 * @author () 
 * @version (a version number or a date)
 */
public class Mummie1 extends Actor
{

    int startX;
    int startY;
    boolean goingup=true;

    public Mummie1(int X, int Y)
    {
       setLocation(X,Y);
        startX = X;
        startY = Y;
    }
  
    
    public void act() 
    {
        int currX=getX();
        int currY=getY();

        if(currY==startY)
        {
            if (goingup==false)
            {
                setLocation(startY+1,currX);
                goingup=true;
            }
            else
            {
                setLocation(startY-1,currX);
                goingup=false;
            }
        }
       //else if (currY==startY+1)
       else
            setLocation(startY, currX); 
      //else if (currY==startY-1)  
      //      setLocation(startY,currX);  
      
      FindCharacter();
    }

private void FindCharacter()//Finds Character
 
    {
        Actor thisCharacter = getOneObjectAtOffset (0,0, Character.class);//Checks if characters is in that cell
        if(thisCharacter!=null)
        getWorld().removeObject(thisCharacter);//Removes the Character
        List allCharacter = getWorld().getObjects(Character.class);//Find Character in the world
        if (allCharacter.isEmpty())//Checks if Character have gone
        Greenfoot.stop();
    }
    
}
davmac davmac

2012/5/28

#
Please use 'code' tags for code - not 'quote' - I've fixed it this time.
MatheMagician MatheMagician

2012/5/28

#
There are two possible errors I notice. When you initiate the actor the startX and startY coordinates might not be right. At the beginning you are making it move up. But when it moves up it is no longer at the original startY. Then it moves back down, but it still thinks it needs to move down more. Both errors are fixed by this code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
import java.util.List;  
  
/** 
 * Write a description of class Mummie1 here. 
 *  
 * @author ()  
 * @version (a version number or a date) 
 */  
public class Mummie1 extends Actor  
{  
    boolean goingup=true;  
    public void act()   
    {  
       if (goingup==false)  
       {  
          setLocation(getX(),getY()+10);  
          goingup=true;  
       }  
       else  
       {  
          setLocation(getX(),getY()-10);  
          goingup=false;  
       }    
       //else if (currY==startY-1)    
       //      setLocation(startY,currX);    
       FindCharacter();  
    }  
    private void FindCharacter()//Finds Character  
   
    {  
        Actor thisCharacter = getOneObjectAtOffset (0,0, Character.class);
        //Checks if characters is in that cell  
        if(thisCharacter!=null)  
            getWorld().removeObject(thisCharacter);//Removes the Character  
        List allCharacter = getWorld().getObjects(Character.class);//Find Character in the world  
        if (allCharacter.isEmpty())//Checks if Character have gone  
        Greenfoot.stop();  
    }  
}  
MatheMagician MatheMagician

2012/5/28

#
Oops, I changed it from y+1 to y+10, so here is the substituted code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
import java.util.List;  
  
/** 
 * Write a description of class Mummie1 here. 
 *  
 * @author ()  
 * @version (a version number or a date) 
 */  
public class Mummie1 extends Actor  
{  
    boolean goingup=true;  
    public void act()   
    {  
       if (goingup==false)  
       {  
          setLocation(getX(),getY()+1);  
          goingup=true;  
       }  
       else  
       {  
          setLocation(getX(),getY()-1);  
          goingup=false;  
       }    
       //else if (currY==startY-1)    
       //      setLocation(startY,currX);    
       FindCharacter();  
    }  
    private void FindCharacter()//Finds Character  
   
    {  
        Actor thisCharacter = getOneObjectAtOffset (0,0, Character.class);
        //Checks if characters is in that cell  
        if(thisCharacter!=null)  
            getWorld().removeObject(thisCharacter);//Removes the Character  
        List allCharacter = getWorld().getObjects(Character.class);//Find Character in the world  
        if (allCharacter.isEmpty())//Checks if Character have gone  
        Greenfoot.stop();  
    }  
}  
DarrenM2012 DarrenM2012

2012/5/28

#
Thanks for that code it all works, but it does need to go down after it has gone up and then returned to the start it, it should go up one space, down one space to the beginning which it does do, it then needs to go down one space, and then return to the start again.
danpost danpost

2012/5/28

#
Just a thought, you could replace lines 12 through 28 with the following:
int step = 0;

public void act()
{
    step = (step + 1) % 4; 
    setLocation(getX(), getY() - 1 + 2 * (step / 2));
    FindCharacter();
}
No 'if's, 'else's, or 'boolean's!
DarrenM2012 DarrenM2012

2012/5/28

#
Ah thank you very much!! :D Much appreciated.
danpost danpost

2012/5/28

#
Now, do you understand what the code is doing (at least as far as lines 5 and 6)? Can you give a step-by-step explanation of it?
MatheMagician MatheMagician

2012/5/28

#
Oh, I see. That explains your trouble. If you would like another way of doing this besides Danpost's, here is my modified one.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)    
import java.util.List;    
    
/**  
 * Write a description of class Mummie1 here.  
 *   
 * @author ()   
 * @version (a version number or a date)  
 */    
public class Mummie1 extends Actor    
{    
    boolean goingup=true;   
    boolean new2 = true;
    boolean right = true;
    int y = 0;
    public void act()     
    {    
        if(new2 == true){
            setLocation(getX(),getY()-1);
            new2 = false;
        }
        else if(right == true){
            setLocation(getX(), getY()+1);
            y++;
        }
        else if(right == false){
            setLocation(getX(), getY()-1);
            y--;
        }
        if(y > 2)
        {
            right = false;
        }
        if(y < 0)
        {
            right = true;
        }
        //else if (currY==startY-1)      
        //      setLocation(startY,currX);      
        FindCharacter();    
    }    
    private void FindCharacter()//Finds Character    
     
    {    
        Actor thisCharacter = getOneObjectAtOffset (0,0, Character.class);  
        //Checks if characters is in that cell    
        if(thisCharacter!=null)    
            getWorld().removeObject(thisCharacter);//Removes the Character    
        List allCharacter = getWorld().getObjects(Character.class);//Find Character in the world    
        if (allCharacter.isEmpty())//Checks if Character have gone    
        Greenfoot.stop();    
    }    
}
DarrenM2012 DarrenM2012

2012/5/28

#
Now, do you understand what the code is doing (at least as far as lines 5 and 6)? Can you give a step-by-step explanation of it?
    step = (step + 1) % 4;   
    setLocation(getX(), getY() - 1 + 2 * (step / 2));  
    
Er, well I suppose it's getting the current co-ordinate, then moving - 1 spaces, then + 2, then the rest I have no idea.
danpost danpost

2012/5/28

#
Alright: let investigate line 1 first. Before we do, I may need to explain the '%' operator. What is does is: it takes the value on the left and divides it by the value on the right; but instead of returning the number of times it went into the number, it returns the remainder part (example: In maths: 13 / 5 = 2 r 3; in java, as int values: 13 / 5 returns 2 and 13 % 5 returns 3 <the remainder part>). Now, line 1: each act cycle the value of 'step' is incremented, and then returns the remainder after dividing the incremented value by 4 (the number of steps in your movement before repetition occurs). So, on consecutive act cycles, the value of 'step' will rotate { 1, 2, 3, 0, 1, 2, 3, 0, 1, ...}. Next, line 2: with the 'step' values in line 1, any number more than 1, when divided by 2 will be 1, else it will be 0. Notice how the sequence of this will be { 0, 1, 1, 0, 0, 1, 1, 0, ...} (sort of like your { up, down, down, up, up, down,...}). Multiplying this sequence by 2 and subtracting 1 will result in the following sequence { -1, 1, 1, -1, -1, 1, 1, ...}. This sequence is what we get from '(step / 2) * 2 - 1', which is equivalent to '-1 + 2 * (step / 2)'. So, you see, all offsets in the movement are calculated mathematically; and there is no need for any checking of values.
MatheMagician MatheMagician

2012/5/28

#
Yours is the more elegant solution and thank you for giving the remainder operator. I have wanted to find the remainder in my programs before.
danpost danpost

2012/5/28

#
Operators, in the java trails has information about this; plus, links to a whole lot more.
MatheMagician MatheMagician

2012/5/28

#
Thanks! :)
IsVarious IsVarious

2012/5/29

#
you could of simply made a method which did this, (what I feel is a lot simpler to understand). and just call that method. Call the method something like "movementPath()" and it would look like this.
movementPath()
{

	turn(-90); 	 // turns up, 
	move(); 	 // moves one space up. 
	turn(180); 	 // faces down. 
	move();  	 // moves one space in that direction. 
	move();  	 // moves one space in that direction. 
	turn(-180);  // faces up. 
	move();  	 // moves one space in that direction. 
	turn(90);	 // turn back towards orginal direction. 
	move();	// moves one space in that direction. 
}
There are more replies on the next page.
1
2