I feel as though I might have some crossed wires in how this get setup which may be my underlying problem here but I feel I should still be able to accomplish my task. After creating most of my game, including adding music, I decided to make an intro screen with separate music and a button to click to play and switch back to the game world. I couldn't figure out how Greenfoot decides which world is the primary world to load first so I told my main game world to switch to this intro world straight off (it still played the game music so I switched it to the intro music and added the variable and methods for playing and stopping both songs) and proceeded to make a Play button that could be clicked, would stop the intro music, start the game music and set the world back to the main game world. I thought I wrote everything correctly but it seems since DragonArena is not active it can't be referenced? Yet the music doesn't stop when the worlds switch. Help? It's saying I can't cast Intro to DragonArena
Code for DragonArena:
Code for Intro World:
Code for Play Button:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* DragonArena is a two player game of fire-breathing dragons.
*
* @author (Mitch Belmer)
* @version (.01)
*/
public class DragonArena extends World
{
private int redScore;
private int blueScore;
private Dragon redDragon, blueDragon;
private int totalPoints;
private GreenfootSound music = new GreenfootSound("intro.mp3");
private GreenfootSound bgm = new GreenfootSound("bgm.mp3");
/**
* Constructor for objects of class DragonArena.
*
*/
public DragonArena()
{
// Create a new world with 768x768 cells with a cell size of 1x1 pixels.
super(768, 768, 1);
prepare();
redScore = 0;
blueScore = 0;
showScore();
}//end DragonArena
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Greenfoot.setWorld(new Intro());
getInput();
for (int i = 0; i < Greenfoot.getRandomNumber(4) + 1; i++)
{
addObject(new Wall(), Greenfoot.getRandomNumber(550) + 100, Greenfoot.getRandomNumber(550) + 100);
}
redDragon = new Dragon("RedDragon.png", "w", "s", "a", "d", "space");
redDragon.turn(-90);
addObject(redDragon,634,627);
blueDragon = new Dragon("BlueDragon.png", "up", "down", "left", "right", "0");
blueDragon.turn(90);
addObject(blueDragon, 107, 184);
music.playLoop();
}//end prepare
/**
* Show Red and Blue Player score
*/
public void showScore()
{
showText("Red Player: " + redScore, 80, 25);
showText("Blue Player: " + blueScore, 675,25);
}//end showScore
/**
* Accessor methods for each of the player objects
*/
public Dragon getRedDragon() { return redDragon; }
public Dragon getBlueDragon() { return blueDragon; }
public GreenfootSound getMusic() { return music; }
/**
* roundOver method allows for the world to start a new round
*/
public void roundOver(Dragon dragon)
{
Greenfoot.playSound("roar.wav");
if (dragon == redDragon) {
//redDragon was hit so increase blue dragon score
blueScore++;
Greenfoot.setWorld(new PointMessage(this, blueDragon));
if (blueScore == totalPoints)
{
showText("Blue Wins", 380, 380);
stopBGM();
}//end if
}//end if
else {
redScore++;
Greenfoot.setWorld(new PointMessage(this, redDragon));
if (redScore == totalPoints)
{
showText("Red Wins", 380, 380);
stopBGM();
}//end if
}//end else
showScore();
locReset();
remFire();
}//end roundOver
/**
* Reset Dragons to start
*/
public void locReset()
{
redDragon.setLocation(634,627);
blueDragon.setLocation(107,184);
}//end locReset
/**
* Search for Fireballs and remove them
*/
public void remFire()
{
removeObjects(getObjects(Fireball.class));
redDragon.fireReset();
blueDragon.fireReset();
}//end remFire
/**
* Get stating values from the players
*/
private void getInput()
{
String input = Greenfoot.ask("How many points for match?");
totalPoints = Integer.parseInt(input);
}//end getInput
/**
* Stop music
*/
public void stopMusic()
{
music.stop();
}//end stopMusic
/**
* Start bgm
*/
public void playBGM()
{
bgm.playLoop();
}//end playBGM
/**
* Stop bgm
*/
public void stopBGM()
{
bgm.stop();
}//end stopBGM
}//end MyWorldimport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Intro here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Intro extends World
{
private DragonArena dragonArena;
/**
* Constructor for objects of class Intro.
*
*/
public Intro()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
addObject(new Play("button.png", "buttonClick.png"), 416, 436);
}//end Intro
}//end Intro Worldimport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Play here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Play extends Actor
{
private String up;
private String down;
private DragonArena dragonArena;
/**
* Create a new Play button
*/
public Play(String upImage, String downImage)
{
up = upImage;
down = downImage;
setImage(up);
}//end Play
/**
* Act - do whatever the Play wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
userClick();
}//end act
/**
* Run game when button is clicked
*/
public void userClick()
{
if (Greenfoot.mousePressed(this))
{
setImage(down);
}//end if
if (Greenfoot.mouseClicked(this))
{
((DragonArena)getWorld()).stopMusic();
((DragonArena)getWorld()).playBGM();
}//end if
}//end userClick
}//end Play Actor
