Game Development Reference
In-Depth Information
In this last script we'll test a sensor.
1.
First, add the Contacts class to the package. This is not required to create
a sensor, it's just to make you see how Box2D handles sensor collision.
import flash.display.Sprite;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Contacts.*;
2.
Then as usual, we give a name to all bodies, make the barrier static,
and define its fixture as a sensor with the isSensor property.
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,470/worldScale);
bodyDef.userData="floor";
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=1;
fixtureDef.restitution=0.5;
fixtureDef.friction=0.5;
fixtureDef.isSensor=true;
var body:b2Body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
bodyDef.position.Set(600/worldScale,240/worldScale);
bodyDef.userData="barrier";
polygonShape.SetAsBox(10/worldScale,220/worldScale);
var body2:b2Body=world.CreateBody(bodyDef);
body2.CreateFixture(fixtureDef);
bodyDef.position.Set(320/worldScale,455/worldScale);
bodyDef.bullet=true;
bodyDef.type=b2Body.b2_dynamicBody;
bodyDef.userData="bullet";
polygonShape.SetAsBox(5/worldScale,5/worldScale);
fixtureDef.isSensor=false;
var body3:b2Body=world.CreateBody(bodyDef);
body3.CreateFixture(fixtureDef);
 
Search WWH ::




Custom Search