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

2013/7/12

i am trying to stop my music from my 1st world with my 5th world. what am i doing wrong??

1
2
qsapp.3 qsapp.3

2013/7/12

#
 public Level5(){    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        Cover.backgroundMusic.stop();
        prepare();
    }//end constructor
qsapp.3 qsapp.3

2013/7/12

#
It gives me the error "non-static variable backgroundMusic cannot be referenced from a static context"
backgroundMusic is not static. It is a variable. Remove "Cover." an it should work fine. Unless you want it to work like that, then you need to make backgroundMusic in the class Cover a static variable (just put it before the name, e.g "private static backgroundMusic = ...")
SPower SPower

2013/7/12

#
Well, you will need the object of your first world (called 'Cover', right?) in order to change it. I would suggest somethign like this:
public Level5(Cover world1)
{
    // other stuff
    world1.stopBackgroundMusic();
    // other stuff
}
where 'stopBackgroundMusic()' is a new method you have to create in your first world, which would look like this:
public void stopBackgroundMusic()
{
    backgroundMusic.stop();
}
But all this will need you to create the Level5 world on your own, so can you show us the code where you do that?
qsapp.3 qsapp.3

2013/7/12

#
This is my main player class. i keep getting an error
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Main player. Gets controlled by user.
 * 
 * @author (Quinn Sapp) 
 * @version (11 July 2013)
 */
public class MainPlayer extends Animal
{
    private Counter counter;
    public MainPlayer(Counter pointCounter){
        counter = pointCounter;
    }// end of Counter
    public void act(){
        checkKeyPress();
        lookForPoint();
        lookForFinish();
        gameOver();
        checkNextLevel();
        checkNextLevel2();
        checkNextLevel3();
        checkNextLevel4();
    }// end act
    public void checkKeyPress(){
        if (Greenfoot.isKeyDown("left")){
            turn(-4);
        }//end if
        if (Greenfoot.isKeyDown("right")){
            turn(4);
        }//end if
        if (Greenfoot.isKeyDown("space")){
            move(2);
        }//end if
    }//end checkKeys
    public void lookForPoint(){
       if (canSee(Point.class)){
        eat(Point.class);
        counter.add(1);
        Greenfoot.playSound("slurp.wav");
       }//end if
       if(counter.getValue() >= 35){
       }//end if
    }  //end lookForPoint
    public void lookForFinish(){
       if (canSee(Finish.class)){
        eat(Point.class);
        counter.add(1);
        Greenfoot.playSound("slurp.wav");
       }//end if
       if(counter.getValue() >= 35){
       }//end if
    }  //end lookForFinish
    public void gameOver(){
        if (counter.getValue() >= 15)
        Greenfoot.setWorld(new Level2());
    }//end gameOver
    public void checkNextLevel(){   
       if (getOneIntersectingObject(Finish.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new MainWorld());
       }//end if
    }//end checkNextLevel
    public void checkNextLevel2(){   
       if (getOneIntersectingObject(Finish2.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new Level3());
       }//end if
    }//end checkNextLevel2
    public void checkNextLevel3(){   
       if (getOneIntersectingObject(Finish3.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new Level4());
       }//end if  
    }//end checkNextLevel3
    public void checkNextLevel4(){   
       if (getOneIntersectingObject(Finish4.class) != null) {  
         //reached the end object;
         //start the new level;  
         Greenfoot.setWorld(new Level5());// this is where the error is i believe i have to put something in the () but idk what, can you please help?
       }//end if  
    }//end checkNextLevel4
}//end MainPlayer
qsapp.3 qsapp.3

2013/7/12

#
This is my Level5
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is level 5 of 5 you need to complete in order to win the Game.
 * This is a bonus level, to just have fun collect as many points as you want because by completing 
 * the first 4 levels you have won the game.
 * @author (Quinn Sapp) 
 * @version (11 July 2013)
 */
public class Level5 extends World
{
    /**
     * Constructor for objects of class MainWorld.
     * 
     */
    public Level5(Cover cover){    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        cover.stopBackgroundMusic();
        prepare();
    }//end constructor
qsapp.3 qsapp.3

2013/7/12

#
This is my Cover class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This entales the instructions, objectives of the game and the START to begin the game.
 * 
 * @author (Quinn Sapp) 
 * @version (11 July 2013)
 */
public class Cover extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("Track 02.wav");
    /**
     * Constructor for objects of class Cover.
     * 
     */
    public Cover()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        backgroundMusic.playLoop(); 
        prepare();
    }
    public void stopBackgroundMusic(){
        backgroundMusic.stop();
    }
danpost danpost

2013/7/13

#
It appears that you are NOT passing the Cover world along from world to world. That is, line 83 in the MainPlayer class shows you creating a new Level5 world without using any arguments. Since it appears you are creating a new MainPlayer object at each level, you cannot just save a reference to the Cover world in the MainPlayer object. This means you will have to keep a reference to the Cover world (or at least the GreenfootSound object 'backgroundMusic') in each level (even if you have the field in a superclass that encompasses all your level worlds) and pass the reference from one world to the next. . I would recommend just passing 'backgroundMusic' along from world to world; as keeping a reference to the whole Cover world would keep an unnecessary amount of resources unavailable; and when there are images involved (like the background image of the Cover world), it becomes extensive.
qsapp.3 qsapp.3

2013/7/13

#
can i have an example please i am comfused.
Gevater_Tod4711 Gevater_Tod4711

2013/7/13

#
To give a reference to your sound object through all of your world classes you need to change the constructor of all world classes. But it would be better not to use the reference to the cover but to your sound object I think, because you don't need anything else from your cover class. So you should change the constructors of all classes like you did in your Level5 class whith the exception that you should use Level5(GreenfootSound bgMusic) for all constructors. Then you need to save the reference in your worldclass. After doing this your class should look like this:
public class Level5 extends World  {

    private GreenfootSound bgMusic;

    public Level5(GreenfootSound bgMusic){
        super(600, 400, 1);
        this.bgMusic = bgMusic;//save the GreenfootSound object in this world;
        if (bgMusic != null) {
            bgMusic.stop();//only execute in Level5;
        }
        prepare();  
    }

    public GreenfootSound getBgSound() {
        return bgSound;
    }
}
Now you just need to change your MainPlayer class because the new Worlds now have to have parameters (Greenfoot.setWorld(...)). Therefore you need to change the checkNextLevel methods in your MainPlayer class like this:
public void checkNextLevel(){     
    if (getOneIntersectingObject(Finish.class) != null) {    
        //reached the end object;  
        //start the new level;    
        
        //get the bgSound object;
        GreenfootSound bgSound = null;
        World world = getWorld();
        if (world instanceof MainWorld) {
            bgSound = ((MainWorld) world).getBgSound();
        }
        else if (world instanceof Level2) {
            bgSound = ((Level2) world).getBgSound();
        }
        else if (world instanceof Level3) {
            bgSound = ((Level3) world).getBgSound();
        }
        else if (world instanceof Level4) {
            bgSound = ((Level4) world).getBgSound();
        }
        else if (world instanceof Level5) {
            bgSound = ((Level5) world).getBgSound();
        }
        
        Greenfoot.setWorld(new MainWorld(bgSound));  
        }//end if  
    }//end checkNextLevel  
If you change all checkNextLevel methods like this method it should work.
qsapp.3 qsapp.3

2013/7/13

#
now its asking what the bgSound etc anything with "bg" is how do i fix that?
Gevater_Tod4711 Gevater_Tod4711

2013/7/13

#
Where is this problem occouring?
qsapp.3 qsapp.3

2013/7/13

#
the getBgSound, and the return bg sound
qsapp.3 qsapp.3

2013/7/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Main player. Gets controlled by user.
 * 
 * @author (Quinn Sapp) 
 * @version (11 July 2013)
 */
public class MainPlayer extends Animal
{
    private Counter counter;
    public MainPlayer(Counter pointCounter){
        counter = pointCounter;
    }// end of Counter
    public void act(){
        checkKeyPress();
        lookForPoint();
        lookForFinish();
        gameOver();
        checkNextLevel();
    }// end act
    public void checkKeyPress(){
        if (Greenfoot.isKeyDown("left")){
            turn(-4);
        }//end if
        if (Greenfoot.isKeyDown("right")){
            turn(4);
        }//end if
        if (Greenfoot.isKeyDown("space")){
            move(2);
        }//end if
    }//end checkKeys
    public void lookForPoint(){
       if (canSee(Point.class)){
        eat(Point.class);
        counter.add(1);
        Greenfoot.playSound("slurp.wav");
       }//end if
       if(counter.getValue() >= 35){
       }//end if
    }  //end lookForPoint
    public void lookForFinish(){
       if (canSee(Finish.class)){
        eat(Point.class);
        counter.add(1);
        Greenfoot.playSound("slurp.wav");
       }//end if
       if(counter.getValue() >= 35){
       }//end if
    }  //end lookForFinish
    public void gameOver(){
        if (counter.getValue() >= 15)
        Greenfoot.setWorld(new Level2());
    }//end gameOver    
    public void checkNextLevel(){       
        if (getOneIntersectingObject(Finish.class) != null) {       
        GreenfootSound bgSound = null;  
        World world = getWorld();  
        if (world instanceof MainWorld) {  
            bgSound = ((MainWorld) world).getBgSound();  
        }  
        else if (world instanceof Level2) {  
            bgSound = ((Level2) world).getBgSound();  
        }  
        else if (world instanceof Level3) {  
            bgSound = ((Level3) world).getBgSound();  
        }  
        else if (world instanceof Level4) {  
            bgSound = ((Level4) world).getBgSound();  
        }  
        else if (world instanceof Level5) {  
            bgSound = ((Level5) world).getBgSound();  
        }  
          
        Greenfoot.setWorld(new MainWorld(bgSound));    
        }//end if    
    }//end checkNextLevel 
}//end MainPlayer
qsapp.3 qsapp.3

2013/7/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is level 5 of 5 you need to complete in order to win the Game.
 * This is a bonus level, to just have fun collect as many points as you want because by completing 
 * the first 4 levels you have won the game.
 * @author (Quinn Sapp) 
 * @version (11 July 2013)
 */
public class Level5 extends World{
    private GreenfootSound bgMusic;
    /**
     * Constructor for objects of class MainWorld.
     * 
     */
    public Level5(GreenfootSound bgMusic){    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        this.bgMusic = bgMusic;
        if(bgMusic != null){
            bgMusic.stop();
        }
        prepare();
    }//end constructor
    public GreenfootSound getBgSound(){
        return bgSound;
    }
There are more replies on the next page.
1
2