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

2019/7/15

Carry over score in between levels

AnimeRules AnimeRules

2019/7/15

#
Hi so I am a beginner coder and I am making a flappy bird game. I created levels and you move levels if you reach a certain score. I have it set so that you are taken to an in between level page and then when you press shift you move to the actual next level. The problem is that when you move levels the counter resets and that causes you to keep restarting the second level. Please Help! MiddleInvisble(the thing that makes the score go up)
public class MiddleInvisible extends Actor
{
    //sets resizing variables
    int lineScaleDown;
    int  acrossScaleDown;
    //beginning score
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public MiddleInvisible()
    {
        //scales the image
        scaleDownImage(30,1);
    }

    public void TouchPipe()
    {
        //adds a score if you touch the pipes
        if (isTouching(Pipes.class))
        {
            score = score + 1;
            //switches worlds
            if (score == 440) Greenfoot.setWorld(new MoveLevel2());
            if (score == 1760) Greenfoot.setWorld(new MoveLevel3());
        }  
    }

    public void act() 
    {               

    }  

    public void scaleDownImage(int x, int y)
    {
        //makes the method to resize the pipes
        lineScaleDown = x;
        acrossScaleDown = y;
        getImage().scale(getImage().getWidth()/lineScaleDown, getImage().getHeight()/acrossScaleDown);

    }   
}
Counter
public class Counter extends MiddleInvisible
{
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //sets the colors for the counter
        setImage(new GreenfootImage("Score : " + score/40, 45, Color.WHITE, Color.PINK));
        TouchPipe();
    }  

}
MoveLevel2(the in-between level)
public class MoveLevel2 extends World
{

    /**
     * Constructor for objects of class MoveLevel2.
     * 
     */
    public MoveLevel2()
    {    
        super(800, 600, 1);         
    }
    
        public void act()
    {
         //when enter is pressed the screen will switch to the game
        if(Greenfoot.isKeyDown("shift")) 
        Greenfoot.setWorld(new FlappyWorld2());
    }
}
FlappyWorld2(Level 2)
//sets variables
public class FlappyWorld2 extends World
{
    /**
     * Constructor for objects of class FlappyWorld.
     * 
     */
    // the beginning count 
    int count = 0;
    private Counter cnt;
    public FlappyWorld2()
    {    
        // resizes the world
        super(800, 600, 1, false); 
        prepare();
    }

    public void act()
    {
        //adds one to the count
        count = count + 1;
        //calls the methods created
        addPipes();
    }

    public void addPipes()
    {
        if(count % 85 == 0)
        {
            //adds a new pipe at the edge of the screen
            int randomNum = Greenfoot.getRandomNumber(6);
            addObject(new BottomPipe(), getWidth()-1, 350 + 50 * randomNum);
            addObject(new TopPipe(), getWidth()-1, -200 + 50 * randomNum);
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        //makes what you go through
        MiddleInvisible mi = new MiddleInvisible();
        addObject(mi, 75,60);
        MiddleInvisible mi1 = new MiddleInvisible();
        addObject(mi1, 75,100);
        MiddleInvisible mi2 = new MiddleInvisible();
        addObject(mi2, 75,130);
        MiddleInvisible mi3 = new MiddleInvisible();
        addObject(mi3, 75,170);
        MiddleInvisible mi4 = new MiddleInvisible();
        addObject(mi4, 75,200);
        MiddleInvisible mi5 = new MiddleInvisible();
        addObject(mi5, 75,230);
        MiddleInvisible mi6 = new MiddleInvisible();
        addObject(mi6, 75,270);
        MiddleInvisible mi7 = new MiddleInvisible();
        addObject(mi7, 75,300);
        MiddleInvisible mi8 = new MiddleInvisible();
        addObject(mi8, 75,330);
        MiddleInvisible mi9 = new MiddleInvisible();
        addObject(mi9, 75,370);
        MiddleInvisible mi10 = new MiddleInvisible();
        addObject(mi10, 75,400);
        MiddleInvisible mi11 = new MiddleInvisible();
        addObject(mi11, 75,430);
        MiddleInvisible mi12 = new MiddleInvisible();
        addObject(mi12, 75,470);
        MiddleInvisible mi13 = new MiddleInvisible();
        addObject(mi13, 75,500);
        MiddleInvisible mi14 = new MiddleInvisible();
        addObject(mi14, 75,530);
        MiddleInvisible mi15 = new MiddleInvisible();
        addObject(mi15, 75,570);
        //draw the ground
        Ground ground = new Ground();
        addObject(ground,296,575);
        Ground ground2 = new Ground();
        addObject(ground2,702,575);
        //draws the counter
        Counter counter = new Counter();
        addObject(counter, 100, 30);
        //draws the flappybird
        FlappyBird flappyBird = new FlappyBird();
        addObject(flappyBird,76,169);
    }
}
Please Help!
AnimeRules AnimeRules

2019/7/15

#
I figured out how to make the counter not reset in level 2 and that works fine but moving to level 3 isn't working. counter hasn't changed MiddleInvisible hasn't changed FlappyWorld
//sets variables
public class FlappyWorld extends World
{
    /**
     * Constructor for objects of class FlappyWorld.
     * 
     */
    // the beginning count 
    int count = 0;
    private static Counter cnt;
    public FlappyWorld()
    {    
        // resizes the world
        super(800, 600, 1, false); 
        prepare();
        cnt= new Counter();
        addObject(cnt, 100, 30);
    }

    public void act()
    {
        //adds one to the count
        count = count + 1;
        //calls the methods created
        addPipes();
    }

    public void addPipes()
    {
        if(count % 100 == 0)
        {
            //adds a new pipe at the edge of the screen
            int randomNum = Greenfoot.getRandomNumber(6);
            addObject(new BottomPipe(), getWidth()-1, 350 + 50 * randomNum);
            addObject(new TopPipe(), getWidth()-1, -200 + 50 * randomNum);
        }
    }
    
    public static Counter getCount(){
        return cnt;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        //makes what you go through
        MiddleInvisible mi = new MiddleInvisible();
        addObject(mi, 75,60);
        MiddleInvisible mi1 = new MiddleInvisible();
        addObject(mi1, 75,100);
        MiddleInvisible mi2 = new MiddleInvisible();
        addObject(mi2, 75,130);
        MiddleInvisible mi3 = new MiddleInvisible();
        addObject(mi3, 75,170);
        MiddleInvisible mi4 = new MiddleInvisible();
        addObject(mi4, 75,200);
        MiddleInvisible mi5 = new MiddleInvisible();
        addObject(mi5, 75,230);
        MiddleInvisible mi6 = new MiddleInvisible();
        addObject(mi6, 75,270);
        MiddleInvisible mi7 = new MiddleInvisible();
        addObject(mi7, 75,300);
        MiddleInvisible mi8 = new MiddleInvisible();
        addObject(mi8, 75,330);
        MiddleInvisible mi9 = new MiddleInvisible();
        addObject(mi9, 75,370);
        MiddleInvisible mi10 = new MiddleInvisible();
        addObject(mi10, 75,400);
        MiddleInvisible mi11 = new MiddleInvisible();
        addObject(mi11, 75,430);
        MiddleInvisible mi12 = new MiddleInvisible();
        addObject(mi12, 75,470);
        MiddleInvisible mi13 = new MiddleInvisible();
        addObject(mi13, 75,500);
        MiddleInvisible mi14 = new MiddleInvisible();
        addObject(mi14, 75,530);
        MiddleInvisible mi15 = new MiddleInvisible();
        addObject(mi15, 75,570);
        //draw the ground
        Ground ground = new Ground();
        addObject(ground,296,575);
        Ground ground2 = new Ground();
        addObject(ground2,702,575);

        //draws the flappybird
        FlappyBird flappyBird = new FlappyBird();
        addObject(flappyBird,76,169);
    }
}
FlappyWorld2(level 2)
//sets variables
public class FlappyWorld2 extends World
{
    /**
     * Constructor for objects of class FlappyWorld.
     * 
     */
    // the beginning count 
    int count = 0;
    private Counter scoro;  
    public FlappyWorld2(Counter scorp)
    {    
        // resizes the world
        super(800, 600, 1, false);
        this.scoro=scoro;
        addObject(scoro, 100, 30);
        prepare();
    }

    public void act()
    {
        //adds one to the count
        count = count + 1;
        //calls the methods created
        addPipes();
    }

    public void addPipes()
    {
        if(count % 85 == 0)
        {
            //adds a new pipe at the edge of the screen
            int randomNum = Greenfoot.getRandomNumber(6);
            addObject(new BottomPipe(), getWidth()-1, 350 + 50 * randomNum);
            addObject(new TopPipe(), getWidth()-1, -200 + 50 * randomNum);
        }
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        //makes what you go through
        MiddleInvisible mi = new MiddleInvisible();
        addObject(mi, 75,60);
        MiddleInvisible mi1 = new MiddleInvisible();
        addObject(mi1, 75,100);
        MiddleInvisible mi2 = new MiddleInvisible();
        addObject(mi2, 75,130);
        MiddleInvisible mi3 = new MiddleInvisible();
        addObject(mi3, 75,170);
        MiddleInvisible mi4 = new MiddleInvisible();
        addObject(mi4, 75,200);
        MiddleInvisible mi5 = new MiddleInvisible();
        addObject(mi5, 75,230);
        MiddleInvisible mi6 = new MiddleInvisible();
        addObject(mi6, 75,270);
        MiddleInvisible mi7 = new MiddleInvisible();
        addObject(mi7, 75,300);
        MiddleInvisible mi8 = new MiddleInvisible();
        addObject(mi8, 75,330);
        MiddleInvisible mi9 = new MiddleInvisible();
        addObject(mi9, 75,370);
        MiddleInvisible mi10 = new MiddleInvisible();
        addObject(mi10, 75,400);
        MiddleInvisible mi11 = new MiddleInvisible();
        addObject(mi11, 75,430);
        MiddleInvisible mi12 = new MiddleInvisible();
        addObject(mi12, 75,470);
        MiddleInvisible mi13 = new MiddleInvisible();
        addObject(mi13, 75,500);
        MiddleInvisible mi14 = new MiddleInvisible();
        addObject(mi14, 75,530);
        MiddleInvisible mi15 = new MiddleInvisible();
        addObject(mi15, 75,570);
        //draw the ground
        Ground ground = new Ground();
        addObject(ground,296,575);
        Ground ground2 = new Ground();
        addObject(ground2,702,575);
        //draws the flappybird
        FlappyBird flappyBird = new FlappyBird();
        addObject(flappyBird,76,169);
    }
}
FlappyWorld3(level 3);
//sets variables
public class FlappyWorld3 extends World
{

    /**
     * Constructor for objects of class FlappyWorld.
     * 
     */
    // the beginning count 
    int count = 0;
    private Counter scor;  
    public FlappyWorld3(Counter scor)
    {    
        // resizes the world
        super(800, 600, 1, false); 
        this.scor=scor;
        addObject(scor, 100, 30);
        prepare();
    }

    public void act()
    {
        //adds one to the count
        count = count + 1;
        //calls the methods created
        addPipes();
    }

    public void addPipes()
    {
        if(count % 60 == 0)
        {
            //adds a new pipe at the edge of the screen
            int randomNum = Greenfoot.getRandomNumber(6);
            addObject(new BottomPipe(), getWidth()-1, 350 + 50 * randomNum);
            addObject(new TopPipe(), getWidth()-1, -200 + 50 * randomNum);
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        //makes what you go through
        MiddleInvisible mi = new MiddleInvisible();
        addObject(mi, 75,60);
        MiddleInvisible mi1 = new MiddleInvisible();
        addObject(mi1, 75,100);
        MiddleInvisible mi2 = new MiddleInvisible();
        addObject(mi2, 75,130);
        MiddleInvisible mi3 = new MiddleInvisible();
        addObject(mi3, 75,170);
        MiddleInvisible mi4 = new MiddleInvisible();
        addObject(mi4, 75,200);
        MiddleInvisible mi5 = new MiddleInvisible();
        addObject(mi5, 75,230);
        MiddleInvisible mi6 = new MiddleInvisible();
        addObject(mi6, 75,270);
        MiddleInvisible mi7 = new MiddleInvisible();
        addObject(mi7, 75,300);
        MiddleInvisible mi8 = new MiddleInvisible();
        addObject(mi8, 75,330);
        MiddleInvisible mi9 = new MiddleInvisible();
        addObject(mi9, 75,370);
        MiddleInvisible mi10 = new MiddleInvisible();
        addObject(mi10, 75,400);
        MiddleInvisible mi11 = new MiddleInvisible();
        addObject(mi11, 75,430);
        MiddleInvisible mi12 = new MiddleInvisible();
        addObject(mi12, 75,470);
        MiddleInvisible mi13 = new MiddleInvisible();
        addObject(mi13, 75,500);
        MiddleInvisible mi14 = new MiddleInvisible();
        addObject(mi14, 75,530);
        MiddleInvisible mi15 = new MiddleInvisible();
        addObject(mi15, 75,570);
        //draw the ground
        Ground ground = new Ground();
        addObject(ground,296,575);
        Ground ground2 = new Ground();

        //draws the flappybird
        FlappyBird flappyBird = new FlappyBird();
        addObject(flappyBird,76,169);
    }
}
MoveLevel2(in between level 1 and 2)
public class MoveLevel2 extends World
{

    /**
     * Constructor for objects of class MoveLevel2.
     * 
     */
    public MoveLevel2()
    {    
        super(800, 600, 1);         
    }
    
        public void act()
    {
         //when enter is pressed the screen will switch to the game
        if(Greenfoot.isKeyDown("shift")) 
        Greenfoot.setWorld(new FlappyWorld2(FlappyWorld.getCount()));
    }
}
MoveLevel3(in between level 2 and 3)
public class MoveLevel3 extends World
{

    /**
     * Constructor for objects of class MoveLevel3.
     * 
     */
    public MoveLevel3()
    {    
        super(800, 600, 1); 
    }
    
        public void act()
    {
         //when enter is pressed the screen will switch to the game
        if(Greenfoot.isKeyDown("shift")) 
        Greenfoot.setWorld(new FlappyWorld3(FlappyWorld.getCount()));
    }
}
Now when you play level 2 you immediately go back to in between level 1 and 2 instead of playing then going to in between level 2 and 3
Mia@2703 Mia@2703

2019/7/15

#
Hello!
AnimeRules AnimeRules

2019/7/15

#
Do you know how to fix this?
Mia@2703 Mia@2703

2019/7/15

#
NO
AnimeRules AnimeRules

2019/7/15

#
if anyone can help me I would really aprecciate it! Thanks!
danpost danpost

2019/7/15

#
You have a grave misunderstanding about extending classes. An extension to a class, in design, is to modify or sub-classify a type of object. For example, if you had an Insect class, its extensions could feasibly be Fly, Bee, Moth, Locust, JuneBug, etc. All objects created from these extensions are still Insect objects. A Counter object is clearly not a MiddleInvisible object. There are multiple previous discussions dealing with keeping a current score when changing worlds. Do a search and check some out.
AnimeRules AnimeRules

2019/7/16

#
I figured it out
AnimeRules AnimeRules

2019/7/16

#
thanks for telling me about classes tho
You need to login to post a reply.