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

2015/5/13

multilevel counter.

possumprior possumprior

2015/5/13

#
hello i am creating a game for my BTEC course and i am having trouble getting the counter to work through multiple levels although it does work on Level 1 of the game. if need be i can upload my code but there are 6 worlds (5 levels and 1 start screen) and 12 Actor classes including the mover class. please help me ASAP as this is coursework.
azazeel azazeel

2015/5/13

#
please read this first, especially this part to maintain counter you can use public static modifier
danpost danpost

2015/5/13

#
If using 'public static' fields, be sure to initialize their values from your start screen world constructor as you will not want their values to be maintained during resetting of the project.
possumprior possumprior

2015/5/14

#
sorry I didn't read that before I posted
possumprior possumprior

2015/5/14

#
i'm also not sure how to use the public static modifier.would it help if i put up my start screen,level 1 and 2 code as well as my counter code?
danpost danpost

2015/5/14

#
The Counter class code is not needed. What is needed is the class that creates a Counter object and saves it in a field ('static', or otherwise).
possumprior possumprior

2015/5/14

#
okay so the level 1 code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This is the whole scene. It creates and contains the objects that are in it.
 */
public class MazeL1 extends World
{
    Counter counter = new Counter();
    int needed = 12;
    public MazeL1(){    
        super(600,600, 1);    // define size and resolution
        prepare();
    }
    private void prepare(){
        EdgeWalls();
        MazeHwalls();
        MazeVwalls();
        man();
        bomb();
        gold();
        addCounter();
    }
    private void EdgeWalls(){
        //top edge walls
        for (int h=0;h<needed;h++){
          EdgeHwall edgehwall = new EdgeHwall();
          addObject(edgehwall,25+(h*50),1);
        }
        //bottom edge walls
        for (int h=0;h<needed;h++){
          EdgeHwall edgehwall = new EdgeHwall();
          addObject(edgehwall,25+(h*50),599);
        }
        //right edge walls
        for (int v=0;v<needed;v++){
          EdgeVwall edgevwall = new EdgeVwall();
          addObject(edgevwall,1,25+(v*50));
        }
        //left edge walls 
        for (int v=0;v<needed;v++){
          EdgeVwall edgevwall = new EdgeVwall();
          addObject(edgevwall,599,25+(v*50));
        }
    }
    private void MazeHwalls(){
        //middle of the objects position//
        //walls for the maze
    }
    private void MazeVwalls(){
        //middle of the objects position//
        //walls for the maze
    }
    private void man(){
       //placement of the man actor at start of the game
    }
    private void bomb(){
       //placement of the bomb actor at start of the game
    }
    private void gold(){
        //placement of the gold coins 
        Gold gold = new Gold();
        addObject(gold,300,300);
    } 
    private void addCounter(){
        addObject(counter, 100,40);                
    }
    public Counter getCounter()
    {
        return counter;
    }
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    { 
        GameOver gameover = new GameOver();
        addObject(gameover,300,300);
    }
}
possumprior possumprior

2015/5/14

#
level 2:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This is the whole scene. It creates and contains the objects that are in it.
 */
public class MazeL2 extends World
{
    Counter counter = new Counter();
    int needed = 12;
    public MazeL2(){    
        super(600,600, 1);    // define size and resolution
        prepare();
    }
    private void prepare(){
        EdgeWalls();
        MazeHwalls();
        MazeVwalls();
        man();
        bomb();
        gold();
        addCounter();
    }
    private void EdgeWalls(){
        //top edge walls
        for (int h=0;h<needed;h++){
          EdgeHwall edgehwall = new EdgeHwall();
          addObject(edgehwall,25+(h*50),1);
        }
        //bottom edge walls
        for (int h=0;h<needed;h++){
          EdgeHwall edgehwall = new EdgeHwall();
          addObject(edgehwall,25+(h*50),599);
        }
        //right edge walls
        for (int v=0;v<needed;v++){
          EdgeVwall edgevwall = new EdgeVwall();
          addObject(edgevwall,1,25+(v*50));
        }
        //left edge walls 
        for (int v=0;v<needed;v++){
          EdgeVwall edgevwall = new EdgeVwall();
          addObject(edgevwall,599,25+(v*50));
        }
    }
    private void MazeHwalls(){
        //middle of the objects position//
        //walls for the maze
    }
    private void MazeVwalls(){
        //middle of the objects position//
        //walls for the maze
    }
    private void man(){
       //placement of the man actor at start of the game
    }
    private void bomb(){
       //placement of the bomb actor at start of the game
    }
    private void gold(){
        //placement of the gold coins                
    } 
    public void addCounter(){
        Counter counter = new Counter();
        addObject(counter, 100,40); 
        counter = ((MazeL1)getWorld()).getCounter();
    }
    public Counter getCounter()
    {
        return counter;
    }
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    { 
        GameOver gameover = new GameOver();
        addObject(gameover,300,300);
    }
}
Super_Hippo Super_Hippo

2015/5/14

#
counter = ((MazeL1)getWorld()).getCounter();
'getWorld' can only be used by an Actor. It returns the world in which the actor currently is. A world isn't in another world, so you can't use this method in a world subclass.
danpost danpost

2015/5/14

#
In your MazeL1 class, change line 8 to the following:
private static Counter counter;
If you assign the value in that line, it will not reset when you start a new MazeL1 world (during reset). Then replace lines 64 through 70 with the following:
private void addCounter()
{
    counter = new Counter();
    addObject(counter, 100, 40);
}

public static Counter getCounter()
{
    return counter;
}
Line 3, in the private 'addCounter' method creates a new Counter object and assigns it to the 'counter' field. Line 4, places the counter in the MazeL1 world. The 'getCounter' method (called a 'getter' method because of what it does) is used by other classes to acquire the Counter object. Being the field was made static (given to the class instead of to the MazeL1 world created from the class), the getter method was also made static. You do not need a world object to call the method -- just use the class name (as you will see ahead). In the MazeL2 class, remove line 8. The counter is referenced in the MazeL1 class. Then replace lines 62 through 70 with the following:
private void addCounter()
{
    addObject(MazeL1.getCounter(), 100, 40);
}
Anywhere in your project that you need to do something with the counter, you can:
// A) get a reference to it with
Counter counter = MazeL1.getCounter();
// and then perform your action (for example)
counter.add(10);

// or B) do the action directly with (for example)
MazeL1.getCounter().add(10);
possumprior possumprior

2015/5/15

#
Thank you so much danpost what u said worked
You need to login to post a reply.