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

2020/6/4

how do i make an object move forward in the direction its facing

ninjapuffin ninjapuffin

2020/6/4

#
I'm having a problem making an object move forward in the direction its facing can someone give me the code for it thanks
Yehuda Yehuda

2020/6/4

#
Explain what you did and why it's not what should be happening. From your question it sounds like you're changing the way an actor is facing then telling it to move. It moves but not in a new direction. Maybe you didn't change the way it's facing but rather its image. or maybe you're not using the move() method...
ninjapuffin ninjapuffin

2020/6/4

#
this is what i did
public void act() 
    {
        if (Greenfoot.isKeyDown("left") )
        {
            turn(5);
        }
        if (Greenfoot.isKeyDown("right") )
        {
            turn(-5);
        }
        
        if (Greenfoot.isKeyDown("up") )
        {
            moveUp();
        }
    }
    
    public void moveUp()
    {
        setLocation ( getX(), getY() - speed );
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }

    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
Yehuda Yehuda

2020/6/4

#
According to the methods you created, you should be calling moveLeft for the first "if", and moveRight for the second "if" in order to move in those directions. Look at how you coded your move methods.
danpost danpost

2020/6/4

#
Use move(int) to move in facing direction. Bear in mind that at low speeds, the direction moved may be off a bit due to pixel positions. If needed, you can correct for that by using a smooth mover system.
ninjapuffin ninjapuffin

2020/6/4

#
like this
move(int);
danpost danpost

2020/6/4

#
ninjapuffin wrote...
like this
move(int);
No. That is the method signature (without the semi-colon). You need an int value or expression for 'int'.
Yehuda Yehuda

2020/6/4

#
See move(int distance) The number you put inside is the distance the Actor will move (in one act cycle). The greater the number, the faster the movement. With this method from the Actor class, the Actor will move the specified amount in the direction it's facing. In your case you can use move(speed) for the up arrow, and then your three move methods can be deleted.
ninjapuffin ninjapuffin

2020/6/5

#
Yehuda wrote...
According to the methods you created, you should be calling moveLeft for the first "if", and moveRight for the second "if" in order to move in those directions. Look at how you coded your move methods.
i didn't need those methods because i can't turn with them i can only move left and right
RcCookie RcCookie

2020/6/5

#
So if you want your Actor to move sideways you can do this:
if(Greenfoot.isKeyDown(„left“)){
    moveLeft();
}
if(Greenfoot.isKeyDown(„right“)){
    moveRight();
}
if(Greenfoot.isKeyDown(„up“)){
    moveUp();
}
If you want your actor to be steered by the left and right key and always move in the direction it is facing, you want to do something like this:
if(Greenfoot.isKeyDown(„left“)){
    turn(-5);
}
if(Greenfoot.isKeyDown(„right“)){
    turn(5);
}
if(Greenfoot.isKeyDown(„up“)){
    move(speed);
}
In this case though, like danpost mentioned above, you will have problems at low speeds(speed < 3). If you want that you may want to consider to use SmoothMover.
You need to login to post a reply.