Game Development Reference
In-Depth Information
textFormat.size=25;
textMon.defaultTextFormat=textFormat;
brick(275,435,30,30,"breakable",new Brick_30_30());
brick(365,435,30,30,"breakable",new Brick_30_30());
brick(320,405,120,30,"breakable",new Brick_120_30());
brick(320,375,60,30,"unbreakable",
new Brick_60_30_rock());
brick(305,345,90,30,"breakable",new Brick_90_30());
brick(320,300,120,60,"unbreakable",
new Brick_120_60_rock());
idol(320,242);
floor();
addEventListener(Event.ENTER_FRAME,updateWorld);
stage.addEventListener(MouseEvent.CLICK,destroyBrick);
}
As you can see, now every brick call includes a new instance of its sprite.
2.
Changes in the brick function are minimal:
private function
brick(pX:int,pY:int,w:Number,h:Number,s:String,
asset:Sprite):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=s;
bodyDef.userData.asset=asset;
addChild(bodyDef.userData.asset);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(w/2/worldScale,h/2/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=2;
fixtureDef.restitution=0.4;
fixtureDef.friction=0.5;
var theBrick:b2Body=world.CreateBody(bodyDef);
theBrick.CreateFixture(fixtureDef);
}
Obviously, the function declaration now has a new argument called asset ,
but the core of the script resides in the use of the userData property. Rather
than merely storing a string, now it's an object:
bodyDef.userData=new Object();
 
Search WWH ::




Custom Search