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

2016/11/13

How can make my actor move right to left?

lolitsme lolitsme

2016/11/13

#
Hi! What codes can help me make my Actor move from right to left without having to press any keys. Which means its automatically moving. Thank you and God bless!
Super_Hippo Super_Hippo

2016/11/13

#
public void act()
{
    setLocation(getX()-1, getY());
}
lolitsme lolitsme

2016/11/13

#
I tried doing that but it always stopped. Do you know how can make the movement continuous?
Super_Hippo Super_Hippo

2016/11/13

#
Do you click on "run" or on "act"? If you click on "run", it shouldn't stop (until it reaches the left side of your world).
FutureCoder FutureCoder

2016/11/13

#
try this private int count1 = 0; private int count2 = 0; private int count3 = 0; public void move() { if(count1 == 0) { move(4); count2 = count2 +1; } if(count2 == 20) { move(-4); count3 = count3 +1; count1 = 1; } if(count3 == 20) { count1 = 0; count2 = 0; count3 = 0; } } this should work
lolitsme lolitsme

2016/11/13

#
Thank you FutureCoder!! :) It really helped a lot.
danpost danpost

2016/11/13

#
There is also this:
private int dir = -1;
private int speed = 4;

public void act()
{
    move(dir*speed);
    if (isAtEdge()) dir = -dir;
}
You need to login to post a reply.