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

2020/2/27

Setting an Image if "left" and "right" keys are not pressed

DaRafster DaRafster

2020/2/27

#
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:
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);
        }

    }



    
}
and here is the subclass code of Movers named Sonic:
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();
    }    
}
danpost danpost

2020/2/27

#
You could put an else at the beginning of line 39 and add an else clause at line 44 to set the last image. You probably won't like the feel of how the movement keys react (mess with different combinations and changes to see what I mean). You will find it will be biased to moving left. Hold one key and toggle the other up and down -- then switch.
DaRafster DaRafster

2020/2/27

#
danpost wrote...
You could put an else at the beginning of line 39 and add an else clause at line 44 to set the last image. You probably won't like the feel of how the movement keys react (mess with different combinations and changes to see what I mean). You will find it will be biased to moving left. Hold one key and toggle the other up and down -- then switch.
I've played around with "else" before I posted this discussion, couldn't figure it out. I managed to do it with the right arrow key, but I couldn't manage to do it with the left arrow key. On my second attempt, it was the other way around. May you please demonstrate what conditions I could put in my else statement in order for me to achieve this?
danpost danpost

2020/2/27

#
DaRafster wrote...
I've played around with "else" before I posted this discussion, couldn't figure it out. I managed to do it with the right arrow key, but I couldn't manage to do it with the left arrow key. On my second attempt, it was the other way around. May you please demonstrate what conditions I could put in my else statement in order for me to achieve this?
Please show what you tried.
You need to login to post a reply.