home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Jumper.swf / scripts / game / GameObject.as < prev    next >
Encoding:
Text File  |  2008-09-05  |  3.1 KB  |  130 lines

  1. package game
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.EventDispatcher;
  5.    import flash.events.TimerEvent;
  6.    
  7.    public class GameObject extends EventDispatcher
  8.    {
  9.        
  10.       
  11.       public var y:Number;
  12.       
  13.       public var vx:Number = 0;
  14.       
  15.       private var theGame:Game;
  16.       
  17.       public var vy:Number = 0;
  18.       
  19.       public var h:Number;
  20.       
  21.       public var w:Number;
  22.       
  23.       public var ay:Number = 0;
  24.       
  25.       public var ax:Number = 0;
  26.       
  27.       public var x:Number;
  28.       
  29.       public function GameObject(x:int, y:int, w:int, h:int, vx:Number = 0, vy:Number = 0)
  30.       {
  31.          super();
  32.          this.theGame = theGame;
  33.          this.x = x;
  34.          this.y = y;
  35.          this.w = w;
  36.          this.h = h;
  37.          this.vx = vx;
  38.          this.vy = vy;
  39.       }
  40.       
  41.       public function getW() : int
  42.       {
  43.          return w;
  44.       }
  45.       
  46.       public function getX() : int
  47.       {
  48.          return x;
  49.       }
  50.       
  51.       public function move(event:TimerEvent) : void
  52.       {
  53.          if(x < 0)
  54.          {
  55.             vx = -vx;
  56.             x = 0;
  57.          }
  58.          if(x + w > 640)
  59.          {
  60.             vx = -vx;
  61.             x = 640 - w;
  62.          }
  63.          x += vx;
  64.          dispatchEvent(new Event("objectChanged"));
  65.       }
  66.       
  67.       public function setPos(x:int, y:int) : void
  68.       {
  69.          this.x = x;
  70.          this.y = y;
  71.          dispatchEvent(new Event("objectChanged"));
  72.       }
  73.       
  74.       public function isInside(obj:GameObject) : Boolean
  75.       {
  76.          if(x >= obj.x && x <= obj.x + obj.w && y >= obj.y && y <= obj.y + obj.h)
  77.          {
  78.             return true;
  79.          }
  80.          if(x + w >= obj.x && x + w <= obj.x + obj.w && y >= obj.y && y <= obj.y + obj.h)
  81.          {
  82.             return true;
  83.          }
  84.          if(x >= obj.x && x <= obj.x + obj.w && y + h >= obj.y && y + h <= obj.y + obj.h)
  85.          {
  86.             return true;
  87.          }
  88.          if(x + w >= obj.x && x + w <= obj.x + obj.w && y + h >= obj.y && y + h <= obj.y + obj.h)
  89.          {
  90.             return true;
  91.          }
  92.          if(x <= obj.x && x + w >= obj.x + obj.w && y >= obj.y && y + h <= obj.y + obj.h)
  93.          {
  94.             return true;
  95.          }
  96.          if(x >= obj.x && x + w <= obj.x + obj.w && y <= obj.y && y + h >= obj.y + obj.h)
  97.          {
  98.             return true;
  99.          }
  100.          return false;
  101.       }
  102.       
  103.       public function distanceTo(obj:GameObject) : Number
  104.       {
  105.          return Math.sqrt(Math.pow(x + w / 2 - (obj.x + obj.w / 2),2) + Math.pow(y + h / 2 - (obj.y + obj.h / 2),2));
  106.       }
  107.       
  108.       public function update() : void
  109.       {
  110.          dispatchEvent(new Event("objectChanged"));
  111.       }
  112.       
  113.       public function getY() : int
  114.       {
  115.          return y;
  116.       }
  117.       
  118.       public function getH() : int
  119.       {
  120.          return h;
  121.       }
  122.       
  123.       public function destroy() : void
  124.       {
  125.          dispatchEvent(new ObjectChangedEvent(this,"objectDestroyed"));
  126.          Game.timer.removeEventListener("timer",move);
  127.       }
  128.    }
  129. }
  130.