I'm making a game and I have two sonic images, one going back and one going forward. I've made it so when you press the left key, the "sonicRunningBackwards" image pops up, and if you press the right key, the "sonicRunning" image pops up.
So how would I conduct a code that would set the image "Sonic" when the left and right keys are not pressed?
Here is my "Movers" class code:
and here is the subclass code of Movers named Sonic:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Movers here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Movers extends Actor
{
GreenfootImage sonic = new GreenfootImage("Sonic.png");
GreenfootImage sonicRunning = new GreenfootImage("Sonic Running Animation.png");
GreenfootImage sonicRunningBackwards = new GreenfootImage("Sonic Running Animation 2.png");
/**
* Act - do whatever the Movers wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
public Movers()
{
sonic.scale(sonic.getWidth()/3, sonic.getHeight()/3);
sonicRunning.scale(sonicRunning.getWidth()/3, sonicRunning.getHeight()/3);
sonicRunningBackwards.scale(sonicRunningBackwards.getWidth()/3, sonicRunningBackwards.getHeight()/3);
}
public void movingWithArrows()
{
if(Greenfoot.isKeyDown("left"))
{
move(-3);
setImage(sonicRunningBackwards);
}
if(Greenfoot.isKeyDown("right"))
{
move(3);
setImage(sonicRunning);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Sonic here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Sonic extends Movers
{
public Sonic()
{
GreenfootImage sonic = getImage();
sonic.scale(sonic.getWidth()/4, sonic.getHeight()/4);
setImage(sonic);
}
/**
* Act - do whatever the Sonic wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
movingWithArrows();
}
}

