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

2021/2/24

stop background music when switching world

Genota Genota

2021/2/24

#
When I start my game, the background starts to play. If I hit an enemy I go the world "GameOver" with a special game over sound playing, but my background music doesnt stop. I am not sure what to need to get it so here is my Title_Screen:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Title_Screen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Title_Screen extends World
{

    /**
     * Constructor for objects of class Title_Screen.
     * 
     */
    private GreenfootSound music = new GreenfootSound("background_music.mp3");
    public Title_Screen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1400, 884, 1);
        setBackground(new GreenfootImage("TITLE SCREEN.png"));
        addObject(new Plattform(),500,845);
        addObject(new CopyOfPlayer(),400,740);
        addObject(new Coin(),990,735);
        addObject(new Coin(),1045,680);
        addObject(new Coin(),1100,625);
        addObject(new Coin(),1155,570);
        addObject(new Coin(),1210,515);
    }
    
   public void act()
   {
       if(Greenfoot.isKeyDown("Enter"))
        {Greenfoot.setWorld(new MyWorld());
        }
   }
    public void stopped()
   {
        music.stop();
   }
   public void started()
   {
        music.playLoop();
   }
}
Player:
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int vSpeed = 0;
    private int accel = 1;
    private int speed = 5;
    private int jumpHeight= -20;
    private boolean jumping = false;
    private int attacking = -1;
    private int animationSpeed = 2;
    private int shotDelay;
    boolean touchingEnemy1 = false;
    private GreenfootSound music = new GreenfootSound("background_music.mp3");
    private GreenfootSound sound = new GreenfootSound("GameOver_sound.mp3");
    
    private GreenfootImage run1 = new GreenfootImage("Glumanda1right1.png");
    private GreenfootImage run2 = new GreenfootImage("Glumanda2right1.png");
    private GreenfootImage run3 = new GreenfootImage("Glumanda3right1.png");
    private GreenfootImage run4 = new GreenfootImage("Glumanda1right2.png");
    private GreenfootImage run5 = new GreenfootImage("Glumanda2right2.png");
    private GreenfootImage run6 = new GreenfootImage("Glumanda3right2.png");
    private GreenfootImage run7 = new GreenfootImage("Glumanda1left1.png");
    private GreenfootImage run8 = new GreenfootImage("Glumanda2left1.png");
    private GreenfootImage run9 = new GreenfootImage("Glumanda3left1.png");
    private GreenfootImage run10 = new GreenfootImage("Glumanda1left2.png");
    private GreenfootImage run11 = new GreenfootImage("Glumanda2left2.png");
    private GreenfootImage run12 = new GreenfootImage("Glumanda3left2.png");
    private GreenfootImage run13 = new GreenfootImage("Glumandamouth1.png");
    private GreenfootImage run14 = new GreenfootImage("Glumandamouth2.png");
    private GreenfootImage run15 = new GreenfootImage("Glumandamouth3.png");
    private GreenfootImage run16 = new GreenfootImage("Glumandamouth4.png");
    private GreenfootImage run17 = new GreenfootImage("Glumandamouth1left.png");
    private GreenfootImage run18 = new GreenfootImage("Glumandamouth2left.png");
    private GreenfootImage run19 = new GreenfootImage("Glumandamouth3left.png");
    private GreenfootImage run20 = new GreenfootImage("Glumandamouth4left.png");
    private GreenfootSound sound1 = new GreenfootSound("Bump_sound.mp3");
    private GreenfootSound sound2 = new GreenfootSound("fire_sound.mp3");
    private int frame = 3;
    private int animationCounter = 0;
    GifImage myGif = new GifImage ("Gif_PS.gif");
    GifImage myGif2 = new GifImage ("Gif_PSleft.gif");
    private int moveToGround;
    private int direction = 1;
    
    public void act() 
    {
        checkFalling();
        fall();
        jump();
        moveAround();
        animationCounter ++;
        fireProjectile();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        hitEnemy();
    }  
    public void moveAround()
    {
      if(Greenfoot.isKeyDown("d"))
      
      {
          setLocation(getX() + speed, getY());
          direction = 1;
          if(animationCounter % 8 == 0)
           animateRight();
      }
      if(Greenfoot.isKeyDown("a"))
       {
         setLocation(getX() - speed, getY());
         direction = -1;
         if(animationCounter % 8 == 0)
         animateLeft();
      }
    }
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
    }
    public void checkFalling()
    {
        if (!isTouching(Ground.class))
        {
            vSpeed++;
        }
        else 
            vSpeed = 0;
        
    }
    public void jump()
    {
        if(Greenfoot.isKeyDown("space")&& (onGround()==true))
        {
            vSpeed = jumpHeight;
            fall();
        }
    }
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight / 2 + 5);
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
        if(ground == null)
        {
         return false;   
        }
        else
        {
         moveToGround(ground);
         return true;
        }
    }
    public void moveRight()
    {
        setLocation (getX()+speed, getY());
    }
    public void animateRight()
    {
        if (frame == 1)
        {
            setImage(run1);
        }
        else if(frame ==2)
        {
            setImage(run2);
        }
        else if(frame ==3)
        {
            setImage(run3);
        }
        else if(frame ==4)
        {
            setImage(run4);
        }
        else if(frame ==5)
        {
            setImage(run5);
        }
        else if(frame ==6)
        {
            setImage(run6);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    public void animateLeft()
    {
        if (frame == 1)
        {
            setImage(run7);
        }
        else if(frame ==2)
        {
            setImage(run8);
        }
        else if(frame ==3)
        {
            setImage(run9);
        }
        else if(frame ==4)
        {
            setImage(run10);
        }
        else if(frame ==5)
        {
            setImage(run11);
        }
        else if(frame ==6)
        {
            setImage(run12);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    public void fireProjectile()
    {
        if (shotDelay > 0) shotDelay--; 
        if (shotDelay == 0 && Greenfoot.mousePressed(null) && direction ==1)
        {           
            shotDelay = 30;
            Projectile projectile = new Projectile();
            getWorld().addObject(projectile, getX(), getY());
            projectile.turnTowards(18000,0);
            projectile.move(110.200);
            setImage(myGif.getCurrentImage());
            sound1.play();
        }
        if (shotDelay == 0 && Greenfoot.mousePressed(null) && direction ==-1)
        {           
            shotDelay = 30;
            Projectile projectile = new Projectile();
            getWorld().addObject(projectile, getX(), getY());
            projectile.turnTowards(-22000,0);
            projectile.move(100.1800);
            setImage(myGif2.getCurrentImage());
            sound1.play();
        }
    }
    public void animatemouthright()
    {
        if (frame == 1)
        {
            setImage(run13);
        }
        else if(frame ==2)
        {
            setImage(run14);
        }
        else if(frame ==3)
        {
            setImage(run15);
        }
        else if(frame ==4)
        {
            setImage(run16);
            frame = 1;
            return;
        }
        
         frame ++;
    }
    public void animatemouthleft()
    {
        if (frame == 1)
        {
            setImage(run17);
        }
        else if(frame ==2)
        {
            setImage(run18);
        }
        else if(frame ==3)
        {
            setImage(run19);
        }
        else if(frame ==4)
        {
            setImage(run20);
            frame = 1;
            return;
        }
    
         frame ++;
    }
     boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight / -2);
        
        Actor ceiling = getOneObjectAtOffset( 0, yDistance, Ground.class);
        if (ceiling != null)
        {
            vSpeed =1;
            bopHead(ceiling);
            return true;
        }
        else
        {
          return false;
        }
    }
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);
        
        Actor rightWall = getOneObjectAtOffset (xDistance, 0, Ground.class);
        
        if(rightWall ==null)
        {
         return false;   
        }
        else
        {
          stopByRightWall(rightWall);
          return true;
        }
    }
    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/-2);
        
        Actor leftWall = getOneObjectAtOffset (xDistance, 0, Ground.class);
        
        if(leftWall ==null)
        {
         return false;   
        }
        else
        {
          stopByLeftWall(leftWall);
          return true;
        }
    }
    public void stopByRightWall(Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() -(wallWidth + getImage().getWidth())/2;
        setLocation(newX -5, getY());
    }
    public void stopByLeftWall(Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() -(wallWidth + getImage().getWidth())/-2;
        setLocation(newX -5, getY());
    }
    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        sound1.play();
    }
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    public void hitEnemy()
    {
        Actor Enemy =getOneIntersectingObject(Enemy.class);
        if(Enemy != null)
        {
          World myWorld = getWorld();
          Greenfoot.setWorld(new GameOver());
        }
    }
    
}
 
MyWorld(The world where the Player hits the enemy):
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
    
    /**
     * Write a description of class MyWorld here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class MyWorld extends World
    {
        public ImgScroll scroller; 
        private Player player; 
        HealthBar healthbar = new HealthBar();
        private GreenfootSound music = new GreenfootSound("background_music.mp3");
        
        /**
         * Constructor for objects of class MyWorld.
         * 
         */
        
        
        
       
        public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1400, 884, 1, false); 
        setBackground(new GreenfootImage("Hintergrund.png"));
        
        addObject(new Quadrat3(),377,533);
        addObject(new side_right(),432,532);
        addObject(new Box(),31,484);
        addObject(new Box(),95,484);
        addObject(new Box(),31,418);
        addObject(new Back1(),175,685);
        addObject(new Door_closed(),84,742);
        addObject(new Mast(),274,684);
        addObject(new Torch_Stick(),184,685);
        addObject(new Torch(),185,646);
        addObject(new Fass(),23,354);
        addObject(new Plattform2(),198,529);
        addObject(new Plattform2(),158,529);
        addObject(new Plattform(),0,845);
        addObject(new Plattform(),300,845);
        addObject(new Plattform(),600,845);
        addObject(new Plattform(),900,845);
        addObject(new Plattform(),1200,845);
        addObject(new Plattform(),1500,845);
        addObject(new Plattform(),1800,845);
        addObject(new Plattform(),2100,845);
        addObject(new Plattform(),2400,845);
        addObject(new Plattform(),2700,845);
        addObject(new Plattform(),3000,845);
        addObject(new Plattform(),3300,845);
        addObject(new Plattform(),3600,845);
        addObject(new Plattform(),3900,845);
        addObject(new Plattform(),4200,845);
        addObject(new Plattform(),4500,845);
        addObject(new Plattform(),4800,845);
        addObject(new Plattform(),5100,845);
        addObject(new Plattform(),5400,845);
        addObject(new Plattform(),5700,845);
        addObject(new Plattform(),6000,845);
        
        addObject(new Enemy1(),1000,750);
        addObject(player = new Player(), 70, 750);
        scroller = new ImgScroll(this, new GreenfootImage("Hintergrund.png"), 6000, 884);
        for (int j=0; j<scroller.getScrollHeight()-100; j+=300)
            for (int i=0; i<scroller.getScrollWidth()-200; i+=300) 

        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
    public void act ()
    {
        scroller.scroll(getWidth()/2-player.getX(), getHeight()/2-player.getY());
    }
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
}
 
GameOver :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class GameOver extends World
{
    private GreenfootSound sound = new GreenfootSound("GameOver_sound.mp3");
    /**
     * Constructor for objects of class GameOver.
     * 
     */
    public GameOver()
    {    
        super(1400, 884, 1); 
        setBackground(new GreenfootImage("Game_Over.png"));
        music();
    }
    public void music()
    {
        sound.play();
    }
}
Super_Hippo Super_Hippo

2021/2/24

#
To stop a sound, you need a reference to it. After you changed to the new world in line 34 of the Title_Screen class, you won’t have any further chance to get a reference to the sound object from the newly created world, so it will run forever. A few alternatives you could go for: 1) stop the sound before changing the world 2) pass a reference when you create the new world 3) make the sound a class variable (static) instead of an object variable to access it from anywhere more easily
You need to login to post a reply.