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

2014/3/15

Movement Troubles

hugtvy hugtvy

2014/3/15

#
Is there a way to make an actor move left-right up-down regardless of which direction they are facing, eg if the mouse is facing north east if i press "w" the character will still travel north? Any help is appreciated.
danpost danpost

2014/3/15

#
Yes. Just use the 'setLocation' method for moving instead of the 'move' method. Please refer to the Actor class documentation.
hugtvy hugtvy

2014/3/16

#
Could you give me a link because I cannot find it.
danpost danpost

2014/3/16

#
It is here.
hugtvy hugtvy

2014/3/16

#
im an idiot so yet again i dont get it, could you please just write the code for left right up and down?
danpost danpost

2014/3/16

#
I usually use the following movement code:
public void move()
{
    int dx = 0, dy = 0; // to hold user instructions
    if (Greenfoot.isKeyDown("w")) dy--;
    if (Greenfoot.isKeyDown("a")) dx--;
    if (Greenfoot.isKeyDown("s")) dy++;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (dx*dy != 0 || dx+dy == 0) return; // remove this line for 8-way movement
    setLocation(getX()+dx, getY()+dy);
    // check for intersections and reverse move if needed
}
hugtvy hugtvy

2014/3/16

#
cheers thats really useful, is there anyway to actually make the actor move faster so that it is the equal to move(2)?
danpost danpost

2014/3/16

#
Just multiply the change in coordinates by 2:
setLocation(getX()+2*dx, getY()+2*dy);
hugtvy hugtvy

2014/3/16

#
cheers that worked
You need to login to post a reply.