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

2017/3/7

Automatically changing & insert objects after.

1
2
OKNEM OKNEM

2017/3/7

#
Cool. I'll get back to you in the morning with some results.
OKNEM OKNEM

2017/3/8

#
Like this?
import greenfoot.*; 

/**
 * My World! :)
 */
public class Space extends World
{
    private Health healthbar = new Health();
    private int health;
    private Counter theCounter;
    public int intropause = 150;
    public int rulespause = 600;
    /**
     * Default constructor that prepares the world.
     */
    public Space()
    {    
        super(800, 600, 1); 
        prepare();
        adjustHealth(5);
    }

    public Counter getCounter()
    {
        return theCounter;
    }
    // Checks the width and height of the Rocket.
    public int x = (new Rocket()).getImage().getWidth();
    public int y = (new Rocket()).getImage().getHeight();

    // Sets the delay for the Asteroid at 0.
    private int delayAsteroid=650;
    /**
     * Checks the delay of the Asteroid class, and if it's at zero, adds another Asteroid at a
     * an x coordinate of 40 degrees, and a random y coordinate.
     */
    public void act()
    {
        if (delayAsteroid>0 && --delayAsteroid>0) 
        {
            return;
        }
        addObject(new Asteroid(), 30, (Greenfoot.getRandomNumber(570)));
        delayAsteroid = (Greenfoot.getRandomNumber(60));
        
        
        
    }
    
    public void prepare()
    {
        Rules rules = new Rules();
        addObject(new Rules(), 400, 300);

        Intro intro = new Intro();
        addObject(new Intro(), 400, 300);

        if(intropause>0)
        {
            intropause--;
        }
        else
        {
            if(intropause == 0)
            {
               removeObject(intro);
            }    
        }
        
        if(rulespause>0)
        {
            rulespause--;
        }
        if(rulespause == 0)
        {
            addObject(healthbar, 690, 34);
            Rocket rocket = new Rocket();
            addObject(new Rocket(270),410,510);
            theCounter = new Counter();
            addObject(theCounter, 76, 30);          
            removeObject(rules);
        }    
    }

    public void adjustHealth(int changeAmount)
    {
        health += changeAmount;
        if (health > 5) health = 5;
        if (health < 0) health = 0;
        healthbar.setImage("health"+health+".png");
        if (health == 0)
        {

        }
    }
    
}
This compiles, but the Intro screen doesn't change.
Super_Hippo Super_Hippo

2017/3/8

#
You can't use timers like that in a method which is only called from the constructor and not from the act-method. It is only executed once. Another thing is that the Intro and Rules you add is not the same as the one you save in the 'intro' and 'rules' variables. Try out this:
public void prepare()
{
    Intro intro = new Intro();
    addObject(intro, getWidth()/2, getHeight()/2);
    Greenfoot.delay(150);
    removeObject(intro);
    Rules rules = new Rules();
    addObject(rules, getWidth()/2, getHeight()/2);
    Greenfoot.delay(600);
    removeObject(rules);
    
    //add all the object for the game
    Greenfoot.start();
}
I am not sure if this is working. If it is not, add only the Intro in the constructor and count down the first timer in the act method. When it reaches 0, you remove the intro and add the rules and do the same thing again. Before the game starts, you use 'return' to escape the act method before the 'random asteroid' thing is executed.
You need to login to post a reply.
1
2