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

2016/5/11

How do i stop a game over music when i clicked retry button ?

LonelyGrey LonelyGrey

2016/5/11

#
I'm having trouble in stopping my game over music. It won't stop when I clicked retry button. This is the code.
import greenfoot.*;

public class Dead extends World
{
    GreenfootSound lose = new GreenfootSound("gameover.mp3");

    public Dead()
    {    
        super(750, 600, 1); 
        objek();
        lose.playLoop();
        if (Greenfoot.mouseClicked(Retry.class))
        {
            Greenfoot.setWorld(new Level1());
            lose.stop();
        }
    }
    public void objek()
    {
        addObject(new Retry(), 375, 383);
    }
}
SPower SPower

2016/5/11

#
I don't think this code even compiles. The mouseClicked method takes an object as its parameter, not a class. In order to keep track of the retry button, you'll have to create an instance variable:
private Retry retryButton = new Retry();
Then, in your constructor, add it to the world:
addObject(retryButton, 375, 383);
Then for the checking for clicks. Right now, you have the code in your constructor, which runs only once, when the world is created. In order to have some code executed with each act cycle, you have to overwrite the act method in the world:
public void act()
{
    if (Greenfoot.mouseClicked(retryButton)) {
        // do stuff
    }
}
You need to login to post a reply.