home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / core / objects / Ball.as next >
Encoding:
Text File  |  2008-09-03  |  2.2 KB  |  80 lines

  1. package core.objects
  2. {
  3.    import Box2D.Collision.Shapes.b2CircleDef;
  4.    import Box2D.Collision.Shapes.b2ShapeDef;
  5.    import Box2D.Dynamics.b2Body;
  6.    import Box2D.Dynamics.b2BodyDef;
  7.    import core.events.CoreEvent;
  8.    
  9.    public class Ball extends BaseObject
  10.    {
  11.        
  12.       
  13.       public var ballDef:BallDef;
  14.       
  15.       public function Ball(def:BallDef)
  16.       {
  17.          ballDef = def;
  18.          super(def);
  19.       }
  20.       
  21.       override protected function init() : void
  22.       {
  23.          super.init();
  24.          engine.dispatcher.addEventListener(CoreEvent.UPDATE,updateHandler);
  25.          updateSkin();
  26.          initSounds();
  27.       }
  28.       
  29.       protected function updateHandler(e:CoreEvent) : void
  30.       {
  31.          updateSkin();
  32.          var posY:Number = body.GetPosition().y * engine.m_physScale;
  33.          if(posY > 480 + ballDef.r)
  34.          {
  35.             isDead = true;
  36.          }
  37.       }
  38.       
  39.       protected function initSounds() : void
  40.       {
  41.       }
  42.       
  43.       override protected function createBody(bodyDef:b2BodyDef, shapeDef:b2ShapeDef) : b2Body
  44.       {
  45.          var body:b2Body = engine.m_world.CreateBody(bodyDef);
  46.          body.CreateShape(shapeDef);
  47.          body.SetMassFromShapes();
  48.          return body;
  49.       }
  50.       
  51.       override protected function createShapeDef() : b2ShapeDef
  52.       {
  53.          var shapeDef:b2CircleDef = null;
  54.          shapeDef = new b2CircleDef();
  55.          shapeDef.radius = ballDef.r / engine.m_physScale;
  56.          shapeDef.density = 1;
  57.          shapeDef.friction = 0.5;
  58.          shapeDef.restitution = 0.2;
  59.          shapeDef.filter.categoryBits = 2;
  60.          shapeDef.filter.maskBits = 65535 & ~2;
  61.          return shapeDef;
  62.       }
  63.       
  64.       override public function destroy(e:CoreEvent = null) : void
  65.       {
  66.          super.destroy(e);
  67.          engine.dispatcher.removeEventListener(CoreEvent.UPDATE,updateHandler);
  68.       }
  69.       
  70.       override protected function createBodyDef() : b2BodyDef
  71.       {
  72.          var bodyDef:b2BodyDef = new b2BodyDef();
  73.          bodyDef.position.Set(ballDef.x / engine.m_physScale,ballDef.y / engine.m_physScale);
  74.          bodyDef.isBullet = true;
  75.          bodyDef.userData = this;
  76.          return bodyDef;
  77.       }
  78.    }
  79. }
  80.