title: Accessing timeline variables from a class?
body: <p>In one of the frame of my program I have code for a "player" that is essentially a cannon that follows the character around, specifics aren't important. So I'd like to make this cannon into a class that I can then place as a movieclip, on the stage and have similar cannons serving similar functions. So Basicly I need to make this into a class that somehow interacts with timeline variables?</p>
  
  <p>right now the Player class looks like this</p>
  
  <pre><code>package 
  {
      import flash.display.*;
      import flash.events.*;
      import flash.ui.*;
  
      public class Player extends MovieClip
      {
      public function Player() {
  }
  }
  }
  </code></pre>
  
  <p>Warning code dump you don't have to read all this, this is the player code that I need to make into a class so that I can make more players with different parameters to their not all following the character etc... So how do I do this? this code is interacting with objects on the stage and other variables in the timeline at the moment.</p>
  
  <pre><code>// player settings
  var _rotateSpeedMax:Number = 20;
  var _gravity:Number = .10;
  // projectile gun settings
  var _bulletSpeed:Number = 4;
  var _maxDistance:Number = 200;
  var _reloadSpeed:Number = 250;//milliseconds
  var _barrelLength:Number = 20;
  var _bulletSpread:Number = 5;
  // gun stuff
  var _isLoaded:Boolean = true;
  var _isFiring:Boolean = false;
  var _endX:Number;
  var _endY:Number;
  var _startX:Number;
  var _startY:Number;
  var _reloadTimer:Timer;
  var _bullets:Array = [];
  
  // array that holds walls
  
  var _solidObjects:Array = [];
  // global vars
  var _player:MovieClip;
  var _dx:Number;
  var _dy:Number;
  var _pcos:Number;
  var _psin:Number;
  var _trueRotation:Number;
  /**
   * Constructor
   */
  _solidObjects = [world.wall01,world.wall02,world.wall03,world.wall04];
  stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
  stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
  //character.addEventListener(Event.ENTER_FRAME, moveDude);
  
  
  //////////////////////////////////////;
  // Player &amp; Weapon Methods
  //////////////////////////////////////
  
  /**
   * Creates player
   * Uses "Player" movieclip linked in library
   */
  createPlayer();
  function createPlayer():void
  {
  
      // attach player movieclip from library
  
      // position player in center
      if (character!=null&amp;&amp;_player!=null)
      {
  
          _player.x = character.x +5;
          _player.y = character.y +5;
      }
      else if (_player ==null)
      {
          _player = new Player();
  
          // add to display list
          stage.addChild(_player);
      }
  }
  
  /**
   * Fire weapon
   */
  function fire():void
  {
      // check if firing
      if (! _isFiring)
      {
          return;
      }
  
      // check if reloaded
      if (! _isLoaded)
      {
          return;
      }
  
      // create bullet
      createBullet();
  
      // start reload timer
      _reloadTimer = new Timer(_reloadSpeed);
      _reloadTimer.addEventListener(TimerEvent.TIMER, reloadTimerHandler);
      _reloadTimer.start();
  
      // set reload flag to false;
      _isLoaded = false;
  }
  
  /**
   * Creates a bullet movieclip and sets it's properties
   */
  function createBullet():void
  {
      // precalculate the cos &amp; sine
      _pcos = Math.cos(_player.rotation * Math.PI / 180);
      _psin = Math.sin(_player.rotation * Math.PI / 180);
  
      // start X &amp; Y
      // calculate the tip of the barrel
      _startX = _player.x - _barrelLength * _pcos;
      _startY = _player.y - _barrelLength * _psin;
  
      // end X &amp; Y
      // calculate where the bullet needs to go
      // aim 50 pixels in front of the gun
      _endX = _player.x - 50 * _pcos + Math.random() * _bulletSpread - _bulletSpread * .5;
      _endY = _player.y - 50 * _psin + Math.random() * _bulletSpread - _bulletSpread * .5;
  
      // attach bullet from library
      var tempBullet:MovieClip = new Bullet();
  
      // calculate velocity
      tempBullet.vx = (_endX - _startX) / _bulletSpeed;
      tempBullet.vy = (_endY - _startY) / _bulletSpeed;
  
      // set position
      tempBullet.x = _startX;
      tempBullet.y = _startY;
  
      // save starting location
      tempBullet.startX = _startX;
      tempBullet.startY = _startY;
  
      // set maximum allowed travel distance
      tempBullet.maxDistance = stage.stageHeight;//_maxDistance;
  
      // add bullet to bullets array
      _bullets.push(tempBullet);
  
      // add to display list;
      stage.addChild(tempBullet);
  }
  
  /**
   * Updates bullets
   */
  function updateBullets():void
  {
      var i:int;
      var tempBullet:MovieClip;
  
      // loop thru _bullets array
      for (i = 0; i &lt; _bullets.length; i++)
      {
          // save a reference to current bullet
          tempBullet = _bullets[i];
  
          // check if gravity is enabled
          if (gravityCheckbox.selected)
          {
              // add gravity to Y velocity
              tempBullet.vy +=  _gravity;
  
          }
  
          // update bullet position
          tempBullet.x +=  tempBullet.vx;
          tempBullet.y +=  tempBullet.vy;
  
          // check if bullet went too far
          if (getDistance(tempBullet.startX - tempBullet.x, tempBullet.startY - tempBullet.y) &gt; tempBullet.maxDistance + _barrelLength)
          {
              destroyBullet(tempBullet);
          }
  
          // check for collision with walls
          if (checkCollisions(tempBullet.x,tempBullet.y))
          {
              destroyBullet(tempBullet);
          }
      }
  }
  
  /**
   * Destroys bullet
   * @parambulletTakes bullet movieclip
   */
  function destroyBullet(bullet:MovieClip):void
  {
      var i:int;
      var tempBullet:MovieClip;
  
      // loop thru _bullets array
      for (i = 0; i &lt; _bullets.length; i++)
      {
          // save a reference to current bullet
          tempBullet = _bullets[i];
  
          // if found bullet in array
          if (tempBullet == bullet)
          {
              // remove from array
              _bullets.splice(i, 1);
  
              // remove from display list;
              bullet.parent.removeChild(bullet);
  
              // stop loop;
              return;
          }
      }
  }
  
  /**
   * Reload weapon
   */
  function reloadWeapon():void
  {
      _isLoaded = true;
  }
  
  /**
   * Checks for collisions between points and objects in _solidObjects
   * @returnCollision boolean
   */
  function checkCollisions(testX:Number, testY:Number):Boolean
  {
      var i:int;
      var tempWall:MovieClip;
  
      // loop thru _solidObjects array
      for (i = 0; i &lt; _solidObjects.length; i++)
      {
          // save a reference to current object
          tempWall = _solidObjects[i];
  
          // do a hit test
          if (tempWall.hitTestPoint(testX,testY,true))
          {
              return true;
  
              // stop loop
              break;
          }
      }
      return false;
  }
  
  /**
   * Calculate player rotation 
   */
  function updateRotation():void
  {
      // calculate rotation based on mouse X &amp; Y
      _dx = _player.x - stage.mouseX;
      _dy = _player.y - stage.mouseY;
  
      // which way to rotate
      var rotateTo:Number = getDegrees(getRadians(_dx,_dy));
  
      // keep rotation positive, between 0 and 360 degrees
      if (rotateTo &gt; _player.rotation + 180)
      {
          rotateTo -=  360;
      }
      if (rotateTo &lt; _player.rotation - 180)
      {
          rotateTo +=  360;
      }
  
      // ease rotation
      _trueRotation = (rotateTo - _player.rotation) / _rotateSpeedMax;
  
      // update rotation
      _player.rotation +=  _trueRotation;
  }
  
  //////////////////////////////////////
  // Event Handlers
  //////////////////////////////////////
  
  /**
   * Enter Frame handler
   * @parameventUses Event
   */
  function enterFrameHandler(event:Event):void
  {
      createPlayer();
      updateRotation();
      updateBullets();
      fire();
  }
  
  /**
   * Mouse Up handler
   * @parameUses MouseEvent
   */
  function onMouseUpHandler(event:MouseEvent):void
  {
      _isFiring = false;
  }
  
  /**
   * Mouse Down handler
   * @parameUses MouseEvent
   */
  function onMouseDownHandler(event:MouseEvent):void
  {
      _isFiring = true;
  }
  
  /**
   * Reload timer
   * @parameTakes TimerEvent
   */
  function reloadTimerHandler(e:TimerEvent):void
  {
      // stop timer
      e.target.stop();
  
      // clear timer var;
      _reloadTimer = null;
  
      reloadWeapon();
  }
  
  //////////////////////////////////////
  // Utilities
  //////////////////////////////////////
  
  /**
   * Get distance
   * @paramdelta_x
   * @paramdelta_y
   * @return
   */
  function getDistance(delta_x:Number, delta_y:Number):Number
  {
      return Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
  }
  
  /**
   * Get radians
   * @paramdelta_x
   * @paramdelta_y
   * @return
   */
  function getRadians(delta_x:Number, delta_y:Number):Number
  {
      var r:Number = Math.atan2(delta_y,delta_x);
  
      if (delta_y &lt; 0)
      {
          r +=  (2 * Math.PI);
      }
      return r;
  }
  
  /**
   * Get degrees
   * @paramradiansTakes radians
   * @returnReturns degrees
   */
  function getDegrees(radians:Number):Number
  {
      return Math.floor(radians/(Math.PI/180));
  }
  </code></pre>