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
frequency frequency

2018/1/31

#
Hello, i want to make this https://www.youtube.com/watch?v=fSp7DeQ4IO0. I want the image X change to image Y when a button is pressed. The image X must change to its original state when the button stops being pressed. I want to make the red circle! SCENARIO http://www.greenfoot.org/scenarios/20770
danpost danpost

2018/1/31

#
By "button", are you referring to a key or a mouse button?
frequency frequency

2018/1/31

#
"space",a keyboard button!
danpost danpost

2018/1/31

#
Use one class for the circle (you will just be changing the image set to it). You will need a boolean field to track the state of the space key. A single Circle class would be like this:
import greenfoot.*;

public class Circle extends Actor
{
    GreenfootImage upImage = new GreenfootImage("upImage.png"); // adjust filename as needed
    GreenfootImage downImage = new GreenfootImage("downImage .png"); // adjust filename as needed
    boolean spaceDown;

    public Circle()
    {
        setImage(upImage);
    }

    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);
                ((Game)getWorld()).bumpScore(); // pseudo-code to have world increment score counter; adjust as needed
            }
            else setImage(upImage);
        }
    }
}
frequency frequency

2018/1/31

#
What will the set image be when i create the actor?
danpost danpost

2018/1/31

#
frequency wrote...
What will the set image be when i create the actor?
Lines 9 through 12 is the constructor block. It sets the initial image to the up state.
frequency frequency

2018/1/31

#
the code is
import greenfoot.*;
 
public class Circle extends Actor
{
    GreenfootImage unpressed = new GreenfootImage("unpressed.png"); // adjust filename as needed
    GreenfootImage pressed = new GreenfootImage("pressed.png"); // adjust filename as needed
    boolean spaceDown;
 
    public Circle()
    {
        setImage(upImage);
    }
 
    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(pressed.png);
                ((Game)getWorld()).bumpScore(); // pseudo-code to have world increment score counter; adjust as needed
            }
            else setImage(unpressed.png);
        }
    }
}
((Game)getWorld()).bumpScore(); // pseudo-code to have world increment score counter; adjust as needed
what should i enter in game?
danpost danpost

2018/1/31

#
frequency wrote...
what should i enter in game?
First, you should "combine" World_30, World_60 and World_90 into one class (which I called Game in the code above). This should not be hard to accomplish. Really the only things you will need is an int parameter for the constructor and a field to hold whatever value is passed as the argument. Some slight adjustments will probably be needed in the rest of the code; but it is just a matter of taking hard-coded values and converting them to expressions using the field value.
frequency frequency

2018/1/31

#
Can i avoid this step? I have 2 days left for this. Cant it work with my other classes?
danpost danpost

2018/1/31

#
frequency wrote...
Can i avoid this step? I have 2 days left for this. Cant it work with my other classes?
Okay -- then you would either need three up buttons and three down buttons or you would have to perform some checks to determine which world the button was in so that the score can be adjusted in it.
frequency frequency

2018/1/31

#
I had 2 classes. Pressed and Unpressed. Do you mean that?
danpost danpost

2018/1/31

#
frequency wrote...
I had 2 classes. Pressed and Unpressed. Do you mean that?
Yes, I was referring to those classes. The checking would not be too hard though. Line 22 would be replaced with:
if (getWorld() instanceof World_30) ((World_30)getWorld()).bumpScore();
else if (getWorld() instanceof World_60) ((World_60)getWorld()).bumpScore();
else ((World_90)getWorld()).bumpScore();
Remove the 'checkKeyboard();' call in your world act methods and change the name of the 'checkKeyboard' method to 'bumpScore'.
frequency frequency

2018/1/31

#
Should this code be given in the classes "pressed and unpressed"?
danpost danpost

2018/1/31

#
frequency wrote...
Should this code be given in the classes "pressed and unpressed"?
Neither. It replaces line 22 in the Circle class above which, in turn, replaces both the Pressed and Unpressed classes.
frequency frequency

2018/1/31

#
Should i add as setimage of the classes the pressed and unpressed.png?
There are more replies on the next page.
1
2