O'Reilly logo

Monkey Game Development by Michael Hartlef

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action — spawning an enemy

This new method called SpawnEnemy will create one enemy and initialize its position and the path to follow:

  1. Create a new method called SpawnEnemy, inside the game class. It will have the x position as a parameter. The y position will be fixed and always on top.
    Method SpawnEnemy:Int (x:Float)
    
  2. Add a new instance of the Enemy class to the enemyList field of the game class. The y position is always 0 and there are 25 path nodes to be created:
    enemyList.AddLast(New Enemy(x,0.0,25))
    Return 0
    End
    

What just happened?

This method created an enemy with 25 path nodes and stored it inside the enemy list of the game class.

Starting a new game

Things that should be done multiple times inside a game should be placed inside a function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required