I'm trying to make it so that if a song is playing, and another song is selected, the first song stops playing. I created a refresh setting, but I'm confused about exactly what I've done wrong.
PlayMusicButton Actor:
PlayerWorld world:
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
public class PlayMusicButton extends Actor
{
private String soundFiles;
private String songNames;
private Color colors;
private static final int WIDTH = 220;
private static final int HEIGHT = 40;
private boolean selected;
public boolean playing;
private int song;
public PlayMusicButton(String soundFiles, String songNames, Color colors)
{
this.soundFiles = soundFiles;
this.songNames = songNames;
this.colors = colors;
selected = false;
playing = false;
makeImage();
}
public void act()
{
checkMouseClick();
}
public void refresh()
{
if(GreenfootSound.isPlaying())
{
GreenfootSound.stop();
}
}
private void checkMouseClick()
{
if(!selected && Greenfoot.mouseClicked(this)) {
PlayerWorld pw = (PlayerWorld)getWorld();
pw.refresh();
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(colors);
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(Color.BLACK);
image.drawRect(0, 0, WIDTH-1, HEIGHT-1);
image.setColor(Color.BLACK);
Font font = image.getFont();
image.setFont(new Font("Courier New", Font.BOLD, 22));
image.drawString("" + songNames, 12, 22);
setImage(image);
selected = true;
Greenfoot.playSound(soundFiles);
}
if(selected && !Greenfoot.mouseClicked(this)) {
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(colors);
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(Color.BLACK);
image.drawRect(0, 0, WIDTH-1, HEIGHT-1);
image.setColor(Color.BLACK);
Font font = image.getFont();
image.setFont(new Font("Courier New", Font.PLAIN, 22));
image.drawString("" + songNames, 12, 22);
setImage(image);
selected = true;
Greenfoot.playSound(soundFiles);
selected = false;
}
}
public boolean isSelected()
{
return selected;
}
private void makeImage()
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(colors);
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(Color.BLACK);
image.drawRect(0, 0, WIDTH-1, HEIGHT-1);
image.setColor(Color.BLACK);
Font font = image.getFont();
image.setFont(new Font("Courier New", Font.PLAIN, 22));
image.drawString("" + songNames, 12, 22);
setImage(image);
}
}
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
public class PlayerWorld extends World
{
private PlayMusicButton[] q = new PlayMusicButton[5];
private static String[] soundFiles = {"Drive Me Home.mp3", "Get It On.mp3", "Hair.mp3", "Marry The Night.mp3", "Open Up Wide.mp3"};
private static String[] songNames = {"Drive Me Home", "Get It On", "Hair", "Marry The Night", "Open Up Wide"};
private static Color[] colors = {Color.yellow, Color.white, Color.lightGray, Color.pink, Color.cyan};
public boolean isPlaying;
public boolean isPlaying()
{
return playing;
}
public PlayerWorld()
{
super(600, 400, 1);
addObject(new StopButton(), 480, 200);
for(int i = 0; i < 5; i++)
{
addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
}
playing = false;
}
public void refresh()
{
q.refresh();
for(int i = 0; i < 5; i++)
{
addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
}
if(isPlaying != null)
{
sound.stop();
return;
}
}
}
