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

2018/4/17

Keeping scores between different worlds

Yomi Yomi

2018/4/17

#
In the first level of my game, the player can gain and lose hp, and collect coins; they start with 3 hp and 0 coins. If theres any changes to the scores (e.g. player picked up a coin, so the coin counter now equals 1), then they go to the next level, the scores get reset to their first values (so the coin counter then goes back to 0 rather than keeping the 1 for the next level). Is there a way I can keep these scores between multiple worlds? I use 3 actors for the score counting process, here is their code; Player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int x = getX();
        int y = getY();
        
        
        
        if (Greenfoot.isKeyDown("a"))
        {
            setLocation(x-1,y);
            if (getOneIntersectingObject(Wall.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(half_wall_side.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(half_wall_up.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(wall_10x1.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(wall_11x1.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(wall_1x7.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_wall.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_half_wall_side.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_half_wall_up.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_wall_10x1.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_wall_11x1.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_wall_1x7.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null) 
            setLocation (getX()+1,getY());
            if (getOneIntersectingObject(blue_half_wall_1x7.class) != null) 
            setLocation (getX()+1,getY());
        }
        if (Greenfoot.isKeyDown("d"))
        {
            setLocation(x+1,y);
            if (getOneIntersectingObject(Wall.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(half_wall_side.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(half_wall_up.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(wall_10x1.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(wall_11x1.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(wall_1x7.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_wall.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_half_wall_side.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_half_wall_up.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_wall_10x1.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_wall_11x1.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_wall_1x7.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null) 
            setLocation (getX()-1,getY());
            if (getOneIntersectingObject(blue_half_wall_1x7.class) != null) 
            setLocation (getX()-1,getY());
        }
        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(x,y-1);
            if (getOneIntersectingObject(Wall.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(half_wall_side.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(half_wall_up.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(wall_10x1.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(wall_11x1.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(wall_1x7.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_wall.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_half_wall_side.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_half_wall_up.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_wall_10x1.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_wall_11x1.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_wall_1x7.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null) 
            setLocation (getX(),getY()+1);
            if (getOneIntersectingObject(blue_half_wall_1x7.class) != null) 
            setLocation (getX(),getY()+1);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(x,y+1);
            if (getOneIntersectingObject(Wall.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(half_wall_side.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(half_wall_up.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(wall_10x1.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(wall_11x1.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(wall_1x7.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_wall.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_half_wall_side.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_half_wall_up.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_wall_10x1.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_wall_11x1.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_wall_1x7.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null) 
            setLocation (getX(),getY()-1);
            if (getOneIntersectingObject(blue_half_wall_1x7.class) != null) 
            setLocation (getX(),getY()-1);
        }
        
        Actor Medicine;
        Medicine = getOneObjectAtOffset(0, 0, Medicine.class);
        if (Medicine != null)
        {
            World world;
            hit_med();
            world = getWorld();
            world.removeObject(Medicine);
            //add 1 to hp
        }
        
        Actor Drug;
        Drug = getOneObjectAtOffset(0, 0, Drug.class);
        if (Drug != null)
        {
            World world;
            hit_drug();
            world = getWorld();
            world.removeObject(Drug);
            //remove 1 from hp
        }
        
        
        
        Actor Coin;
        Coin = getOneObjectAtOffset(0, 0, Coin.class);
        if (Coin != null)
        {
            World world;
            hit_coin();
            world = getWorld();
            world.removeObject(Coin);
            //add 1 to currency
        }
        
        
        
        Actor Scene_Changer_L1_1;
        Scene_Changer_L1_1 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_1.class);
        if (Scene_Changer_L1_1 != null)
        {
            Greenfoot.setWorld(new Level_1_2());
        }
        
        Actor Scene_Changer_L1_2;
        Scene_Changer_L1_2 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_2.class);
        if (Scene_Changer_L1_2 != null)
        {
            Greenfoot.setWorld(new Level_1_3());
        }
        
        Actor Scene_Changer_L1_3;
        Scene_Changer_L1_3 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_3.class);
        if (Scene_Changer_L1_3 != null)
        {
            Greenfoot.setWorld(new Level_1_4());
        }
        
        Actor Scene_Changer_L1_4;
        Scene_Changer_L1_4 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_4.class);
        if (Scene_Changer_L1_4 != null)
        {
            Greenfoot.setWorld(new Level_2_1());
        }
        
        Actor Scene_Changer_L2_1;
        Scene_Changer_L2_1 = getOneObjectAtOffset(0, 0, Scene_Changer_L2_1.class);
        if (Scene_Changer_L2_1 != null)
        {
            Greenfoot.setWorld(new Level_2_2());
        }
        
        
    }    
    
    private void hit_coin()
    {
            test_room testRoom = (test_room) getWorld();
            coin_counter counter = testRoom.getCounter();
            counter.bumpCount(1);
    }
    
    private void hit_med()
    {
            test_room testRoom = (test_room) getWorld();
            hp counter = testRoom.getHpCounter();
            counter.bumpCount(1);
    }
    
    private void hit_drug()
    {
            test_room testRoom = (test_room) getWorld();
            hp counter = testRoom.getHpCounter();
            counter.reduceCount(1);
    }
    
}
hp:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class hp here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class hp extends Actor
{
    private int totalCount = 3;
    
    

    public void Counter()
    {
        setImage(new GreenfootImage("HP: 3", 20, Color.WHITE, Color.BLACK));
    }

   
    
    public void bumpCount(int amount)
    {
        totalCount += 1;
        setImage(new GreenfootImage("HP: " + totalCount, 20, Color.WHITE, Color.BLACK));
    }
    
    public void reduceCount(int amount)
    {
        totalCount -= 1;
        setImage(new GreenfootImage("HP: " + totalCount, 20, Color.WHITE, Color.BLACK));
    }
    
    
}
 

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

/**
 * Write a description of class coin_counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class coin_counter extends Actor
{
    

    private int totalCount = 0;
    
    

    public void Counter()
    {
        setImage(new GreenfootImage("Coins: 0", 20, Color.WHITE, Color.BLACK));
    }

   
    
    public void bumpCount(int amount)
    {
        totalCount += 1;
        setImage(new GreenfootImage("Coins: " + totalCount, 20, Color.WHITE, Color.BLACK));
    }  
    
    
}
danpost danpost

2018/4/17

#
Right now, the value of totalCount is not accessible outside the hp class. So, you need a "getter" method for it:
// add in both the hp class and the coin_counter class
public int getCount()
{
    return totalCount;
}
Next, even though you are passing values to the bumpCount and reduceCount methods, the code is only incrementing or decrementing the count by one (ignoring the value passed). Change the "1" in both methods (if present) in both classes to "amount" (the name of the value passed). Now, I will show for one of the world changers. You will have to incorporate it into the others:
Actor Scene_Changer_L1_1;
Scene_Changer_L1_1 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_1.class);
if (Scene_Changer_L1_1 != null)
{
    Level_1_2 level = new Level_1_2();
    level.getHpCounter().bumpCount(((Level_1_1)getWorld()).getHpCounter().getCount());
    level.getCounter().bumpCount(((Level_1_1)getWorld()).getCounter().getCount());
    Greenfoot.setWorld(level);
}
Yomi Yomi

2018/4/17

#
Worked perfectly, thank you for the help. :)
You need to login to post a reply.