Skip to main content

4 Tutorial : Add bad droid with collision

Hi ! In this tutorial I will teach you how to use collision.


First, add another sprite named droidBad in OnLoad(): droidBad = gfx.CreateSprite( "/Sys/Img/Icon.png", "bad" );
Next, add in OnReady():
gfx.AddSprite( droidBad, 0.5, 0, 0.3 );
droidBad.SetPhysics( 1, "dynamic", null, 0.9 );
Launch your game.
You can see collision between droids.

Now, handle collision events : add this function to your game file : OnCollide( actorA, actorB ) ;
function OnCollide( actorA, actorB ) {
               
}             
actorA and actorB are sorted alphabetically with their groups' name.

Here, we can get actor's group : actorA.group and actorB.group.
Add inside the function : alert( actorA.group + " collides with " + actorB.group );
You can see :
when bad collides with good.

Finally, stop the game when bad collides with good with gfx.Pause();
We have :
function OnCollide( actorA, actorB ) {
   if( actorA.group == "bad" && actorB.group == "good" ) {
      gfx.Pause();
      alert("GAME OVER !");
   }
}


Here is the spk
Bye!

Comments

Post a Comment

Popular posts from this blog

1 Tutorial : Add game engine to a project

In this article, I will teach you to add the game engine object to your project.

2 Tutorial : Create a scene and sprites

In this article, I will teach you to create a scene with a sprite.

3 Tutorial : Animate a sprite

Hi ! In this tutorial I will teach you how animate a sprite.