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

2016/11/8

Creating Buttons?

rn42v1r rn42v1r

2016/11/8

#
Hi there, I'd like to create a button which turns music on and off. How do I do this? I already created images for the "pause" and "play" button...
danpost danpost

2016/11/9

#
Create an Actor subclass for the button. Save the "pause" and "play" images in fields so you can check to see which one the actor currently has -- to determine what action to do and what image to set to it next. Make sure you initialize the image of the actor when you create it (with one of those referenced in the fields).
rn42v1r rn42v1r

2016/11/9

#
danpost wrote...
Create an Actor subclass for the button. Save the "pause" and "play" images in fields so you can check to see which one the actor currently has -- to determine what action to do and what image to set to it next. Make sure you initialize the image of the actor when you create it (with one of those referenced in the fields).
Sry, Im pretty new to Greenfoot and programming in general, what exactly do you mean with "fields"? I thought it would be a good idea if I created 2 subclasses (one for each state of the button, play and pause) and let one disappear as I click on it and let the other spawn...
danpost danpost

2016/11/9

#
rn42v1r wrote...
I thought it would be a good idea if I created 2 subclasses (one for each state of the button, play and pause) and let one disappear as I click on it and let the other spawn...
That is another way to accomplish it.
Im pretty new to Greenfoot and programming in general, what exactly do you mean with "fields"?
"fields" are what give objects their different states -- and "methods" give them their behavior. The Actor class provides several states for its instance (objects created from its subclasses)
  • World world - the world the actor is currently in ('null' if not in a world)
  • int x - the x-coordinate value of its location in the world
  • int y - the y-coordinate value of its location in the world
  • int rotation - the current rotation of the actor
  • GreenfootImage image - the image the actor currently displays
You can have your Actor objects retain other objects and state by using fields in their classes. One of the first couple of pages in the java tutorials explains this.
rn42v1r rn42v1r

2016/11/10

#
I found my mistake and managed to make my "MusicON" button play the music. Now my "MusicOFF" button doesn't want to stop it though... Maybe this is helpful
1
2
3
4
5
6
7
import greenfoot.*;
 
public class Buttons extends Actor
{   GreenfootSound backroundMusic = new GreenfootSound("SpongebobRemix.mp3");
     
     
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*;
 
public class MusicON extends Buttons
{  
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            backroundMusic.playLoop();
            getWorld().addObject(new MusicOFF(), getX(), getY());
            getWorld().removeObject(this);
         
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import greenfoot.*;
 
public class MusicOFF extends Buttons
{  
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            backroundMusic.pause();
            getWorld().addObject(new MusicON(), getX(), getY());
            getWorld().removeObject(this);
        }
    }   
}
danpost danpost

2016/11/10

#
Even though both classes extend the Buttons class, you are creating a separate instance of the music with each button created. In other words, One button changes one GreenfootSound object and the other changes a different GreenfootSound object. In the Buttons class, add the 'static' modifier to the beginning of the line 4 (above, after the '{'). Then, the object will belong to the class itself instead of a separate instance of it for each Buttons object created from its subclasses.
rn42v1r rn42v1r

2016/11/10

#
Thank you very much, it's working :D
You need to login to post a reply.