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

2014/6/30

Stop playing music

Svek Svek

2014/6/30

#
Hey Im making a space invaders look alike and i need to know how u let music stop playing this is my code:
public class SpaceWorld extends World
{
    private Human thePlayer;
    private SpaceShip theEnemy;
    private Counter ScoreCounter;
    private GreenfootSound music = new GreenfootSound("Zelda Main Theme Song.mp3");

    public SpaceWorld()
    {    
        super(1180, 670, 1); 

        thePlayer = new Human();
        theEnemy = new SpaceShip();
        Spawn();
        HumanSpawn();
        music.playLoop();
        ScoreCounter = new Counter("Score");
        addObject(ScoreCounter, 60, 30);
I made an private GreenfoorMusic and made the music play in a loop but if you click on restart the music plays 2times please help?!
danpost danpost

2014/6/30

#
You can add (override) a 'public void stopped' method to your world class code that has a 'music.stop();' or 'music.pause();' command in it. Refer to the World class API for info on this method. You can also add (override) a 'public void started' method to call 'music.playLoop();' and restart the music when the scenario is started again.
Svek Svek

2014/6/30

#
can u give an example of how the code should look like?
danpost danpost

2014/6/30

#
Svek wrote...
can u give an example of how the code should look like?
public void started()
{
    music.playLoop();
}

public void stopped()
{
    music.stop();
}
Svek Svek

2014/6/30

#
But if i put the public void stopped() in my world and i try to call music.stop(); in an other sprite Greenfoot sais cannot find music - variable music?
danpost danpost

2014/6/30

#
In the sprite class, use:
((SpaceWorld)getWorld()).music.stop();
Svek Svek

2014/6/30

#
if i do that it sais music has private acces in spaceworld
danpost danpost

2014/6/30

#
Svek wrote...
if i do that it sais music has private acces in spaceworld
Make it 'public'. Or, use:
((SpaceWorld)getWorld()).stopped();
instead.
Svek Svek

2014/6/30

#
I made it public and it works fine if i lose but if i win the music just keeps on going and gives an error:
{
    private boolean left = false;
    public int hitpoints;
    public int speed;
    public int jump;
    /**
     * Checks to see if the enemy is shot, if so then the enemy is removed from
     * the world
     */
    public void act() {
        if (getX() - getImage().getWidth() / 2 <= 1  ||
        getX() + getImage().getWidth() / 2 >= getWorld().getWidth()-1)
        {
            left = !left;
            setLocation( getX() , getY() +jump);
        }

        if(left)
        {
            move(-speed);
        }
        else {
            move(speed);
        }

        if(isTouching(Human.class)){
            removeTouching(Human.class);
            Greenfoot.setWorld(new Invaded());  
            ((SpaceWorld)getWorld()).music.stop(); 
        }

        if(hitpoints <= 0)
        {
            getWorld().removeObject(this);
        }

        if (hitpoints <= 0 && this instanceof SpaceShip){ 
            Greenfoot.setWorld(new Winning());
            ((SpaceWorld)getWorld()).music.stop();

        }
    }
}
so this peace isn't working?
 if (hitpoints <= 0 && this instanceof SpaceShip){ 
            Greenfoot.setWorld(new Winning());
            ((SpaceWorld)getWorld()).music.stop();
danpost danpost

2014/6/30

#
Replace lines 32 through 41 with this:
if (hitpoints <= 0)
{
    if (this instanceof SpaceShip)
    {
        ((SpaceWorld)getWorld()).music.stop();
        Greenfoot.setWorld(new Winning());
    }
    getWorld().removeObject(this);
}
Svek Svek

2014/6/30

#
Thanks man, u helped me alot this week. Really apreciating what ur doing keep up the good work
You need to login to post a reply.