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

2017/10/7

Changing the speed of my Actor

Delucaria Delucaria

2017/10/7

#
Hey, I got a problem with my code and I don't know what could possibly be wrong. I want my actor to move faster by pressing a and slower by pressing d. It's working just fine for the faster part, but it doesn't work for the slowing down one. My code looks like this: public void act() { int speed=1; move(speed); if (Greenfoot.isKeyDown("up")){ setRotation(270); } if (Greenfoot.isKeyDown("down")){ setRotation(90); } if (Greenfoot.isKeyDown("left")){ setRotation(180); } if (Greenfoot.isKeyDown("right")){ setRotation(0); } if (Greenfoot.isKeyDown("a")){ speed = speed+1; move(speed); } if (Greenfoot.isKeyDown("d")){ speed = speed-1; move(speed); } }
Super_Hippo Super_Hippo

2017/10/7

#
Put the 'int speed=1' part out of the act method or you are setting it to 1 every time again.
Delucaria Delucaria

2017/10/7

#
But if I delete it, Greenfoot tells me speed isn't initialized. Even if it would be set to 1 every time, then the speed up wouldn't work either I guess, but it does work. I'm just confused as to why the speed up if method works, but the speed down one doesn't
danpost danpost

2017/10/7

#
Delucaria wrote...
But if I delete it
Hippo did not say to delete it -- what was said was to move the line so that it is outside the act method (move the two lines above it down so that they are below that line).
You need to login to post a reply.