Hello,
How can I make a button react to click mouse? I am creating a jukebox and I have added five different images for each song as well as the songs. I don't know how to make the buttons to act. I want to be able to program the song buttons so that clicking one of them selects the song to play and plays it, like pressing a button on a real jukebox. When a song starts to play, the ‘Play’ button image changes to the ‘Pause’ image.
Please help!
Thanks
import greenfoot.*;
/**
* Write a description of class Button here.
*
*
* @version 02
*/
public class Button extends Actor
{
// Variables that are active for the entire Button class
private static int songPlaying = 0; // The number (1, 2, ...) of the song playing, or 0 if no song is playing
// Instance variables that are active for each instance of a Button
private String image; // This button's image
private String song; // This button's song name, or blank if the button is not a song
private int buttonType; // 1, 2, 3, ... for a song button, -1 for play/pause, -2 for stop
private GreenfootSound gfs; // The song to play
private boolean isMouseClicked; // True if the mouse was clicked on this instance
/**
* Button constructor called when a new instance of a Button is created.
*/
public Button(String imageName, String songName, int typeOfButton)
{
image = imageName;
setImage(image);
song = songName;
gfs = new GreenfootSound(song);
buttonType = typeOfButton;
}
/**
* Act - do whatever the Button wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
isMouseClicked = Greenfoot.mouseClicked(this);
checkSongPlaying();
}
/**
* checkSongPlaying - if a song is playing, see if we stop it. If a song is not playing, see if we start it
*/
private void checkSongPlaying()
{
if (isMouseClicked)
{
getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
}
if (isMouseClicked && buttonType < 0)
{
if (songPlaying == 0)
{
getWorld().showText("Hey! There's nothing playing!", 250, 380);
}
else
{
getWorld().showText("Stopping song " + songPlaying, 250, 380);
songPlaying = 0;
}
}
if (isMouseClicked && songPlaying == 0 && buttonType > 0)
{
getWorld().showText("Starting song " + buttonType + " named " + song, 250, 380);
songPlaying = buttonType;
}
}
}
