Time for action — detailing the collision detection

The problem in this game is that sometimes it will report collisions of objects that are still in action, basically, when they pass by each other. For this, we check if the ID of the object's parent is bigger than the one of the second object's parent. If yes, the second object will start a transition.

  1. Inside the method OnObjectCollision, check if the collision group is equal to g.grpCircle.
    Method OnObjectCollision:Int(obj:ftObject, obj2:ftObject)
    If obj.collGroup = g.grpCircle Then
    
  2. Now, check whether the ID of the first parent obj is bigger than the second one and also that the second parent ID is positive. Negative ones are still in transition.
    If (obj.GetParent().GetID() > obj2.GetParent().GetID()) And (obj2.GetParent().GetID() >= 0) Then
    
  3. Raise the game score.
    g.score += 1
    
  4. Check whether the second parent object is in transition. If not, then start a rotation transition by relative 90 degrees to its current angle and a duration of 200 milliseconds. The transition ID is set to 1, so OnObjectTransition is called once the transition finishes.
    If obj2.GetParent().GetTransitionCount() = 0 Then obj2.GetParent().CreateTransRot(90,200, True,1)
    
  5. Raise g.collCount by 1 and set the ID of the parent object to negative g.collCount.
    g.collCount += 1
    obj2.GetParent().SetID(-g.collCount)
    
  6. Play our famous bing sound.
    g.sndBing.Play()
    
  7. Close all open If checks.
    Endif
    Endif
    Return 0
    End
    

What just happened?

We checked if a collision is able to fire a new transition. We did this by comparing the ID values of the parent atom elements. If all checks were successful, we raised the game score and fired a new transition. And the chain reaction goes on! That's the goal.

The game is done. Save it again and run it. Hopefully, no errors appear and you can have a race to the highest score. Don't worry if you cause an endless chain reaction. It is all by design.

Have a go hero — enhancing Chain Reaction

Now that the game is done, you can add a few things to it by yourself. How about a high-score list? How you could do this is described in Chapter 4, Game# 3 Comet Crusher. There, we used the internal high-score list functionality of fantomEngine.

Another thing you could try is an option screen to turn off sound effects or add different sounds that play in random order. I agree, the current sound becomes very annoying after a short time.

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

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