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

2017/4/24

How to delay a new world

7oel 7oel

2017/4/24

#
Im trying to make it that when a bullet hits the plane, it waits a second, and then loads a new world how do i do that?
import greenfoot.*; 
/**
 * Write a description of class Airplane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Airplane extends Actor
{
   private static final int max_acts_between_shots = 10;
   private int actsBetweenShots = Integer.MAX_VALUE;
   private GreenfootImage Explosion = new GreenfootImage("Explosion.png");
   public static int score1;
   
    /**
     * Act - do whatever the Airplane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
               getImage().scale(60,60);
               move(2);
        if(Greenfoot.isKeyDown("A"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            turn(5);
        }
        if(((MyWorld) getWorld()).getCountDown().isCountDownFinished())
        {
        if(Greenfoot.isKeyDown("SPACE"))
        {
            if(actsBetweenShots > max_acts_between_shots) {
          bullet b = new bullet();
          getWorld().addObject(b, getX(), getY());
           actsBetweenShots = 0;
           b.setRotation(getRotation());
                       
        }
        actsBetweenShots++;
        
        }
    }
    if(isTouching(bullet2.class))
    {
        World MyWorld = getWorld();
        MyWorld world = (MyWorld)MyWorld;
        score score = world.getScore();
        score.addScore1();
        Explosion Explosion = new Explosion();
        getWorld().addObject(Explosion, getX(),getY());
        getWorld().removeObject(this);
        
        Greenfoot.setWorld(new MyWorld());
        
         
        
        
    }
        
    }
    

}
danpost danpost

2017/4/24

#
Remove line 56 and add the following lines to your MyWorld class act method:
if (getObjects(Airplane.class).isEmpty() && getObjects(Explosion.class).isEmpty())
{
    Greenfoot.setWorld(new MyWorld());
    return;
}
7oel 7oel

2017/4/24

#
danpost wrote...
Remove line 56 and add the following lines to your MyWorld class act method:
if (getObjects(Airplane.class).isEmpty() && getObjects(Explosion.class).isEmpty())
{
    Greenfoot.setWorld(new MyWorld());
    return;
}
It says getObjects "cannot find symbol- method getObjects(java.lang.Class<Explosion>)
danpost danpost

2017/4/24

#
7oel wrote...
It says getObjects "cannot find symbol- method getObjects(java.lang.Class<Explosion>)
Show where you used it (at least the entire method this time).
7oel 7oel

2017/4/24

#
danpost wrote...
7oel wrote...
It says getObjects "cannot find symbol- method getObjects(java.lang.Class<Explosion>)
Show where you used it (at least the entire method this time).
import greenfoot.*; 
/**
 * Write a description of class Airplane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Airplane extends Actor
{
   private static final int max_acts_between_shots = 10;
   private int actsBetweenShots = Integer.MAX_VALUE;
   private GreenfootImage Explosion = new GreenfootImage("Explosion.png");
   public static int score1;
   
    /**
     * Act - do whatever the Airplane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
               getImage().scale(60,60);
               move(2);
        if(Greenfoot.isKeyDown("A"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            turn(5);
        }
        if(((MyWorld) getWorld()).getCountDown().isCountDownFinished())
        {
        if(Greenfoot.isKeyDown("SPACE"))
        {
            if(actsBetweenShots > max_acts_between_shots) {
          bullet b = new bullet();
          getWorld().addObject(b, getX(), getY());
           actsBetweenShots = 0;
           b.setRotation(getRotation());
                       
        }
        actsBetweenShots++;
        
        }
    }
    if(isTouching(bullet2.class))
    {
        World MyWorld = getWorld();
        MyWorld world = (MyWorld)MyWorld;
        score score = world.getScore();
        score.addScore1();
        Explosion Explosion = new Explosion();
        getWorld().addObject(Explosion, getX(),getY());
        getWorld().removeObject(this);
        
       if (getObject(Airplane.class).isEmpty() && getObject(Explosion.class).isEmpty())
       {
    Greenfoot.setWorld(new MyWorld());
    return;
}

        
         
        
        
    }
        
    }
    

}
danpost danpost

2017/4/24

#
danpost wrote...
Remove line 56 and add the following lines to your MyWorld class act method:
if (getObjects(Airplane.class).isEmpty() && getObjects(Explosion.class).isEmpty())
{
    Greenfoot.setWorld(new MyWorld());
    return;
}
7oel 7oel

2017/4/24

#
danpost wrote...
danpost wrote...
Remove line 56 and add the following lines to your MyWorld class act method:
if (getObjects(Airplane.class).isEmpty() && getObjects(Explosion.class).isEmpty())
{
    Greenfoot.setWorld(new MyWorld());
    return;
}
But this doesnt delay for 1sec before making the new world?
danpost danpost

2017/4/24

#
It should delay until the explosion animation is completed. Or, is your explosion just a still. Please show your Explosion class code.
7oel 7oel

2017/4/24

#
danpost wrote...
It should delay until the explosion animation is completed. Or, is your explosion just a still. Please show your Explosion class code.
Its just a still, it doesnt have a code
danpost danpost

2017/4/24

#
7oel wrote...
Its just a still, it doesnt have a code
Okay, then you need to do the following extra stuff to the MyWorld class: * add the following instance field:
private int gameOverDelay;
* change the 'if' block I supplied to this:
if (getObjects(Airplane.class).isEmpty() && ++gameOverDelay == 60)
{
    Greenfoot.setWorld(new MyWorld());
    return;
}
7oel 7oel

2017/4/24

#
Doesnt work, just stays as explosion Airplane Code:
import greenfoot.*; 

public class Airplane extends Actor
{
   private static final int max_acts_between_shots = 10;
   private int actsBetweenShots = Integer.MAX_VALUE;
   private GreenfootImage Explosion = new GreenfootImage("Explosion.png");
   public static int score1;
   
    /**
     * Act - do whatever the Airplane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
               getImage().scale(60,60);
               move(2);
        if(Greenfoot.isKeyDown("A"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            turn(5);
        }
        if(((MyWorld) getWorld()).getCountDown().isCountDownFinished())
        {
        if(Greenfoot.isKeyDown("SPACE"))
        {
            if(actsBetweenShots > max_acts_between_shots) {
          bullet b = new bullet();
          getWorld().addObject(b, getX(), getY());
           actsBetweenShots = 0;
           b.setRotation(getRotation());
                       
        }
        actsBetweenShots++;
        
        }
    }
    if(isTouching(bullet2.class))
    {
        World MyWorld = getWorld();
        MyWorld world = (MyWorld)MyWorld;
        score score = world.getScore();
        score.addScore1();
        Explosion Explosion = new Explosion();
        getWorld().addObject(Explosion, getX(),getY());
        getWorld().removeObject(this);
        
      


        
         
        
        
    }
        
    }
    

}
MyWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MyWorld extends World
{
    private Countdown countDown = new Countdown();
    score score = new score();
    private int gameOverDelay;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {  
        super(600, 400, 1); 
        
        prepare();
        if(getObjects(Airplane.class).isEmpty() && ++gameOverDelay == 60)
        {
            Greenfoot.setWorld(new MyWorld());
            return;
        }
        
        }


        
    
        private void prepare()
    {
        
        addObject(new Airplane(),50,200);
        
        addObject(new Airplane2(),550,200);
        addObject(score, 25, 25);
        
        addObject(countDown,300,200);
    }
    private boolean countDownFinished;
    public Countdown getCountDown() {
        return countDown;
    }
    
    public score getScore()
    {
        return score;
    }
}

danpost danpost

2017/4/24

#
You did not place the 'if' block in an act method -- you placed it in the constructor of the class. Add an act method to the class and place it in there.
7oel 7oel

2017/4/24

#
danpost wrote...
You did not place the 'if' block in an act method -- you placed it in the constructor of the class. Add an act method to the class and place it in there.
Im sorry i dont really understand
danpost danpost

2017/4/24

#
Take the 'if' block out from where you put it, add the following into the class:
public void act()
{

}
and put the 'if 'block at line 3 of the act method supplied here.
7oel 7oel

2017/4/24

#
danpost wrote...
Take the 'if' block out from where you put it, add the following into the class:
public void act()
{

}
and put the 'if 'block at line 3 of the act method supplied here.
thanks!
You need to login to post a reply.