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

2013/11/23

When mouse is clicked button change for "3 seconds"

Yedya Yedya

2013/11/23

#
So whenever the mouse is clicked on a button,the image changes.But not for long enough,its only for a split second.How would I have the image change for >1 second?
danpost danpost

2013/11/23

#
Add an instance int field to use as a timer. When the button is clicked, change the button and set the timer to 100 or so. If the value of the timer is greater than zero, have the act method decrement it and check for zero. If zero, change the image back.
Yedya Yedya

2013/11/23

#
I don't understand the 2nd part of the of your logic.The if statement is within the the act method. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class E extends Actor { public void act() { int timer=0; if(Greenfoot.mouseClicked(this)) { play(); setImage("left.png"); timer=100; }// Add your action code here. if(Greenfoot.mouseClicked(this)) { if(timer>0) { // ? } } } public void play() { Greenfoot.playSound("E3.mp3"); } }
danpost danpost

2013/11/23

#
The timer needs to be declared outside the act method and decrement within the act method only if its value is greater than zero:
int timer;

public void act()
{
    if (timer > 0)
    {
        timer--;
        if (timer == 0) /* change image back */;
    }
    else if (Greenfoot.mouseClicked(this))
    {
        play();
        setImage("left.png");
        timer = 100;
    }
}
Yedya Yedya

2013/11/23

#
Thanks alot! Works perfect! How can I + your rep?
You need to login to post a reply.