Skip to main content
Inspiring
August 16, 2013
Answered

A problem with a timer event sometimes not working

  • August 16, 2013
  • 1 reply
  • 2353 views

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!!!!

This topic has been closed for replies.
Correct answer kglad

Yup... the whole "thing" happens in that frame...

let´s say it is frame 3... once you go there, your background (which is a MC container) loads, then a background image loads inside the MC, then your enemies appear one by one... so you click on them with your mouse and you get rid of them, but if you don´t, you go to another frame which shows you that you have one less life.... and after a few seconds, it gets you back to frame 3 where everything (but your lifes, score, etc) start all over since it starts adding the MC again and the timers start ticking again also...

And everything works as expected, but these enemies, that sometimes don´t "kill" you and stay forever


ok, so there are a number of issues.

the first error is enemy1Array contains two duplicate objects ~5 seconds after entering that frame.  i'm not sure if that's a problem because i don't see where you're even using enemy1Array, but you should fix that error (if enemy1Array is used) or remove enemy1Array (if it's not used).

the next problem is you can call recycleEnemy more than once and re-add those timers.  flash will probably protect yourself from the bad coding but you shouldn't count on it.  when you call removeEnemy you should stop those timers and remove those listeners before re-adding another pair of listeners.  or just reset the timers in removeEnemy and start them in recycleEnemy.  there's no need to re-add those listeners more than once.  they could be added in your if(!lives) conditional.

the next (and probably most important) error is your removeChild(background1) statement which fails to remove enem1. so, when you re-enter that frame you have an enem1 on-stage that you can't kill and can't even reference.  remove it when you remove the background1.

1 reply

kglad
Community Expert
Community Expert
August 16, 2013

you're going to lose an enem1 references when there's more than 1 enem1 and your enemy1Array is screwy.

what are you trying to do?

SirMarleyAuthor
Inspiring
August 16, 2013

enem1 is your enemy... you have to push on it to eliminate it....

each 5 seconds you have another one...

but if it is on the stage for more than 5.5 secs, you lose a life...

Now, the first one works ok, the second one stays, the third one works good also, but the forth does not...

After that it seems to be working fine, but sometimes it happens again

kglad
Community Expert
Community Expert
August 16, 2013

after 5.5 seconds, in addition to losing a life, is that 5.5 second old enemy destroyed?

if not, do you lose another life from that same enemy after 11 seconds?