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

2016/2/9

How do I get to Level 3 on my Game? Need Help Asap:(

Dimitris1992 Dimitris1992

2016/2/9

#
So I have a game which I have managed to make a second level for, using the score method, whereby if the score goes up to 10 it goes to level 2, however I did the same for Level 3 but level 2 keeps resetting, as opposed to moving onwards to the third level. This is my Code can someone please help me? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class StandardBall here. * * @author (your name) * @version (a version number or a date) */ public class StandardBall extends Actor{ public int checkScore = 0; private Counter counter; private int hSpeed = 7, vSpeed = 10; public void act() { movement(); getWorld().addObject(new trail(),getX(),getY()); Actor paddle = getOneIntersectingObject(paddle.class); if( paddle != null ) { hSpeed= -hSpeed; } Actor barrel = getOneIntersectingObject(barrel.class); if(barrel != null) { getWorld().removeObject(barrel); Counter counter = new Counter(); counter.addScore(); checkScore++; } Actor bee = getOneIntersectingObject(bee.class); if(bee != null) { getWorld().removeObject(bee); Counter counter = new Counter(); counter.addScore(); checkScore++; } Level2(); Level3(); } public void movement() { setLocation( getX() +hSpeed, getY() + vSpeed); if(getX()<5) { hSpeed = -hSpeed; } if(getY()<5) { vSpeed = -vSpeed; } if (getX() > getWorld().getWidth()-2) { hSpeed = -hSpeed; } if (getY() > getWorld().getHeight()-2) { vSpeed = -vSpeed; } } public void MyWorld(){ if(checkScore >=10){ Greenfoot.setWorld(new Level2()); } } public void Level2(){ if(checkScore >=10){ Greenfoot.setWorld(new Level3()); } } public void Level3(){; if(checkScore >=10){ Greenfoot.setWorld(new Level3()); } } }
danpost danpost

2016/2/9

#
You need to see what level world the actor is currently in to determine which world to proceed to. This can be solved by adding a second condition in your 'if' statements in the 'MyWorld', 'Level2' and 'Level3' methods:
public void MyWorld(){
    if (checkScore >= 10 && getWorld() instanceof MyWorld){
        Greenfoot.setWorld(new Level2());
    }
}

public void Level2(){
    if (checkScore >= 10 && getWorld() instanceof Level2)){
        Greenfoot.setWorld(new Level3());
    }
}

public void Level3(){
    if (checkScore >= 10 && getWorld() instanceof Level3)){
        Greenfoot.setWorld(new Level3());
    }
}
As you currently have it coded, the sum of all your level changing method calls, or this:
MyWorld();
Level2();
Level3();
will do the same as the following:
World next = null;
if (checkScore >= 10){ // this check is done in all three level changing methods
    next = new Level2(); // method 'MyWorld'
    next = new Level3(); // method 'Level2'
    next = new Level3(); // method 'Level3'
}
if (next != null){
    Greenfoot.setWorld(next);
}
As you can tell, lines 3 and 4 are bumped by line 5 in that 'next' will never contain a Level2 world at line 7 or 8. When you create and set a new world active, you are not changing the world the current actor is in or what its score value would contain. Only the last world setting will take after the current act method has completed its execution.
You need to login to post a reply.