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

2013/2/19

Spawn Asteroids, that "fall down"!

1
2
Korgaz Korgaz

2013/2/19

#
Hey guys, i want to make a game, just like Asteroids. So i need Asteroids to spawn at the top world edge and move down. The movement downwards i already got, but the spawning is kinda hard for me!
public void act() 
    {
        if(Greenfoot.getRandomNumber(2000) < 50) {
            addObject(new Asteroid_Small(), Greenfoot.getRandomNumber(519),0); 
        }
    }
this one is the code i tried with. Still not working
danpost danpost

2013/2/19

#
The code provided looks ok to me. Where did you put this code?
Game/maniac Game/maniac

2013/2/19

#
try this:
public void act()   
    {  
        if(Greenfoot.getRandomNumber(200) < 50) {  
            addObject(new Asteroid_Small(), Greenfoot.getRandomNumber(519),0);   
        }  
    }
Game/maniac Game/maniac

2013/2/19

#
I made the probability for the spawning smaller
Korgaz Korgaz

2013/2/19

#
@ danpost: I've put this into my World class
Korgaz Korgaz

2013/2/19

#
@ Game/maniac: I've tried this before, didn't work though!
danpost danpost

2013/2/19

#
Please post your world class code.
Korgaz Korgaz

2013/2/19

#
here you go:
import greenfoot.*;  // imports Actor, World, Greenfoot, GreenfootImage

public class Sea extends World
{

    /**
     * Create the turtle world. Our world has a size 
     * of 560x460 cells, where every cell is just 1 pixel.
     */
    public Sea() 
    {
        super(900, 678, 1);

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Starship rocket = new Starship();
        addObject(rocket, 207, 553);
        rocket.setLocation(189, 553);
        Menu menu = new Menu();
        addObject(menu, 716, 347);
        rocket.setLocation(240, 608);
        rocket.setLocation(261, 644);
        menu.setLocation(711, 339);

    }

    public void act() 
    {
        if(Greenfoot.getRandomNumber(200) < 50) {
            addObject(new Asteroid_Small(), Greenfoot.getRandomNumber(519),0); 
        }
    }
}
danpost danpost

2013/2/19

#
There is nothing here that would cause a problem. Let us look at the 'Asteroid_Small' class code.
Korgaz Korgaz

2013/2/19

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Asteroid_Small here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Asteroid_Small extends Animal
{
    private int timer=0;
    
    /**
     * Act - do whatever the Asteroid_Small wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY()+4);
        Remove();
    }

    public void Remove(){
        if (atWorldEdge())
        {getWorld().removeObject(this);
        }
    }

    
}
ok here it is
danpost danpost

2013/2/19

#
See what happens if you remove line 14 from the world class code (the call to the 'prepare' method in the world constructor). Post back as to results. I say this because, again, I do not see any problems with any of the code given so far.
Korgaz Korgaz

2013/2/19

#
Well, now all my Objects i saved in the world don't appear, when i start it. But it still doesn't spawn them. Kind regards, Korgaz
danpost danpost

2013/2/19

#
The idea was to eliminate any other possibilities that may be the cause of your problem. You can put the 'prepare();' call (line 14 of the world class) back in your code. If you have done any editing to the 'Animal' class, please show what you have there. If not, the only thing I can think of is that you are resetting your scenario but not starting it by clicking on the Run button (and I seriously doubt that this is the case).
Korgaz Korgaz

2013/2/19

#
Well actually i haven't changed anything in the Animal class. So there is no other possibility, where the mistake could hide? :( Well thanks so far
danpost danpost

2013/2/19

#
Just a thought, try the same with line 20 of the Asteroid_Small class (the 'Remove();' call in the act method) and try it then. Again, post back with results.
There are more replies on the next page.
1
2