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

2015/4/11

Score transfere to next Level

moksha92 moksha92

2015/4/11

#
Hi Guys, i got my game finished but the score from World 1 doesn't transfere to World2. It just goes back to 0 again. How do i write the code in my Subworld class to transfere the score? Thanks! Here the Codes: Subworld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class subworld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class subworld extends World
{
    Counter counter = new Counter("Score: ");
    public subworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 
        setPaintOrder(ScoreBoard.class,ScoreBoard2.class, hase.class, pilz.class, dog.class, Counter.class);
        prepare();
    }
    private int counterspawn;
    private int colorTimer = -1;
    private void prepare()
    {
        hase hase = new hase();
        addObject(hase, 275, 250);
        dog dog = new dog();
        addObject(dog, 603,365);
        dog.setLocation(823,515);
        hase.setLocation(252,222);
        addObject(counter, 100, 560);
    }
    public void act()
    {
        runcounterspawn();
        if(colorTimer>0)
        {   
            colorTimer--;
            if (colorTimer==0)
            {   
                setBackground("grass.png");
                colorTimer--;
            }
        }
    }
    public void counteat()
    {
     counter.add(10);  
            
    }
    public void changeColor()
    {
        colorTimer=150;
        setBackground("mushroombunt.png");
    }
    private void runcounterspawn()
    {
        counterspawn = (counterspawn+1)%300;
        if (counterspawn == 0) spawnPilz();
    }
    private void spawnPilz()
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new pilz(), x, y);
    }
    public void gameOver()
    {
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.playSound("zonk.mp3");
        Greenfoot.stop();
    }
    public void changeLevel()
    {
     Greenfoot.setWorld(new world2());
    }
    public void nextlevel()
    {
        Greenfoot.playSound("clap.mp3");
        changeLevel();
    }   
    public void win()
    {
        addObject(new ScoreBoard2(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.playSound("clap.mp3");
        Greenfoot.stop();
    }
    
}
world2:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * Constructor for objects of class world2.
     * 
     */
    public world2()
    {    
      
        prepare();
    }
    
    private void prepare()
    {
        addObject(counter, 100, 560);
        rock rock = new rock();
        addObject(rock, 793, 93);
        rock rock2 = new rock();
        addObject(rock2, 802, 514);
        rock rock3 = new rock();
        addObject(rock3, 101, 511);
        rock rock4 = new rock();
        addObject(rock4, 104, 97);
        rock4.setLocation(101, 90);
        rock rock5 = new rock();
        addObject(rock5, 449, 94);
        rock rock6 = new rock();
        addObject(rock6, 449, 515);
        rock rock7 = new rock();
        addObject(rock7, 450, 326);
        rock7.setLocation(450, 322);
        rock rock8 = new rock();
        addObject(rock8, 801, 324);
        rock8.setLocation(797, 318);
        rock rock9 = new rock();
        addObject(rock9, 104, 325);
        rock9.setLocation(100, 321);
        dog dog = new dog();
        addObject(dog, 603,365);
        dog.setLocation(823,515);
    }
}
world1 is empty my actor:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class hase here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class hase extends Actor
{
    public int pilzEaten = 0;
    public void act() 
    {
        {if(Greenfoot.isKeyDown("up"))
            {move(3);
             if (!getIntersectingObjects(rock.class).isEmpty()) move(-4);}}
         {if(Greenfoot.isKeyDown("down"))
             {move(-3);
              if (!getIntersectingObjects(rock.class).isEmpty()) move(+4);}}
         {if(Greenfoot.isKeyDown("left"))
             {turn(-3);}}
         {if(Greenfoot.isKeyDown("right"))
             {turn(3);}}
          eat();
    }    
    public void eat()
    {Actor pilz;
        pilz = getOneObjectAtOffset(0,0,pilz.class);
        if(pilz !=null)
        {
            getWorld().removeObject(pilz);
            Greenfoot.playSound("burp.mp3");
            ((subworld) getWorld()).changeColor();
            ((subworld) getWorld()).counteat();
            pilzEaten++;
            if (pilzEaten==3)
            {
                if (getWorld() instanceof world2)
                {
                 ((world2) getWorld()).win();
                }
                else if(getWorld() instanceof world1)
                {
                 ((world1) getWorld()).nextlevel();
                }
            }
       }
    }
}
and the counter:
import greenfoot.*;  
import java.awt.Font;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;

    public Counter()
    {
        this("");
    }

    public Counter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 16;

        setImage(new GreenfootImage(stringLength, 24));
        GreenfootImage image = getImage();
        Font font = image.getFont();
        image.setFont(font.deriveFont(24.0F));
        
        updateImage();
    }
    
    public void act() {
        if(value < target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
    }

    public void add(int score)
    {
        target += score;
    }

    public void subtract(int score)
    {
        target -= score;
    }

    public int getValue()
    {
        return value;
    }
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 18);
    }
}
danpost danpost

2015/4/11

#
In the subworld class, change the 'changeLevel' method to this:
public void changeLevel()
{
    Counter c1 = (Counter) getObjects(Counter.class).get(0); // current counter
    World w2 = new world2(); // new world
    Counter c2 = (Counter) w2.getObjects(Counter.class).get(0); // new counter
    c2.add(c1.getValue()); // transfer score
    Greenfoot.setWorld(w2); // activate new world
}
danpost danpost

2015/4/11

#
You may want to add the following method to the Counter class:
public void setValue(int val)
{
    value = val;
    target = val;
    updateImage();
}
and change line 6 in my previous code post to this:
c2.setValue(c1.getValue());
so the counter does not animate the score to get to its current value.
moksha92 moksha92

2015/4/19

#
Thanks for the help! Works fine. One more question... If my actor now eats 10 Mushrooms then the Level changes and the score is transfered. The Problem is, that the counter only counts 9 eaten mushrooms and forgets the 10th one. So the score is 90 and not 100 in the new level. How do i fix this? ;)
danpost danpost

2015/4/19

#
Change line 46 in you subworld class to this:
counter.setValue(counter.getValue()+10);
You need to login to post a reply.