Time for action — updating each object

Inside the OnObjectUpdate method, we will control the player machine. For this, we will check if the collision markers are set and act accordingly.

  1. First, check whether the object is our player object.
    Method OnObjectUpdate:Int(obj:ftObject)
    If obj = g.player Then
    
  2. Next, check that the object is not in some kind of transition. All movements will be made with transitions, so if there aren't any, it stands still.
    If obj.GetTransitionCount() = 0 Then
    
  3. Set the object to be unanimated for now.
    obj.SetAnimated(False)
    
  4. Check whether the UP arrow key on the keyboard, or the "UP" direction of the joypad on your controller, was pressed. Also check that the player doesn't run into a wall or whether a crate that the player pushes could run into another crate.
    If (KeyDown(KEY_UP) Or JoyDown(JOY_UP)) And g.hitWall = False And ((g.hitCrate<>Null And g.hitWall2 = False And g.hitCrate2=Null) Or g.hitCrate=Null) Then
    
  5. Determine the vector position that is 64 pixels in front of the player.
    Local vec:Float[] = obj.GetVector(64,0,True)
    
  6. Transition the player to this vector within 500 milliseconds.
    obj.CreateTransPos(vec[0],vec[1], 500,False,0)
    
  7. Check whether the player runs into a crate and that there is no wall next to the crate.
    If g.hitCrate <> Null And g.hitWall2 = False Then
    
  8. Determine the vector position 128 pixels in front of the player. That will be the target position for the crate's transition.
    Local vecC:Float[] = obj.GetVector(128,0,True)
    
  9. Transition the crate to the vector we determined before.
    g.hitCrate.CreateTransPos(vecC[0],vecC[1], 500,False,0)
    Endif
    Endif
    
  10. Next, check whether the LEFT arrow key was hit on the keyboard or the joypad. Then crate a rotation transition by -90 degrees within 500 milliseconds.
    If (KeyDown(KEY_LEFT) Or JoyDown(JOY_LEFT)) Then
    obj.CreateTransRot(-90, 500,True,0)
    Endif
    
  11. Do the same for the RIGHT arrow key and turn the player's machine to the right.
    If (KeyDown(KEY_RIGHT) Or JoyDown(JOY_RIGHT)) Then
    obj.CreateTransRot( 90, 500,True,0)
    Endif
    
  12. If the object is still in transition, set it to be animated. That will give the illusion that the tracks of the player's machine are running.
    Else
    obj.SetAnimated(True)
    Endif
    Endif
    Return 0
    End
    

What just happened?

By detailing the OnObjectUpdate method, we can control the player's machine now, and when collision detection is implemented, it won't move through the walls and will also be able to push the crates.

Push push—checking for collisions

To be able to push the crates to their target positions, we need to act on collision detection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.171.212