Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Game #3, CometCrusher > Time for action — detailing the OnObjectUpdate metho...

Time for action — detailing the OnObjectUpdate method

Time for action — detailing the OnObjectUpdate method The OnObjectUpdate method has one parameter, the object that is updated. Check if the object is equal to g.player. Method OnObjectUpdate:Int(obj:ftObject) If obj = g.player Then If the up arrow key is pressed, add 1.5 times the engine delta field value to the object (player) with the AddSpeed method. Also, spawn a player engine particle with g.SpawnPlayerEngine. If KeyDown(KEY_UP) 'Speed up obj.AddSpeed(1.5*delta) g.SpawnPlayerEngine() Endif Check if the left arrow key is pressed. Then set the angle, 15*delta degrees relative to its current angle with SetAngle. If KeyDown(KEY_LEFT) 'turn left obj.SetAngle(-15.0 * delta,True) Endif If the right arrow key is pressed, set the angle, +15*delta degrees relative to its current angle with SetAngle. If KeyDown(KEY_RIGHT) 'turn right obj.SetAngle(15.0 * delta,True) Endif Check if the S key was pressed, then spawn a player shot with g.SpawnPlayerShot. If KeyHit(KEY_S) 'Shoot your gun g.SpawnPlayerShot() Endif To fire up the shield, check if the Space key was hit. If the shield is not active, then activate it with g.ActivateShield. 'Fire up shield If KeyHit(KEY_SPACE) And g.shield.isActive= False Then g.ActivateShield() Endif Endif Return 0 End What just happened? Inside the OnUpdateObject method, you have added some logic to read the input from the player and control the movement of the player ship. Also, you can now activate the shield and fire a shot from your deadly cannon. Did I win—checking the losing conditions with OnLayerUpdate Every game needs winning conditions. Or at least losing conditions. A perfect place is the OnLayerUpdate method which is called when the engine updates a layer. In our game, the relevant layer is layerGame.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint