PDA

View Full Version : Code won't continue after removing eventhandler


group_silicon
08-15-2008, 08:30 PM
Hi there,

i've got a function containg other functions and listeners...

I want to get back the amount of collected items in this game...

when everthing is done, i set the exit bool to true and the listeners are deleted, but, the program stops there. after the trace "key listeners removed" comes once again the trace "sdkfjkdsfj" and then the program should give back the collectedAmount... but it doesn't...


please help me...


import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.*;
import flash.net.URLRequest;


trace(startGame());



function startGame():int{

...
...
...

function moveNerd(event:KeyboardEvent):void{

var speed:int = 20;
if(event.keyCode == 38){
trace("nach oben gedrückt")
//circle.y -= 5;
}
else if(event.keyCode == 40){
trace("nach unten gedrückt")
//circle.y += 5;
}
else if(event.keyCode == 37){
trace("nach links gedrückt");
if(!(nerd.x <= 35))nerd.x -= speed;
}
else if
(event.keyCode == 39){
trace("nach rechts gedrückt");
if((nerd.x < 500))nerd.x += speed;
}
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveNerd);

//This loop creates 40 particles that are positioned randomly on the stage.
for (var i=....
....
...
}
trace("done!");

function enterFrameHandler (e:Event):void {
//Loop through the particles
var exit:Boolean = false;

for (var i = 0; i < particlesArray.length; i++) {
var particle = particlesArray[i];
var Badparticle = BadparticlesArray[i];
particle.y += particle.speedY;
particle.x += particle.speedX;
Badparticle.y += Badparticle.speedY;
Badparticle.x += Badparticle.speedX;


if(nerd.hitTestObject(particle)){
particle.y = 0;
particle.x = Math.random() * stage.stageWidth;
++collectedAmount;
}

else if(nerd.hitTestObject(Badparticle)){
Badparticle.y = 0;
Badparticle.x = Math.random() * stage.stageWidth;
if(collectedAmount > 0)
--collectedAmount;
}


AmountField.text = ("Anzahl: " + collectedAmount);


//If the particle is below the bottom, position it back to the top
if (particle.y > stage.stageHeight) {
particle.y = 0;
particle.x = Math.random() * stage.stageWidth;
}
if (Badparticle.y > stage.stageHeight) {
Badparticle.y = 0;
Badparticle.x = Math.random() * stage.stageWidth;
}
if(TimerField.text == "Time's up!"){
exit = true;
//break;
}
}
if(exit == true){
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
trace("enter-listener entfernt");
stage.removeEventListener(KeyboardEvent.KEY_DOWN, moveNerd);
trace("key listener removed");

}
trace("sdkfjkdsfj");
}
addEventListener (Event.ENTER_FRAME, enterFrameHandler);


return collectedAmount;
}

bloodstyle
08-16-2008, 03:43 AM
Your return statement needs to be moved into the "exit" part of your program, looks like it's in your startGame function now.

group_silicon
08-16-2008, 09:29 AM
Your return statement needs to be moved into the "exit" part of your program, looks like it's in your startGame function now.

not at all, because everthing is in the startGame() function, and I want to get the amount (collectedAmount) back, to use it in another program.
I think the return-statement is correct... but there is something wrong with the handler ENTER_FRAME...

I REALLY HAVE NO CLUE :mad:

thanks anyway