从类中访问时间线变量?
Posted
技术标签:
【中文标题】从类中访问时间线变量?【英文标题】:Accessing timeline variables from a class? 【发布时间】:2015-02-22 06:29:27 【问题描述】:在我的程序的一个框架中,我有一个“玩家”的代码,它本质上是一门跟随角色的大炮,细节并不重要。所以我想把这门大炮做成一个类,然后我可以把它作为一个电影剪辑放在舞台上,并让类似的大炮提供类似的功能。所以基本上我需要把它变成一个以某种方式与时间线变量交互的类?
现在 Player 类看起来像这样
package
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class Player extends MovieClip
public function Player()
警告代码转储你不必阅读所有这些,这是我需要制作成一个类的播放器代码,以便我可以让更多的玩家使用不同的参数而不是全部跟随角色等......那么我该怎么做呢?这段代码目前正在与舞台上的对象和时间轴中的其他变量进行交互。
// 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));
【问题讨论】:
我建议你不要在时间轴上编码。但是从类中你可以访问像这样的变量 MovieClip(root).variable 【参考方案1】:您可以从类中访问如下变量:MovieClip(root).variable
【讨论】:
那么,如何访问舞台上的对象?我有对象 world.wall01、world.wall02 等。(对象内的 wall0number 实例名称和世界实例名称)当我尝试将它们添加到上面提到的列表中时,我收到了这个奇怪的错误TypeError: Error #1123: Filter operator not supported on type class flash.display.MovieClip. at Player() at Maintest_fla::MainTimeline/createPlayer()
这是我是如何通过他们的_solidObjects = [MovieClip.(root).world.wall01,MovieClip.(root).world.wall02,MovieClip.(root).world.wall03,MovieClip.(root).world.wall04];
@Altrows - 去掉MovieClip
之后的句点,这就是引发过滤操作错误的原因。仔细看@tziuka 的回答,MovieClip
后面没有句号。
天哪,我是个白痴,有时我想我需要更多的睡眠):谢谢。以上是关于从类中访问时间线变量?的主要内容,如果未能解决你的问题,请参考以下文章