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

2016/11/26

How to stop music in actor when greenfoot stops and when world changes

Henkbot Henkbot

2016/11/26

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Music here. * * @author (your name) * @version (a version number or a date) */ public class Music extends Actor { GreenfootSound inf = new GreenfootSound("bm.mp3"); GreenfootSound boss = new GreenfootSound(""); GreenfootSound start = new GreenfootSound(""); GreenfootSound end = new GreenfootSound(""); boolean firstTurn = true; private int music = 0; /** * Act - do whatever the Music wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (getWorld() instanceof Minionsworld) { if (music == 1) { inf.playLoop(); } if(Greenfoot.mouseClicked(this)) { if (inf.isPlaying()) { inf.pause(); setImage("play.png"); } else { inf.playLoop(); setImage("pause.png"); } } } if (music < 2) { music++; } } }
Super_Hippo Super_Hippo

2016/11/26

#
The world has a method which is automatically called when the scenario changes from the running to the pause state, the 'stopped' method. If you save a reference to the added music object, you can then pause the music. When you change the world, you again need a reference to this music object and pause/stop the music.
danpost danpost

2016/11/26

#
You might make use of my BgMusic class which is located in my BgMusic Actor Class Demo scenario. The class is designed to control the background music regardless of the active world. The use of the 'stopped' method is still required when the scenario changes from the running to the paused state (and the 'started' method when going the other way); but, no reference is needed to the BgMusic object because of the way the class was designed. All your World subclasses will need implementations of both the 'started' and 'stopped' methods if all bases are to be covered (a superclass could be utilized to have the implementation applied to all its sub-worlds).
You need to login to post a reply.