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

2015/4/6

Making Sound Stop From Other Worlds.

NightScythe NightScythe

2015/4/6

#
I'm working on a game program for school and there are multiple levels. I know how to make sound stop before it changes to a different world but I need to be able to change it after because I want the music between the first three levels to be continuous but the Boss level needs to change sound and I don't want the normal level sound playing over it. Does anyone have any advice?
Super_Hippo Super_Hippo

2015/4/6

#
It would be much easier to tell you what to do if you would post the relevant code which does not behave as you wish.
NightScythe NightScythe

2015/4/6

#
import greenfoot.*; import java.util.*; /** * Write a description of class Level1 here. * * @author (your name) * @version (a version number or a date) */ public class Level1 extends World { public boolean hearts = false; public boolean gameOvered = false; public GreenfootSound theme; public GreenfootSound gOver; public boolean playTheme = true; public boolean playing = false; public Level1() { super(480, 300, 1); populate(); } public void act() { levelClear(); gameOver(); if(!playing) music(); } public void populate() { Actor ball = new Fighter1(); Actor ground = new Ground(); Actor ground2 = new Ground(); Actor ground3 = new Ground(); Actor ground4 = new Ground(); Actor ground5 = new Ground(); Actor ground6 = new Ground(); Actor ground7 = new Ground(); Actor ground8 = new Ground(); Actor skel1 = new Skeleton1(); Actor skel2 = new Skeleton1(); Actor skel3 = new Skeleton1(); addObject(ground,30,getHeight()-10); addObject(ground2,90,getHeight()-10); addObject(ground3,150,getHeight()-10); addObject(ground4,210,getHeight()-10); addObject(ground5,270,getHeight()-10); addObject(ground6,330,getHeight()-10); addObject(ground7,390,getHeight()-10); addObject(ground8,450,getHeight()-10); addObject(ball,10,(getHeight()*2)/3); addObject(skel1,getWidth()-10,(getHeight()*2)/3); addObject(skel2,(getWidth()*1)/3,(getHeight()*2)/3); addObject(skel3,(getWidth()*2)/3,(getHeight()*2)/3); addObject(new Health1(),40,15); } public Fighter1 getFighter1() { Fighter1 f = (Fighter1)getObjects(Fighter1.class).get(0); return f; } public int things() { int actors = numberOfObjects(); return actors; } public void levelClear() { if(things() == 10 && !hearts) { Actor heart1 = new Heart(); Actor heart2 = new Heart(); Actor heart3 = new Heart(); addObject(heart1,getWidth()-20,(getHeight()*1)/3); addObject(heart2,(getWidth())/2,(getHeight()*1)/3); addObject(heart3,20,(getHeight()*1)/3); hearts = true; } } public boolean checkGameOver() { Health1 h = (Health1)getObjects(Health1.class).get(0); return h.gameOver; } public void gameOver() { if(checkGameOver() == true && !gameOvered) { stopMusic(); gOver = new GreenfootSound("GameOver.mp3"); Actor gO = new GameOver(); Actor retry = new Retry(); Actor exit = new Exit(); addObject(gO,getWidth()/2,50); addObject(retry,getWidth()/2,100); addObject(exit,getWidth()/2,150); gOver.play(); gameOvered = true; } } public void music() { theme = new GreenfootSound("HyruleCastleTheme.mp3"); if(playTheme) { theme.play(); playing = true; } } public void stopMusic() { theme.stop(); playTheme = false; } } *This is the Code for the first Level that starts the Music.
NightScythe NightScythe

2015/4/6

#
import greenfoot.*; /** * Write a description of class BossWorld here. * * @author (your name) * @version (a version number or a date) */ public class BossWorld extends World { public boolean gameOvered = false; public GreenfootSound bossTheme; public boolean playTheme = true; public boolean playing = false; public BossWorld() { super(480, 300, 1); populate(); stopLevelOneMusic(); } public void act() { gameOver(); if(!playing) music(); } public void populate() { Actor ball = new FighterBoss(); Actor ground = new Ground(); Actor ground2 = new Ground(); Actor ground3 = new Ground(); Actor ground4 = new Ground(); Actor ground5 = new Ground(); Actor ground6 = new Ground(); Actor ground7 = new Ground(); Actor ground8 = new Ground(); Actor darkLink = new DarkLink(); addObject(ground,30,getHeight()-10); addObject(ground2,90,getHeight()-10); addObject(ground3,150,getHeight()-10); addObject(ground4,210,getHeight()-10); addObject(ground5,270,getHeight()-10); addObject(ground6,330,getHeight()-10); addObject(ground7,390,getHeight()-10); addObject(ground8,450,getHeight()-10); addObject(ball,10,(getHeight()*2)/3); addObject(darkLink,getWidth()-10,(getHeight()*2)/3); addObject(new HealthBoss(),40,15); addObject(new DarkHealth(),getWidth()-20,15); } public FighterBoss getFighterBoss() { FighterBoss f = (FighterBoss)getObjects(FighterBoss.class).get(0); return f; } public DarkLink getDarkLink() { DarkLink dl = (DarkLink)getObjects(DarkLink.class).get(0); return dl; } public boolean checkGameOver() { HealthBoss h = (HealthBoss)getObjects(HealthBoss.class).get(0); return h.gameOver; } public void gameOver() { if(checkGameOver() == true && !gameOvered) { Actor gO = new GameOver(); Actor retry = new Retry(); Actor exit = new Exit(); addObject(gO,getWidth()/2,50); addObject(retry,getWidth()/2,100); addObject(exit,getWidth()/2,150); gameOvered = true; } } public void music() { bossTheme = new GreenfootSound("FinalBoss.mp3"); if(playTheme) { bossTheme.play(); playing = true; } } public void stopLevelOneMusic() { World l1 = new Level1(); l1.theme.stop(); } } *This is the Code of the Boss Level that I want to stop the music.
NightScythe NightScythe

2015/4/6

#
The music booleans are toward the top and the methods are toward the bottom.
danpost danpost

2015/4/7

#
Your 'stopLevelOneMusic' method in the BossWorld class will never work. The first line in the method creates a NEW Level1 world and is a totally different object (with its own music fields and sound objects) than the world that was previously active. You either need to pass a reference to the previously active world to the BossWorld world so it can stop its music OR (and this would be much easier and efficient) turn it off when you create and set the new BossWorld active (which I would presume is in your Fighter1 class).
NightScythe NightScythe

2015/4/7

#
The only problem is that there are two levels in between the first and boss levels.
danpost danpost

2015/4/7

#
NightScythe wrote...
The only problem is that there are two levels in between the first and boss levels.
Then as my final suggestion, I recommend that you change the sound file fields to class constants instead of instance fields:
// change these lines in you Level1 class
public GreenfootSound theme;
public GreenfootSound gOver;
// to these
public static final GreenfootSound theme = new GreenfootSound("HyruleCastleTheme.mp3");
public static final GreenfootSound gOver = new GreenfootSound("GameOver.mp3");
Then, remove line 3 in the 'music' method and line 6 in the 'gameOver' method from the Level1 class code. Now, you can use:
Level1.theme.stop();
from anywhere (regardless of the active world) to stop the music. And you can remove all those methods for controlling the music and just start and stop the music from where you call those methods from.
NightScythe NightScythe

2015/4/7

#
Thank You! It works perfectly now.
You need to login to post a reply.