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!
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!
Game over
ReplyDelete