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

2014/3/24

Enemies will not actually spawn

Ghost_Nixon Ghost_Nixon

2014/3/24

#
I'm trying to make a game where I have the enemies spawn and then move to the left, but when I use this code
 /**
         *  Spawns the enemy on the edge of the world randomly
         *  
         */
        public void spawning()
        {
            
            
            getWorld().addObject(this,599, Greenfoot.getRandomNumber(400));
        }
    
They will not actually spawn into the world. I don't know how to get them to spawn in and then move.
Entity1037 Entity1037

2014/3/24

#
All this is doing is re-adding the actor you're using into the world. So basically you're just re-drawing it into the world with the given variables (I'm pretty sure). What you should do is this:
getWorld().addObject(new Enemy(),599,Greenfoot.getRandomNumber(400));
Entity1037 Entity1037

2014/3/24

#
Try looking up the methods on the Greenfoot API. It will show you how to use all of the greenfoot methods.
mrligocki mrligocki

2014/3/24

#
You should have your spawning method in a different class than your enemy class (maybe your world, but then you would have to add an act method). if it was in your world then you need something like this:
public void act()
{
spawning();
}

public void spawning()
{
if(something like a timer or random number)
addObject(new Enemy(), 599, Greenfoot.getRandomNumber(400));
}
You can also change
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 
the false lets objects leave the screen, so you can spawn enemies off the screen and have them move onto the screen. To do this you would need to change your Y coordinate so that it's larger than the screen.
Ghost_Nixon Ghost_Nixon

2014/3/25

#
Thanks dudes.
You need to login to post a reply.