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

2018/1/31

How can i make an image change when i press a button

1
2
danpost danpost

2018/1/31

#
frequency wrote...
Should i add as setimage of the classes the pressed and unpressed.png?
You should add the Circle class above (with the change on line 22) and delete both the Pressed and Unpressed classes from the project. No image needs to be applied to the Circle class (the code within the class takes care of its image).
frequency frequency

2018/2/1

#
Where you say "GAME WORLD" which world should i enter?
import greenfoot.*;
 
public class Circle extends Actor
{
    GreenfootImage upImage = new GreenfootImage("unpressed.png"); // adjust filename as needed
    GreenfootImage downImage = new GreenfootImage("pressed.png"); // adjust filename as needed
    boolean spaceDown;
 
    public Circle()
    {
        setImage("unpressed.png");
    }
 
    public void act()
    {
        if (spaceDown != Greenfoot.isKeyDown("space")) // was there a change in state of key
        {
            spaceDown = !spaceDown; // save new state
            if (spaceDown) // is down the new state
            {
                setImage(downImage);
                ((MyWorld)getWorld()).bumpScore(); // pseudo-code to have world increment score counter; adjust as needed
            }
            else setImage(upImage);
        }
    }
}
danpost danpost

2018/2/1

#
Since you have 3 possible worlds the circle can be in, you will need to replace line 22 with the following:
if (getWorld() instanceof World30) ((World30)getWorld()).bumpScore();
else if (getWorld() instanceof World60) ((World60)getWorld()).bumpScore();
else if (getWorld() instanceof World90) ((World90)getWorld()).bumpScore();
frequency frequency

2018/2/1

#
i GET Underlines http://prntscr.com/i8porc
frequency frequency

2018/2/1

#
I MADE IT! DAMN I AM SO HAPPY MAN! Take a look. http://www.greenfoot.org/scenarios/20778 Now i must make it reset the score and fix the count down timer!
You need to login to post a reply.
1
2