package {
import flash.events.Event;
import flash.utils.*;
import flash.geom.*;
import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import wck.*;
import shapes.*;
import misc.*;
import extras.*;
public class Pig extends Circle {
/** Constructor method, called when the bird instance is created */
public function Pig() {
// constructor code
super();
}
/** Called when the pig is added to stage by box2d */
override public function create():void {
trace("pig created");
// Physic properties
density = 10;
friction = 1;
type = "Dynamic";
reportBeginContact = true;
reportEndContact = true;
reportPostSolve = true;
reportPreSolve = true;
allowDragging = false;
super.create();
// Start the collision detection in 1000ms.
// Sometimes the collision is detected at start so I delayed the collection detection
setTimeout(initCollision, 1000);
}
private function initCollision():void {
// Init the collision handler
listenWhileVisible(this, ContactEvent.POST_SOLVE, postSolveContactHandler, false, 0, true);
}
/** Set the physic shapes and the size */
override public function shapes():void {
// 15 px of raduis
circle(15);
}
/** Method called when this physic object collides */
private function postSolveContactHandler(e:ContactEvent):void
{
// Get the impulse the contact
var impulse:b2ContactImpulse = e.impulses
// Get the normal lenght of the contact
var normalLength:Number = Math.sqrt((impulse.normalImpulse1 * impulse.normalImpulse1) + (impulse.normalImpulse2 * impulse.normalImpulse2));
trace("normalLength", normalLength);
// Check if the contact is strong
if (normalLength > 1) {
// Remove the pig
remove();
return;
}
}
}
}