So I have a method called animate() and I need to stop it while a variable called isGoingFast is true and when isGoingFast is false, resume the animate() method.. Does anyone have any idea how I go about coding this?
Here's my code:
Here's what I tried to do, but when I run my code it just stops everything.
import greenfoot.*;
public class Plane extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;
private boolean isGoingFast;
private GreenfootImage image3;
public void act()
{
handleMovement();
rocketCollision();
grabBarrel();
animate();
}
public void rocketCollision()
{
if (isTouching(Rocket.class))
{
setLocation( 400, 300);
Greenfoot.playSound("Bang.wav");
BarrelWorld barrelWorld = (BarrelWorld)getWorld();
barrelWorld.removeLives(-1);
}
if (isTouching(Barrel.class))
{
BarrelWorld barrelWorld = (BarrelWorld)getWorld();
barrelWorld.updateScore(+1);
}
}
public void handleMovement()
{
move(2);
if (Greenfoot.isKeyDown("left"))
{
turn(-2);
}
if (Greenfoot.isKeyDown("right"))
{
turn(2);
}
if (Greenfoot.isKeyDown("up"))
{
move(3);
}
else
{
move(2);
}
//Modify the handleMovement() method so that the isGoingFast variable is set to true when the up arrow is clicked and set to false if it isn’t.
if (Greenfoot.isKeyDown("up"))
{
isGoingFast = true;
setImage(image3);
}
else
{
isGoingFast = false;
}
}
public void grabBarrel()
{
if (isTouching(Barrel.class))
{
removeTouching(Barrel.class);
getWorld().addObject (new Barrel(250), (Greenfoot.getRandomNumber(650)+70), (Greenfoot.getRandomNumber(450)+70));
Greenfoot.playSound("Waha.wav");
}
}
public void animate()
{
if (getImage().equals(image1))
{
setImage(image2);
}
else
{
setImage(image1);
}
if (Greenfoot.isKeyDown("up"))
{
Greenfoot.stop();
}
else
{
Greenfoot.start();
}
}
public Plane()
{
image1 = new GreenfootImage("airplane1.png");
image2 = new GreenfootImage("airplane2.png");
image3 = new GreenfootImage("airplaneFaster.png");
isGoingFast = false;
setImage(image1);
}
}
public void animate()
{
if (getImage().equals(image1))
{
setImage(image2);
}
else
{
setImage(image1);
}
if (Greenfoot.isKeyDown("up"))
{
Greenfoot.stop();
}
else
{
Greenfoot.start();
}
}
