Well if we put that same line in the Loop Tab in an if statement, we can create them whenever we want! In obj_player / Loop Tab:
projectile = object_new('obj_projectile')
Press Stop/Play and try it!
if key_was_pressed('space'): projectile = object_new('obj_projectile')
if key_was_pressed('space'): projectile = object_new('obj_projectile') projectile.x = self.x projectile.y = self.y
Now you'll notice we haven't got a sprite set up for it so it is just the empty image square.
spawner = object_new('obj_spawner')
Now that we've created it in the Start Tab, we can use it to count in obj_spawner / Loop:
asteroidTimer = 0
This is saying that every single frame, add 1 to asteroidTimer. There are 60 frames every 1 second, so whenever it equals 60, we will know that 1 second has passed! If it equals 120, that means 2 seconds have passed, etc.
asteroidTimer = asteroidTimer + 1
Press Stop/Play and try it out!
if asteroidTimer == 120: asteroid = object_new('obj_asteroid') asteroid.x = self.x asteroid.y = self.y
Awesome! Now our spawner keeps spawning asteroids every 2 seconds and never stops!
if asteroidTimer == 120: asteroid = object_new('obj_asteroid') asteroid.x = self.x asteroid.y = self.y asteroidTimer = 0
Now test it out again. After a few seconds, asteroids should start coming in from above in a continuous stream!
spawner.y = 400
Ready for more? Try the next lesson!
PixelPAD is loading...