Time for action — detailing the collision detection

The OnObjectCollision method has two parameters. The first object is the one that actually checks against a collision with the second object. To identify an object, we will compare against its collision group field, collGroup.

  1. Add a local integer variable called i to the OnObjectCollision method.
    Method OnObjectCollision:Int(obj:ftObject, obj2:ftObject)
    Local i:Int
    
  2. Now, check if the collGroup field of the second object is g.grpComet. As this is a constant from the game class, we need to add the prefix g. which stores it.
    If obj2.collGroup = g.grpComet Then
    
  3. Compare the tag field with the constant comet size g.cmLarge.
    If obj2.tag = g.cmLarge Then
    
  4. Spawn an explosion of 15 particles via g.SpawnExplosion at the position of the first object.
    g.SpawnExplosion(15,obj.xPos, obj.yPos)
    
  5. Now, create two new comets of the size g.cmMiddle.
    For i = 1 To 2
    g.CreateComet(g.cmMiddle,obj2.xPos, obj2.yPos, Rnd(1,4)/2,Rnd(0,359))
    Next
    
  6. Add 100 points to the game score. Then close the IF check.
    g.score = g.score + 100
    Endif
    
  7. Repeat the same check, but check if tag is equal to g.cmMiddle. This time create only 10 particles, three small comets, and add 250 points to the game score.
    If obj2.tag = g.cmMiddle Then
    g.SpawnExplosion(10,obj.xPos, obj.yPos)
    For i = 1 To 3
    g.CreateComet(g.cmSmall,obj2.xPos, obj2.yPos, Rnd(1,4)/2,Rnd(0,359))
    Next
    g.score = g.score + 250
    Endif
    
  8. Repeat the same check but check if a tag field of the second object equals g.cmSmall. Create only 5 particles and add 500 points to the score. Don't create any new comets.
    If obj2.tag = g.cmSmall Then
    g.SpawnExplosion(5,obj.xPos, obj.yPos)
    g.score = g.score + 500
    Endif
    
  9. Now subtract 1 from g.cometCount and remove the second object.
    g.cometCount -= 1
    obj2.Remove()
    Endif
    

    That's all for collision checks against comets. The next stop will be the player shots. Why? Because when they hit a comet they need to be removed too.

  10. Check if the collGroup field of the first object equals g.grpShot. If yes, then remove it.
    If obj.collGroup = g.grpShot Then
    obj.Remove()
    Endif
    
  11. Now, check if the player ship ran into a comet.

    Check if the collGroup field of the first object equals g.grpPlayer. If yes, subtract 1 from g.lifes.

    If obj.collGroup = g.grpPlayer Then
    g.lifes -= 1
    Endif
    Return 0
    End
    

What just happened?

During a collision, a lot of things happen. Depending on which objects have collided with each other, you have removed them and updated the game score. If a large or medium-sized comet was destroyed, you have also created a set of smaller comets. This gives the illusion that the former comet was blasted into smaller pieces.

It's about time—acting on timer events

An object timer was created for some objects, either to remove an object (particles, shots) when the timer fires or to deactivate (shield) it.

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

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