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

2017/3/31

Game Freezes when World2 is started

thevictorylp thevictorylp

2017/3/31

#
I`ve created a Space Invaiders like game which changes levles when a certain score is reached but if it changes from level 1 to level 2, level 2 automatically freezes and i really dont know why. Thanks in Advance

Rocket Code

public class Rocket extends Movement
{
    public void World2(){  
    World W2 = new World2();
    Greenfoot.setWorld(W2);
}
    public void FinalLevel(){ 
    World FL = new FinalLevel();
    Greenfoot.setWorld(FL);
}
    public void World3(){ 
    World W3 = new World3();
    Greenfoot.setWorld(W3);
}
     public void World4(){ 
    World W4 = new World4();
    Greenfoot.setWorld(W4);
}
    
    ;
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    long lastShot = System.currentTimeMillis()-1000;
    long currentTime = System.currentTimeMillis();
    static GreenfootSound music = new GreenfootSound("Binding of Isaax Main Theme.mp3");
   
    public void act() 
    {
int x = getX();
int y = getY();
currentTime = System.currentTimeMillis();
      
      if(Greenfoot.isKeyDown("a"))
      {
        this.setLocation(x-2,y);
      }
      if(Greenfoot.isKeyDown("d"))
      {
          this.setLocation(x+2,y);
        }
        
      if(Greenfoot.isKeyDown(" ") && (currentTime >= lastShot + 1000))
      {
          AllyTear A = new AllyTear();
          getWorld().addObject(A,x,y+1);
          lastShot = currentTime;
        }
      if(Greenfoot.isKeyDown("left"))
      {
          this.setLocation(x-2,y);
      }
      if(Greenfoot.isKeyDown("right"))
      {
          this.setLocation(x+2,y);
        }
        if(Greenfoot.isKeyDown("up") && (currentTime >= lastShot + 1000))
      {
          AllyTear A = new AllyTear();
          getWorld().addObject(A,x,y+1);
          lastShot = currentTime;
        }
Actor ET = getOneIntersectingObject(EnemyTear.class);
  
     if(ET!= null){
         Lose Lose = new Lose();
         music.stop();
         getWorld().addObject(Lose, getWorld().getWidth()/2,getWorld().getWidth()/2);
         int s = Score.getScore();
         Greenfoot.stop();
       
        
        }
int gs = Score.getScore();
        
     if(gs == 11)
     {  World2();
    }
    if(gs == 23)
    {  World3();
    }
    if(gs == 35)
    {   World4();}
    if(gs == 47)
    {   FinalLevel();
    }
    if(Greenfoot.isKeyDown("8"))
      {
        Greenfoot.setWorld(new ExtraLevel());
      }
    }

World2 Code

public class World2 extends World { /** * Constructor for objects of class World2. * */ public World2() { super(560, 560, 1); Score counter = new Score(); Score.score = 11; addObject(counter, 60, 20); this.addObject(new Rocket(), this.getWidth() /2, 510); this.addObject(new Enemy(), 50, 100); this.addObject(new Enemy(), 150, 100); this.addObject(new Enemy(), 250, 100); this.addObject(new Enemy(), 350, 100); this.addObject(new Enemy(), 450, 100); this.addObject(new Enemy(), 100, 70); this.addObject(new Enemy(), 200, 70); this.addObject(new Enemy(), 300, 70); this.addObject(new Enemy(), 400,70); }

Score Code

public class Score extends Actor
{
    
     static 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 void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 20, Color.WHITE, Color.BLACK));
    }    
     static void addScore()
    {
        score++;
    }
    static int getScore()
    {
        return score;    
    }
}
Nosson1459 Nosson1459

2017/3/31

#
The only thing fishy going on in that world is that you did Score.score and made score static instead of doing counter.score and leaving it non-static.
You need to login to post a reply.