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

2016/11/29

Referencing a non active world.

MJBelmer MJBelmer

2016/11/29

#
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:
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 MyWorld
Code for Intro World:
import 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 World
Code for Play Button:
import 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
Nosson1459 Nosson1459

2016/11/29

#
you can have another world then right-click on it and what says "new WorldName()"
MJBelmer MJBelmer

2016/11/29

#
I just had a huge mental breakthrough and found a solution for my problem and grasped the concept of passing worlds and holding them as variables in objects I create so I can call methods of the world through the objects.
Nosson1459 Nosson1459

2016/11/30

#
So is there any problems here to be answered?
MJBelmer MJBelmer

2016/11/30

#
Nope I figured out what I was missing to reference another world. I think I need to understand how Greenfoot knows what world to initialize first but for now things work okay. Thank you Nosson
Nosson1459 Nosson1459

2016/11/30

#
The world which is initialized first, is the one that you right-click on, then in that right-click drop menu press "new Intro()", which is the first thing on that list, also the only thing written in black. That could be the menu world and when you click on the menu button in that world you do
Greenfoot.setWorld(new DragonArena());
and it will switch to game world.
You need to login to post a reply.