Time for action — enhancing the OnObjectTouch method

Inside the OnObjectTouch method of the engine class, we want to start a new game when the PLAY or RESET button are hit. If the EXIT button was hit, we want to end the game. And if an atom element got hit, we want to start a chain reaction.

  1. Insert a Select statement with a call to the object's GetTag method.
    Class engine Extends ftEngine
    Method OnObjectTouch:Int(obj:ftObject, touchId:Int)
    Select obj.GetTag()
    
  2. Add a Case statement with the game's grpAtom constant. This identifies an atom element.
    Case g.grpAtom
    
  3. Now, check if this object already has a transition going on.
    If obj.GetTransitionCount() = 0 Then
    
  4. Create a new rotation transition by relative 90 degrees to its current angle and with a transition ID of 1. This ID needs to be set so that the OnObjectTransition method is called, once the transition is completed.
    obj.CreateTransRot(90,200, True,1)
    
  5. Now, raise the game's collCount field by 1 and set the objects ID with a negative collCount value. We do this because we check against the ID later on.
    g.collCount += 1
    obj.SetID(-g.collCount)
    Endif
    
  6. Now, add a Case statement with the game's btnPlay and btnReset constants.
    Case g.btnPlay, g.btnReset
    
  7. Call the game's StartNewGame method.
    g.StartNewGame()
    
  8. Insert a Case statement with the game's btnExit statement.
    Case g.btnExit
    
  9. End the game by a call to the Error statement with an empty error text. Weird, but it works! Then, close the Select statement:
    Error("")
    End
    Return 0
    End
    

Save your code now, and then build and run it. All buttons are functional; you can even tap on an element, and it will rotate by 90 degrees.

What just happened?

The OnObjectTouch event is called when the user hits the object with a finger or on a desktop with the left mouse click. We acted on the buttons to change game modes and started the first transition of a new chain reaction.

The eagle has landed—a transition is finished

Once a transition is done, and a transition ID was set during its creation, the engine will call OnObjectTransition.

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

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