A problem with a timer event sometimes not working
Guys...
I have this problem:
I have a child added to the stage eacg 5 seconds and if this child still on the stage after another 5 seconds, you go to the next frame...
My problem is that sometimes it does not work, meaning that you can have this child on the stage for ever and nothing will happen... but sometimes it does work properlly...
Is there any mistakes in my code or what should I do?
var miC4:Loader = new Loader();
miC4.load(new URLRequest("nivel1.jpg"));
addChild(background1);
background1.addChild(miC4);
if (!lives){var lives:int = 3;}
var enem1:Loader = new Loader();
enem1.load(new URLRequest("1enemigo.png"));
var enemy1Array:Array = new Array(enem1);
var t1:Timer=new Timer(5000,1);
var t2:Timer=new Timer(10500,1);
recycleEnemy();
function removeEnemy(){
background1.removeChild(enem1);
recycleEnemy();
}
function touchListener(event:MouseEvent){
enem1.removeEventListener(MouseEvent.CLICK, touchListener);
removeEnemy();
}
function recycleEnemy():void{
enem1.x=(50 + Math.random() * (stage.stageWidth - 150));
enem1.y=(50 + Math.random() * (stage.stageHeight + -100));
t1.addEventListener(TimerEvent.TIMER, addEnemy);
t1.start();
t2.addEventListener(TimerEvent.TIMER, bang1);
t2.start();
}
function addEnemy(e:TimerEvent):void {
background1.addChild(enem1);
enem1.addEventListener(MouseEvent.CLICK, touchListener);
enemy1Array.push(enem1);
}
function bang1(e:TimerEvent):void {
if(enem1.stage){
lives--;
if (lives >= 0) {
t1.stop();
t2.stop();
removeChild(background1);
gotoAndStop(5);
}
}
Thanks a lot!!!!
