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)
Counter
MoveLevel2(the in-between level)
FlappyWorld2(Level 2)
Please Help!
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); } }
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(); } }
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()); } }
//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); } }