Hi,
I have a welcomeScreen world that starts playing the game song and a gameOver world when the player dies.
this is my Code..
welcomeScreen:
gameOver:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class welcomeScreen extends World { boolean canPlay = true; static GreenfootSound music = new GreenfootSound("music.mp3"); /** * Constructor for objects of class welcomeScreen. * */ public welcomeScreen() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(900, 500, 1); //addObject(new titleOverlay() , 477, 88); addObject(new buttonStart(), 450, 338); } public void act() { music = new GreenfootSound("music.mp3"); if (canPlay) { music.play(); canPlay = false; } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class gameOver extends World { boolean played = false; int timer = 0; int H, W; GreenfootSound gameOver = new GreenfootSound("gameover.mp3"); welcomeScreen welcomeScreen = new welcomeScreen(); public gameOver() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(900, 500, 1); addObject(new gameOverText(), 459,82); gameOver.play(); } public void act() { timer++; H = getBackground().getHeight(); W = getBackground().getWidth(); scaleImage(); if (welcomeScreen.music.isPlaying() ) { welcomeScreen.music.stop(); } if (!gameOver.isPlaying() ) { Greenfoot.stop(); } } public void scaleImage() { } }