Game Development Reference
In-Depth Information
Now we can use the object to store anything we want, such as the name and
the sprite.
bodyDef.userData.name=s;
bodyDef.userData.asset=asset;
3.
Finally, we can add the sprite on the stage:
addChild(bodyDef.userData.asset);
4.
Once we apply the changes to userData property when creating bricks, to
maintain some kind of uniformity with other bodies, we need to apply the
same changes to floor and idol functions. The floor function is shown
as follows:
private function floor():void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,465/worldScale);
bodyDef.userData=new Object();
bodyDef.userData.name="floor";
bodyDef.userData.asset=new Floor();
addChild(bodyDef.userData.asset);
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);
}
And the idol function is shown as follows:
private function idol(pX:Number,pY:Number):void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
bodyDef.userData=new Object();
bodyDef.userData.name="idol";
bodyDef.userData.asset=new Idol();
addChild(bodyDef.userData.asset);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(5/worldScale,20/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=1;
 
Search WWH ::




Custom Search