Copy link to clipboard
Copied
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!!!!
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. w
...Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Yes...
function bang1(e:TimerEvent):void {
if(enem1.stage){
lives--;
if (lives >= 0) {
t1.stop();
t2.stop();
removeChild(background1);
gotoAndStop(5);
}
}
Everything goes away from the stage, when you lose a life ...
Then on frame 5 there´s a function to make you go back to the game, and once you are there the timer starts again for adding enemies and for them to make you lose another life
Copy link to clipboard
Copied
removing the background just means you're not seeing enem1's being added but they're still added.
do you enter the frame that contains that code more than once?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I just fixed it....
var t1:Timer=new Timer(5000,0);
var t2:Timer=new Timer(10500,0);
That way the timer will repeat indefinitelly
Thanks!
Copy link to clipboard
Copied
that's not going to fix the problem. don't click any enem1 and when you reenter that frame, you'll have 2 enem1. then the next time you re-enter, you'll have 3 etc
Copy link to clipboard
Copied
I got you... Yup, I thought it was working fine, then I made a few tries, and the problem was another one... so I will try as you said and let you know. Thanks Kglad!!!!
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
it worked perfectly...
function removeEnemy(){
t1.stop();
t2.stop();
background1.removeChild(enem1);
recycleEnemy();
}
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();
}
Thanks a lot!!!! (I have put the removeChild(enem1) and (background1) in stage 5 directly as the beggining of that frame)
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now