Time for action — detailing the collision detection

Inside the OnObjectCollision method, we will remove tiles, and the ball, and will spawn some particle effects:

  1. Check whether the collision group of obj2 is equal to grpEnemy. That means the ball collided with the enemy:
    Method OnObjectCollision:Int(obj1:ftObject, obj2:ftObject)
    If obj2.GetColGroup() = g.grpEnemy Then
    
  2. Remove obj1, the ball:
    obj1.Remove()
    
  3. Spawn a new particle emitter at the ball's position, with a time factor of 300 milliseconds and a kind flag of 1:
    g.SpawnEmitter(obj1.GetPosX(), obj1.GetPosY(), 300,1)
    
  4. Play the explosion sound effect:
    g.sndExplo.Play()
    
  5. Now, reduce the lifes variable of the game class:
    g.lifes -= 1
    
  6. And create a new ball:
    g.CreateBall()
    Endif
    

    This was the check against enemy collisions. Now, we handle the tile set.

  7. Check whether the collision group of obj2 is equal to grpTile:
    If obj2.GetColGroup() = g.grpTile Then
    

    We will do a very simple collision resolution. This means we will determine the angle from the ball to the tile and then calculate a new angle by adding 180 degrees to it.

  8. Add that angle to the current speed angle of the ball, stored in the local variable ca:
    Local ca:Float = obj.GetSpeedAngle() + obj.GetTargetAngle(obj2) + 180.0
    
  9. Set the speed angle of the ball with the new value:
    obj.SetSpeedAngle(ca)
    
  10. Add a factor of 10 times the level number to the game score:
    g.score += (10 * g.levelNum)
    
  11. Remove the tile object:
    obj2.Remove()
    
  12. Spawn a new particle emitter at the tile's position and with a time factor of 200 milliseconds:
    g.SpawnEmitter(obj2.GetPosX(), obj2.GetPosY(), 200)
    
  13. Add some extra speed to the ball to make it behave like a pinball a little bit:
    obj.AddSpeed(5)
    
  14. Reduce the tile count by one:
    g.tileCount -= 1
    
  15. Play the hit sound effect and close the first IF check:
    g.sndHit.Play()
    Endif
    Return 0
    End
    

What just happened?

By adding the handling of the collision events, we have finished the game in this chapter. When the ball hits the enemy, it will be destroyed and the lives left will be reduced. If the ball hits a tile, the tile will be destroyed and the game score will be rise. And both events, particle emitters will be spawned and a explosion sound will be played.

Have a go hero — enhancing a chain reaction

Now that you have done your homework, it is time to become a hero. There is still a lot that you can do to make the game more exciting:

  • How about adding different particle effects or power-ups that appear randomly and maybe give some kind of shield to the player for a short period of time?
  • Another thing you could add is how the tiles are set up. You could do that differently or maybe add extra walls to the level.

You see, there is so much to add. Go ahead and become a hero!

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

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