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

2014/5/27

Help with switching images

qwakery qwakery

2014/5/27

#
I'm trying to make a button which when pressed, changes to a different image and changes a boolean. Basically theres an on and an off state - if you click the button when its off, it will turn on, and if you click the button when its on, it will turn off. How would I code this?
erdelf erdelf

2014/5/27

#
i think this should work
public boolean on;
public void act()
{
  if(Greenfoot.mouseClicked(this))
  {
        setImage(new GreenfootImage(on?"on.png":"off.png"));
        on !=on;
}
}
danpost danpost

2014/5/27

#
erdelf wrote...
i think this should work
public boolean on;
public void act()
{
  if(Greenfoot.mouseClicked(this))
  {
        setImage(new GreenfootImage(on?"on.png":"off.png"));
        on !=on;
}
}
I believe lines 6 and 7 should be reversed if you want to show the image that reflects the current state).
erdelf erdelf

2014/5/28

#
@danpost, oh yeah right, forgot about that
qwakery qwakery

2014/5/28

#
danpost wrote...
erdelf wrote...
i think this should work
public boolean on;
public void act()
{
  if(Greenfoot.mouseClicked(this))
  {
        setImage(new GreenfootImage(on?"on.png":"off.png"));
        on !=on;
}
}
I believe lines 6 and 7 should be reversed if you want to show the image that reflects the current state).
When i compile, there is an error on line 7 (or line 6 after youve reversed 6 and 7) saying 'not a statement. Help?
danpost danpost

2014/5/28

#
Sorry, that should be
on = !on;
erdelf erdelf

2014/5/28

#
my fault, sry danpost is right
qwakery qwakery

2014/5/28

#
thanks guys, works perfectly now :-D
You need to login to post a reply.