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

2012/8/16

function Actor.move(int) works curiously

sasafed sasafed

2012/8/16

#
Hi All! I am a beginner with Greenfoot but I have some real projects in Java and Delphi. Now I try to use Greenfoot. When I fist used "move(int)" in Actor I firstly think that it is not work at all. Objects at once move to world's border with single call of move(1). Then decreasing cell's size to 1 I could use the function properly. But increasing the cell's size I mention that objects move not by cell-size dist but by cell-size square. I.e. if cell size is 10 the call of move(1) relocating the object to 100 pixels. move(3) to 300 pixels...
public class Terra extends World
{
    public Terra() {  super(50, 30, 20);  }
} 

public class Bug extends Actor
{
    public void act()  {
        move(1);
   }    
}
In this code with one call of act() the Bug move to left on 400 pixels... How this can be?
sasafed sasafed

2012/8/16

#
I am sorry, Bug move to right :) May be need some initialization?
davemib123 davemib123

2012/8/16

#
I have this for moving left and right:
   /**
     * Move a bit to the left.
     */
    public void moveLeft(){
        setLocation ( getX() - velocity, getY() );
    }

    /**
     * Move a bit to the right.
     */
    public void moveRight(){
        setLocation ( getX() + velocity, getY() );
    }

  public void checkKeys(){
        if (Greenfoot.isKeyDown("left") || (Greenfoot.isKeyDown("A"))) {
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") || (Greenfoot.isKeyDown("D"))) {
            moveRight();
        }
}
SPower SPower

2012/8/16

#
if the size of the cell is 10 by 10 pixels, and you say this:
move(1);
you move 1 cell, which will be 10 pixels. The move method is meant to move forward based on your rotation. So if you set rotation to, let's say, 30 degrees:
setRotation(30);
and you move 1 cell forward, you will move different then when you'd have set your rotation to 100 degrees. I hope this makes a bit sense :)
danpost danpost

2012/8/16

#
This discussion was about that very thing.
sasafed sasafed

2012/8/16

#
davemib123 wrote...
I have this for moving left and right: Thank you! Of course setLocation(x,y) works properly and I really can use it instead of move(int). But this function is present and why must I be prevented to use it?
sasafed sasafed

2012/8/16

#
danpost wrote...
This discussion was about that very thing.
Thank you. Now the question can be closed. I am sorry I couldn't find this earlier. My search in move(int) didn't bring me the discussion :(
danpost danpost

2012/8/16

#
I tried that very search with no results, but I KNEW that it was there somewhere. So, I did a manual search (browse through).
You need to login to post a reply.