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

2017/3/29

Help Next Level

salvo salvo

2017/3/29

#
Hey everyone: basically i want that if the score reach 3 the Game get to the next Level here is my code for my Counter
public class Counter1 extends Actor
{
    int score = 0;
    /**
     * Erstellt den Counter für Rakete 1
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
    }
    /**
     * Addiert 1 auf den Score
     */
    public void addScore()
    {
        score++; 
    }
    public void SpaceWelt2()
    {
        if (score == 3)
        
        {
            ((SpaceWelt)getWorld()).SpaceWelt2();
        } 
    }
}
here is the code from my Space
public class SpaceWelt extends World
{
    Counter1 counter1 = new Counter1();
    Counter2 counter2 = new Counter2();
    /**
     * Erstelle eine neue Welt mit 101x101 Zellen.
     * Jede Zelle hat die Größe von 6x6 Pixel.
     * Die Welt beinhaltet zwei Raketen und zwei Felsen (Metoriten) und Bubbles..
     * Die Bubbles haben andere Koordinaten als die Raketen.
     */
    public SpaceWelt() 
    {   
        super(101, 101, 6);        
        setBackground("space1.jpg");
        
        Rocket1 rocket1= new Rocket1(); 
        int x1 = Greenfoot.getRandomNumber(101);
        int y1 = Greenfoot.getRandomNumber(101);
        addObject(rocket1,5,50);
        
        Rocket2 rocket2 = new Rocket2();
        int x2 = Greenfoot.getRandomNumber (101);
        int y2 = Greenfoot.getRandomNumber (101);
        addObject(rocket2,95,50);
        
        Fels Fels = new Fels();
        addObject(Fels,30,50);
        
        Fels2 Fels2 = new Fels2();
        addObject(Fels2,70,50);
        
        Fels3 Fels3 = new Fels3();
        addObject(Fels3,50,50);
        
        addObject(counter1, 10, 4);
        addObject(counter2, 90, 4);
        }
    
    public void SpaceWelt2()
    {      
        setBackground("space1.jpg");
        
        removeObjects(getObjects(Rocket1.class));
        removeObjects(getObjects(Rocket2.class));
        removeObjects(getObjects(Fels.class));
        removeObjects(getObjects(Fels2.class));
        removeObjects(getObjects(Fels3.class));
        
        Rocket1 rocket1= new Rocket1(); 
        int x1 = Greenfoot.getRandomNumber(101);
        int y1 = Greenfoot.getRandomNumber(101);
        addObject(rocket1,5,50);
        
        Rocket2 rocket2 = new Rocket2();
        int x2 = Greenfoot.getRandomNumber (101);
        int y2 = Greenfoot.getRandomNumber (101);
        addObject(rocket2,95,50);
        
        Fels Fels = new Fels();
        addObject(Fels,30,50);
        
        Fels2 Fels2 = new Fels2();
        addObject(Fels2,70,50);
        
        Fels3 Fels3 = new Fels3();
        addObject(Fels3,50,50);
        
        Fels4 Fels4 = new Fels4();
        addObject (Fels4, 50,70);
        
        Fels5 Fels5 = new Fels5();
        addObject (Fels5, 50, 30);
        
        addObject(counter1, 10, 4);
        addObject(counter2, 90, 4);
    
    }

     /**
     * Die Welt meldet an Counter 1, dass sich ein Parameter ändert 
     */
    public  Counter1 getCounter1()
        {
            return counter1; 
        }
    /**
     * Die Welt meldet an Counter 1, dass sich ein Parameter ändert 
     */
    public  Counter2 getCounter2()
        {
            return counter2;
        }
    }
Fifilein Fifilein

2017/3/29

#
You can try to change line 23
 //from
((SpaceWelt)getWorld()).SpaceWelt2();
//to
Greenfoot.setWorld(new SpaceWelt2());
  
salvo salvo

2017/3/29

#
ok i tried too but it says that Greenfoot can not find class SpaceWelt2
Fifilein Fifilein

2017/3/29

#
salvo wrote...
but it says that Greenfoot can not find class SpaceWelt2
Ok try to change it to the Worldclass name you want to set.
salvo salvo

2017/3/29

#
nothing happens ...
danpost danpost

2017/3/29

#
You need to call the 'SpaceWelt2' method for it to work. From your originally posted code, add the following line after the 'counter++;' line in the Counter1 class:
SpaceWelt2();
or just remove lines 17 through 19.
salvo salvo

2017/3/29

#
i really don't know where the problem is nothing happens if counter 1 gets to 3
salvo salvo

2017/3/29

#
ok i changed the void in boolean and add a if now it works thanks guys
danpost danpost

2017/3/29

#
salvo wrote...
ok i changed the void in boolean and add a if now it works thanks guys
You should probably show what you did to fix it so that other that run into a similar problem can see what you did.
salvo salvo

2017/3/29

#
public class Counter1 extends Actor
{
    int score = 0;
    /**
     * Erstellt den Counter für Rakete 1
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
        if(Score3())
        {
            Greenfoot.setWorld(new SpaceWelt2());
        }
    }
    /**
     * Addiert 1 auf den Score
     */
    public void addScore()
    {
        score++; 
    }
    public boolean Score3()
    {
        if (score == 3)
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
}
danpost danpost

2017/3/29

#
salvo wrote...
< Code Omitted >
Couple of things about this code. The image only needs to change when first create and when the score changes -- it does not need to be reset every single act cycle. Similarly with checking or proceeding to the next level -- that only needs done when the score changes. If you are going to continue to use this counter from level to level, then you will run into issues because when the score is 3, once you add it into the next world the will say -- hey, the score is three, let's set another new level active, again (and again). The other thing is the 'Score3' method which returns whether 'score == 3'. This can be asked direction from the calling statement (instead of calling another method to do that. You could essentially call a method to call another method to call another method each returning the same bolean value. This is wasteful, and in essence what you are doing here. A better class coding follows:
import greenfoot.*;

public class Counter1 extends Actor
{
    int score = 0;
    
    public Counter1()
    {
        updateImage(); // set initial image
    }

    public void addScore()
    {
        score++; 
        updateImage(); // score changed, reset image
        if (score == 3) // check for level change
        {
            Greenfoot.setWorld(new SpaceWelt2());
        }
    }
    
    private void updateImage()
    {
        setImage(new GreenfootImage("Score: "+score, 24, Color.WHITE, Color.BLACK));
    }
}
However, checking for level changing is really not something a counter should do. It should probably be something your SpaceWelt class should do.
salvo salvo

2017/3/30

#
ok thanks Dan
You need to login to post a reply.