Accessing timeline variables from a class?

Trows

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?

right now the Player class looks like this

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;

    public class Player extends MovieClip
    {
    public function Player() {
}
}
}

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.

// 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 & 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&&_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 & sine
    _pcos = Math.cos(_player.rotation * Math.PI / 180);
    _psin = Math.sin(_player.rotation * Math.PI / 180);

    // start X & Y
    // calculate the tip of the barrel
    _startX = _player.x - _barrelLength * _pcos;
    _startY = _player.y - _barrelLength * _psin;

    // end X & 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 < _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) > 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 < _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 < _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 & 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 > _player.rotation + 180)
    {
        rotateTo -=  360;
    }
    if (rotateTo < _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 < 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));
}
tziuka

From the class you can access a variable like this: MovieClip(root).variable

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Javascript accessing class variables from window object

분류에서Dev

Accessing R's Environmental Variables from Bash

분류에서Dev

accessing variables from main method in python idle

분류에서Dev

Accessing the EntityManager from a JSF Converter class

분류에서Dev

Accessing private instance variable of inner class from outer class

분류에서Dev

Accessing derived class member from base class pointer

분류에서Dev

Incorrect array values when accessing an array from another class?

분류에서Dev

Accessing Environment Variables in flash

분류에서Dev

Accessing Static variables in cakephp

분류에서Dev

Variables not updated from within baseHTTPserver class

분류에서Dev

Variables not updated from within baseHTTPserver class

분류에서Dev

Accessing the Properties of a class dynamically

분류에서Dev

Accessing nested hashes using variables

분류에서Dev

Accessing nested JavaScript objects with variables

분류에서Dev

Accessing winreg from jython

분류에서Dev

How to preserve class variables accessed from JNI when using ProGuard

분류에서Dev

Local variables referred from inner class must be final or effectively final

분류에서Dev

The use of protected member variables from child class and abstract parent

분류에서Dev

accessing the member of a class of pointer array of another class

분류에서Dev

Accessing javascript variables across html files

분류에서Dev

Accessing struct member variables passed into function

분류에서Dev

Accessing the variables of a parent widget via a button

분류에서Dev

angular $interval - accessing variables outside of the function

분류에서Dev

Accessing Docker Services from Host

분류에서Dev

Accessing MacOS X clipboard from

분류에서Dev

Accessing variable from JavaScript object

분류에서Dev

Accessing API from Chrome extension

분류에서Dev

Accessing AR from value object

분류에서Dev

Accessing a variable in a void from main

Related 관련 기사

  1. 1

    Javascript accessing class variables from window object

  2. 2

    Accessing R's Environmental Variables from Bash

  3. 3

    accessing variables from main method in python idle

  4. 4

    Accessing the EntityManager from a JSF Converter class

  5. 5

    Accessing private instance variable of inner class from outer class

  6. 6

    Accessing derived class member from base class pointer

  7. 7

    Incorrect array values when accessing an array from another class?

  8. 8

    Accessing Environment Variables in flash

  9. 9

    Accessing Static variables in cakephp

  10. 10

    Variables not updated from within baseHTTPserver class

  11. 11

    Variables not updated from within baseHTTPserver class

  12. 12

    Accessing the Properties of a class dynamically

  13. 13

    Accessing nested hashes using variables

  14. 14

    Accessing nested JavaScript objects with variables

  15. 15

    Accessing winreg from jython

  16. 16

    How to preserve class variables accessed from JNI when using ProGuard

  17. 17

    Local variables referred from inner class must be final or effectively final

  18. 18

    The use of protected member variables from child class and abstract parent

  19. 19

    accessing the member of a class of pointer array of another class

  20. 20

    Accessing javascript variables across html files

  21. 21

    Accessing struct member variables passed into function

  22. 22

    Accessing the variables of a parent widget via a button

  23. 23

    angular $interval - accessing variables outside of the function

  24. 24

    Accessing Docker Services from Host

  25. 25

    Accessing MacOS X clipboard from

  26. 26

    Accessing variable from JavaScript object

  27. 27

    Accessing API from Chrome extension

  28. 28

    Accessing AR from value object

  29. 29

    Accessing a variable in a void from main

뜨겁다태그

보관