Game Development Reference
In-Depth Information
6.
Most of the new code needs to be written in the
updateWorld
function.
The idea is to check for idol collisions at every world step rather than
setting up listeners.
Obviously this will be done only if the game is not over yet, so we are
changing the
updateWorld
function in the following way:
private function updateWorld(e:Event):void {
var radToDeg:Number=180/Math.PI;
world.Step(1/30,10,10);
world.ClearForces();
if (! gameOver) {
for (var b:b2Body=world.GetBodyList();
b; b=b.GetNext()) {
if (b.GetUserData()=="idol") {
var position:b2Vec2=b.GetPosition();
var xPos:Number=Math.round(position.x*worldScale);
textMon.text=xPos.toString();
textMon.appendText(",");
var yPos:Number=Math.round(position.y*worldScale);
textMon.appendText(yPos.toString());
textMon.appendText("\nangle: ");
var angle:Number=Math.round(b.GetAngle()*radToDeg);
textMon.appendText(angle.toString());
textMon.appendText("\nVelocity: ");
var velocity:b2Vec2=b.GetLinearVelocity();
var xVel:Number=Math.round(velocity.x*worldScale);
textMon.appendText(xVel.toString());
textMon.appendText(",");
var yVel:Number=Math.round(velocity.y*worldScale);
textMon.appendText(yVel.toString());
for (var c:b2ContactEdge=b.GetContactList();
c; c=c.next) {
var contact:b2Contact=c.contact;
var fixtureA:b2Fixture=contact.GetFixtureA();
var fixtureB:b2Fixture=contact.GetFixtureB();
var bodyA:b2Body=fixtureA.GetBody();
var bodyB:b2Body=fixtureB.GetBody();
var userDataA:String=bodyA.GetUserData();
var userDataB:String=bodyB.GetUserData();
if (userDataA=="floor" && userDataB=="idol") {
levelFailed();
}
if (userDataA=="idol" && userDataB=="floor") {