Game Development Reference
In-Depth Information
bodyDef.userData="floor";
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,15/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=0.4;
fixtureDef.friction=0.5;
var theFloor:b2Body=world.CreateBody(bodyDef);
theFloor.CreateFixture(fixtureDef);
}
Now we have a way to identify the floor.
2.
At this time, setting up the entire process with listeners and a custom contact
class just to check for a single collision (idol on the ground) would be a waste
of CPU power. There are several collisions: bricks against bricks, bricks against
floor, and idol against brick, but we just need to check if the idol hits the floor.
3.
When you want to handle just a small number of collisions, I would suggest
you to do it without creating a custom contact listener class, and manage
everything on the fly, looping through collisions in a similar way you already
learned to loop through bodies.
4.
First, we need to import the required class to handle collisions:
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Contacts.*;
5.
Then, a Boolean variable called gameOver will allow us to store the game
state. A true value means the idol touched the ground and the level failed;
a false value means the idol is still in the game. Its default value is false
because at the beginning of the level the idol is alive.
private var world:b2World;
private var worldScale:Number=30;
private var textMon:TextField = new TextField();
private var textFormat:TextFormat = new TextFormat();
private var gameOver:Boolean=false;
 
Search WWH ::




Custom Search