много помощи от вас, ребята :). Мой следующий вопрос здесь :).AS3 Как добавить класс на сцену из таймера?
У меня есть таймер в классе MyTimer.as и Thief1_mc.as. Как я могу добавитьChild (Thief1_mc) на сцене из MyTimer? Все выглядит просто, единственная проблема - это свойство «stage». Класс MyTimer не может отправлять «этап» в качестве аргумента, потому что он не находится на самой сцене. Я попытался добавить MyTimer на сцене в Main class, например addChild (MyTimer), трассировка говорит, что MyTimer находится на сцене, но я до сих пор не могу передать аргумент этапа Thief1_mc. Мне нужен этот аргумент для отправки, потому что класс Thief1_mc должен добавить себя на сцену, используя свойство «stage».
Код:
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 класс и "_thief1.createItself (этап)" Вызывающий со стадией arument
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();
}
}
}
}
Просто выбросьте это, ваша функция createItself (st): void должен быть создан сам (st: Stage): void. –