Actionscript 3 AS3如何通过计时器在舞台上添加类?
你们有很多帮助:)。我的下一个问题是:) 我在班上有计时器MyTimer.as和Thief1_mc.as电影剪辑。 如何从MyTimer在舞台上添加Child(Thief1_mc)?一切看起来都很简单,唯一的问题是“阶段”属性。MyTimer类无法将“stage”作为参数发送,因为它不在stage本身上。我尝试在主类addChild(MyTimer)中的stage上添加MyTimer,跟踪显示MyTimer在stage上,但我仍然无法将stage参数传递给Thief1_mc。我需要发送这个参数,因为类Thief1_mc必须使用属性“stage”将自己添加到stage上 守则:Actionscript 3 AS3如何通过计时器在舞台上添加类?,actionscript-3,timer,addchild,Actionscript 3,Timer,Addchild,你们有很多帮助:)。我的下一个问题是:) 我在班上有计时器MyTimer.as和Thief1_mc.as电影剪辑。 如何从MyTimer在舞台上添加Child(Thief1_mc)?一切看起来都很简单,唯一的问题是“阶段”属性。MyTimer类无法将“stage”作为参数发送,因为它不在stage本身上。我尝试在主类addChild(MyTimer)中的stage上添加MyTimer,跟踪显示MyTimer在stage上,但我仍然无法将stage参数传递给Thief1_mc。我需要发送这个参数,
public class Thief1_mc extends MovieClip
{
//this variable type Stage will contain stage
private var stageHolder:Stage;
public function Thief1_mc()
{
//constructor
}
//function that creates this object with passed "stage" argument from the caller
public function createItself(st):void
{
//variable that contain the stage so I can use this argument anywhere in the class
stageHolder = st;
//i have to refer to the stage by passed "st" parameter to create this object
stageHolder.addChild(this);
//initial position
this.x = 380;
this.y = 230;
}
}
}
MyTimer类和带有stage Arment的“\u thief1.createOther(stage)”调用者
public class MyTimer extends Sprite
{
private static var nCount:Number = 120;
private static var currentCount:Number;
private static var _timer:Timer = new Timer(1000,nCount);
private static var _timerDispather:Timer;
private static var _thief1:Thief1_mc = new Thief1_mc ;
public function MyTimer()
{
// constructor code
}
//another timer
private static function increaseInterval(interval:int):void
{
_timerDispather = new Timer(interval);
_timerDispather.addEventListener(TimerEvent.TIMER, onUpdateTimeAnotherTimer);
_timerDispather.start();
}
//another timer;
private static function onUpdateTimeAnotherTimer(e:Event):void
{
_thief1.createItself(stage);//the most important part
}
public static function activateTimer():void
{
currentCount = nCount;
_timer.addEventListener(TimerEvent.TIMER, onUpdateTime);
_timer.start();
}
public static function deactivateTimer():void
{
_timer.removeEventListener(TimerEvent.TIMER, onUpdateTime);
_timer.stop();
_timer.reset();
currentCount = nCount;
//another timer
_timerDispather.removeEventListener(TimerEvent.TIMER, onUpdateTimeAnotherTimer);
_timerDispather.stop();
_timerDispather.reset();
}
private static function onUpdateTime(e:Event):void
{
currentCount--;
if (currentCount == 0)
{
_timer.removeEventListener(TimerEvent.TIMER, onUpdateTime);
_timer.stop();
_timer.reset();
}
}
}
}您的代码在一些地方是向后的。它的流程不是很好,在项目的某个阶段,您现在面临的问题将是原来的十倍 首先,您的
MyTimer
类不应该扩展Sprite。它不会被渲染,也不会以图形方式表示任何内容
第二,你的计时器类承担了更多的责任。我会修改它来管理您的计时器和计时器事件只。在timer类中创建一个列表,该列表将包含一些其他元素,这些元素可以使用方法触发器执行其他操作,例如创建和添加Thief1\u mc
简化后的版本可能如下所示:
public class Updater
{
private var _timer:Timer;
private var _toUpdate:Vector.<IUpdatable> = new Vector.<IUpdatable>();
public function Updater()
{
_timer = new Timer(60);
_timer.start();
_timer.addEventListener(TimerEvent.TIMER, _notifyUpdatables);
}
private function _notifyUpdatables(e:TimerEvent):void
{
for each(var i:IUpdatable in _toUpdate)
{
i.update(this);
}
}
public function addUpdatable(updatable:IUpdatable):void
{
_toUpdate.push(updatable);
}
public function removeUpdatable(updatable:IUpdatable):void
{
var index:int = _toUpdate.indexOf(updatable);
if(index >= 0) _toUpdate.splice(index, 1);
}
}
在你的例子中,我要做的是有一个类,它扩展了Sprite并管理应用程序/游戏的图形。它将实现我所描述的IUpdatable
接口,还可以处理添加Thief1\u mc
:
public class View extends Sprite implements IUpdatable
{
public function update(updater:Updater):void
{
// Create a Thief.
var thief:Thief = new Thief();
updater.addUpdatable(thief);
addChild(thief);
}
}
您的小偷
可以利用我们提供的IUpdatable
接口,并在创建更新队列时将其添加到更新队列中,正如我前面所做的那样。举一个完整的例子,下面是小偷类:
public class Thief extends Sprite implements IUpdatable
{
public function update(updater:Updater):void
{
// Make this Thief so some stuff.
//
}
}
下面是如何在document类中将其结合在一起:
public class App extends Sprite
{
private var _updater:Updater;
private var _view:View;
public function App()
{
_updater = new Updater();
_view = new View();
_updater.addUpdatable(_view);
stage.addChild(_view);
}
}
这可能有点压倒一切,看起来像很多工作,但你现在有一个干净的基础,以方便地添加更多的元素。
我们将职责分离,并稍微收紧了流程,而不是让您的一个类像最初那样管理计时器和添加小偷。Updater
只处理存储IUpdatable
实例,并在其中的计时器每次滴答一声时调用它们的update()
方法。视图
类管理图形,并在每次通过更新程序更新时添加小偷
。视图
最初是添加到舞台上的,因此您所需要做的就是将小偷添加到自身中,让他们出现
如果你接受了这一点,并在Updater
中重新构造计时器的工作方式,我想你会达到你想要的效果,但会有更好的理解和结构。把它扔出去,你的函数createSelf(st:void)需要createSelf(st:Stage):void。谢谢你的回答。我将检查您的代码,但对于这样一个简单的任务,它似乎有点复杂。基本上,每次MyTimer滴答作响时,我都需要在舞台上创建对象。这里我最担心的是,即使MyTimer是由主类添加到stage上的,MyTimer也无法访问属性stage!?!嗯,我知道如果一个对象在舞台上,它应该可以访问舞台属性???再次感谢Marty@irnik当然,为了尽快解决您的特定问题,只需在MyTimer
类中创建一个静态方法,如public static function setState(stage:stage)
,然后在类中设置一个属性,您可以使用该属性来引用该阶段。@irnik请原谅我冗长的回答,但我相信,在不久的将来,当你的代码变得难以管理时,你会回到这里,并从中得到一些启示:)我认识Marty,但作为一名新手,我更喜欢与我熟悉的员工一起工作:)@irnik True,但是,在早期接触到好的编程风格是件好事,而不是像我和许多其他开发人员那样,先学习“简单”的方法,然后再回溯,最终学习正确的方法。
public class App extends Sprite
{
private var _updater:Updater;
private var _view:View;
public function App()
{
_updater = new Updater();
_view = new View();
_updater.addUpdatable(_view);
stage.addChild(_view);
}
}