Copy link to clipboard
Copied
hi, please help. Why won't my Event Listener get removed and the function stopped from executing?
function energy_dec(e=null)
{
if (Sl02_Target_Percent > 30)
{
Sl02_Target_Percent--
if (EL_dec_present == false)
{
t.addEventListener(TimerEvent.TIMER,energy_dec);
EL_dec_present = true
trace("EL_dec ADDED")
}
}
else
{
t.stop()
t.removeEventListener(TimerEvent.TIMER,energy_dec);
EL_dec_present = false
trace("EL_dec REMOVED")
}
}
energy_dec();
Thanks...
Copy link to clipboard
Copied
put a trace outside that function body. does that one execute more than once?
p.s. if you want your timer to only execute once, why don't you set it up to do just that?
var t:Timer=new Timer(whatever,1);
Copy link to clipboard
Copied
hi kglad, thanks for your quick reply...
Yeah, the function gets executed once every time the animation plays from the beginning , thats not the problem. Problem is that it executes every time the timer triggers (in my case two times a second...), even after i try to stop the timer ind remove the EL...
Copy link to clipboard
Copied
because of the lack of formatting i failed to see those nested if-statements. but now that i look more closely, i don't even see where your timer would start.
Copy link to clipboard
Copied
sorry for lack of formatting...
i start the timer once in my "initialiser" function at the beginning of the code. It does only execute once, not every time the animation loops...
Copy link to clipboard
Copied
the code you showed makes no sense. the code you're not showing (when combined with the code you are showing) might make sense but i can't determine that. ie, show the relevant code.
Copy link to clipboard
Copied
My whole code is pretty long and messy by now, but i will try to extract the relevant parts:
import flash.events.Event;
var initializer;
var Sl02_Target_Percent;
var tSpeed:uint = 100;
var t:Timer = new Timer(tSpeed,0)
var EL_dec_present:Boolean;
//Initializer START____________________________________________
function Intialize()
{
if(initializer == false)
{
trace("Initializer DONT fire");
}
else
{
trace("Initializer FIRE");
Sl02_Target_Percent =80;
t.start();
EL_dec_present = false;
}
initializer=false;
}
Intialize()
// Initializer END___________________________________________
// Function Energy Decrease
function energy_dec(e=null)
{
trace ("Sl02_Target_Percent = " + Sl02_Target_Percent)
if (Sl02_Target_Percent > 30)
{
Sl02_Target_Percent--
if (EL_dec_present == false)
{
t.addEventListener(TimerEvent.TIMER,energy_dec);
EL_dec_present = true
trace("EL_dec ADDED")
}
}
else
{
t.stop()
t.removeEventListener(TimerEvent.TIMER,energy_dec);
EL_dec_present = false
trace("EL_dec REMOVED")
}
}
energy_dec();
SORRY, if my code is messy and does not make sense, i am a novice, this is my very first proj in Flash, but i am really trying to learn...
Copy link to clipboard
Copied
is that code in a frame that plays more than once? if yes, that's a problem.
if not, add a trace statement outside the function bodies and show the trace output
Copy link to clipboard
Copied
The code is on the first frame of a layer called "Actions" on the main stage. The Animation is 100 frames long and loops indefinitely. Could you pls elaborate why this is a problem when i try to remove an EL or stop a timer?
i was thinking that i have the "Initialize" function for things that only have to get executed once, not every time the animation loops
Thanks
Copy link to clipboard
Copied
initialize() and energy_dec() are called each time you enter the frame that contains that code.
to prevent that use:
var initialize_called:Boolean;
var energy_dec_called:Boolean;
if(!initialize_called){
initialize_called=true;
initialize();
}
if(!energy_dec_called){
energy_dec_called=true;
energy_dec();
}
Copy link to clipboard
Copied
A am far from an expert and i really appreciate your help, thats why i hate to contradict you, but:
The thing you are suggesting with
var initialize_called:Boolean;
var energy_dec_called:Boolean;
if(!initialize_called){
initialize_called=true;
initialize();
}
doesn't this have the very some effect as the thing i am doing with
function Intialize()
{
if(initializer == false)
{
trace("Initializer DONT fire");
}
else
{
...
}
initializer=false;
????
Thanks anyway...
Copy link to clipboard
Copied
test the code i suggested.
Copy link to clipboard
Copied
allright
Copy link to clipboard
Copied
doesn't work...now Initialize() and energy_dec() NEVER get executed, not even once...
Copy link to clipboard
Copied
you did something wrong. copy and paste the code you tested.
Copy link to clipboard
Copied
import flash.events.Event;
var initializer;
var Sl02_Target_Percent;
var added:Boolean;
var tSpeed:uint = 100;
var t:Timer = new Timer(tSpeed,0)
var tTrue:Boolean;
var EL_dec_present:Boolean;
// Suggested Code by kglad
var initialize_called:Boolean;
var energy_dec_called:Boolean;
if(!initialize_called){
initialize_called=true;
Initialize();
}
if(!energy_dec_called){
energy_dec_called=true;
energy_dec();
}
function tTrueSet(e=null)
{
//energy_dec()
if (tTrue == true)
{
tTrue=false;
}
else
{
tTrue = true;
}
//t.stop();
//t.removeEventListener(TimerEvent.TIMER,nextFF);
//t=null;
trace ("tTrue = " + tTrue)
}
//Initializer START____________________________________________
function Initialize()
{
if(initializer == false)
{
trace("Initializer DONT fire");
}
else
{
var i= null;
StopAll()
resetAlpha()
PercentR = 20;
TextR.stop();
trace("Initializer FIRE");
TextL.addEventListener(Event.ENTER_FRAME, Equal_L_to_R);
Slider02.stop()
Sl02_Target_Percent =80;
var MultiplicationFactor = Slider02.totalFrames/100
var Sl02_Target_Frame:uint = Sl02_Target_Percent * MultiplicationFactor
Slider02.gotoAndStop(Sl02_Target_Frame);
//Slider02.Sl02_Target_Frame = Sl02_Target_Frame;
//var Sl02_Current_Frame:uint;
trace ("Sl02_Target_Frame = " +Sl02_Target_Frame)
//Sl02_Current_Frame = Sl02_Target_Frame;
//Slider02.t=new Timer(1000/fps,0);
//Slider02.t.addEventListener(TimerEvent.TIMER,nextFF);
//Slider02.t.start();
t.start();
EL_dec_present = false;
energy_dec();
}
initializer=false;
}
// Initializer END___________________________________________
var i:int;
var PercentR:int;
var EL_active_FadeRUPto50 = false;
var EL_active_FadeRto80 = false;
var EL_active_FadeRDownto50 = false;
//var Sl02_Target_Percent = null;
var EL_active_FadeRto20 = false;
//var EL_active_FadeRDownto50 = false;
//var EL_active_FadeRDownto50 = false;
trace("Alpha Blue=" + WindBlue50.alpha)
trace("Alpha Red=" + WindRed50.alpha)
trace("Alpha White=" + WindWhite50.alpha)
/*
function CheckPerc(e=null):void
{
if (TextR.PercR >=80)
{
TextR.addEventListener(Event.ENTER_FRAME, CheckPerc);
TextR.stop();
}
}
*/
function timer_dec()
{
}
// Function Energy Decrease
function energy_dec(e=null)
{
trace ("energy_dec toggled")
trace ("Sl02_Target_Percent = " + Sl02_Target_Percent)
if (Sl02_Target_Percent > 30 && EL_dec_present == false)
{
t.addEventListener(TimerEvent.TIMER,energy_dec);
//EL_dec_present = true;
//if (tTrue = true)
//{
Sl02_Target_Percent--
//}
/*
if (added == false)
{
addEventListener(Event.ENTER_FRAME, energy_dec);
added= true;
trace ("added = " + added)
}
else
{
trace ("added = " + added)
}
//ifTimerThenFire()
//Sl02_Target_Percent = 30;
trace ("energy_dec fired")
}
else
{
if (added ==true)
{
removeEventListener(Event.ENTER_FRAME, energy_dec);
trace ("REMOVED")
added= false;
}
*/
}
if (Sl02_Target_Percent <= 30)
{
t.removeEventListener(TimerEvent.TIMER,energy_dec);
EL_dec_present = false;
trace ("EL_dec_present = " + EL_dec_present)
}
}
//CheckPerc();
function Equal_L_to_R(e=null):void
{
var PercL = 100 - TextR.PercR
//trace ("PercL= " + PercL)
TextL.text = PercL + "%";
//TextL.text = "23";
//trace (TextL.text)
}
Equal_L_to_R();
/*
// Slider02 Playback Speed
var currentMC:MovieClip
var fps:int = 1;
function nextFF(e:TimerEvent):void
{
Slider02.nextFrame();
if(Slider02.currentFrame==Slider02.totalFrames)
{
Slider02.gotoAndStop(1);
//currentMC.t.removeEventListener(TimerEvent.TIMER,nextFF);
//playF(Slider02,1)
//currentMC.t.stop();
Slider02.t=null;
//Slider02.play()
}
/* stop after one play
if(currentMC.currentFrame==currentMC.totalFrames)
{
currentMC.t.stop();
currentMC.t.removeEventListener(TimerEvent.TIMER,nextFF);
currentMC.t=null;
}
}
*/
//Slider03 Code
var SliderValue:uint = mySlider.SliderKnob.x;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
var ValueMult:Number = 100 / mySlider.boundWidth;
var SliderValue100:uint = SliderValue * ValueMult;
SliderValue = mySlider.SliderKnob.x;
statusTXT.text = "Slider position is: " + SliderValue100 +"SV= "+mySlider.boundWidth;
}
//____________________________________________
function EL_ADD_FadeRto80_func():void
{
if (EL_active_FadeRto80 == false)
{
TextR.addEventListener(Event.ENTER_FRAME, FadeRto80);
EL_active_FadeRto80 = true;
trace ("ADDED_EL FadeRto80 is active = " + EL_active_FadeRto80)
}
}
function EL_REMOVE_FadeRto80_func():void
{
if (EL_active_FadeRto80 == true)
{
TextR.removeEventListener(Event.ENTER_FRAME, FadeRto80);
EL_active_FadeRto80 = false;
trace ("REMOVED_EL FadeRto80 is active = " + EL_active_FadeRto80)
}
}
function EL_ADD_FadeRUPto50_func():void
{
if (EL_active_FadeRUPto50 == false)
{
TextR.addEventListener(Event.ENTER_FRAME, FadeRUPto50);
EL_active_FadeRUPto50 = true;
trace ("ADDED_EL FadeRUPto50 is active = " + EL_active_FadeRUPto50)
}
}
function EL_REMOVE_FadeRUPto50_func():void
{
if (EL_active_FadeRUPto50 == true)
{
TextR.removeEventListener(Event.ENTER_FRAME, FadeRUPto50);
EL_active_FadeRUPto50 = false;
trace ("REMOVED_EL FadeRUPto50 is active = " + EL_active_FadeRUPto50)
}
}
function EL_ADD_FadeRDownto50_func():void
{
if (EL_active_FadeRDownto50 == false)
{
TextR.addEventListener(Event.ENTER_FRAME, FadeRDownto50);
EL_active_FadeRDownto50 = true;
trace ("ADDED_EL FadeRDownto50 is active = " + EL_active_FadeRDownto50)
}
}
function EL_REMOVE_FadeRDownto50_func():void
{
if (EL_active_FadeRDownto50 == true)
{
TextR.removeEventListener(Event.ENTER_FRAME, FadeRDownto50);
EL_active_FadeRDownto50 = false;
trace ("REMOVED_EL FadeRto80 is active = " + EL_active_FadeRDownto50)
}
}
function EL_ADD_FadeRto20_func():void
{
if (EL_active_FadeRto20 == false)
{
TextR.addEventListener(Event.ENTER_FRAME, FadeRto20);
EL_active_FadeRto20 = true;
trace ("ADDED_EL FadeRto20 is active = " + EL_active_FadeRto20)
}
}
function EL_REMOVE_FadeRto20_func():void
{
if (EL_active_FadeRto20 == true)
{
TextR.removeEventListener(Event.ENTER_FRAME, FadeRto20);
EL_active_FadeRto20 = false;
trace ("REMOVED_EL FadeRto20 is active = " + EL_active_FadeRto20)
}
}
//___________________________________________________________________________________________
function FadeRto80(e=null):void{
if (TextR.PercR >=80)
{
EL_REMOVE_FadeRto80_func();
TextR.stop();
}
else{
EL_ADD_FadeRto80_func();
TextR.play();
}
}
function FadeRUPto50(e=null):void{
if (TextR.PercR >=50)
{
EL_REMOVE_FadeRUPto50_func();
TextR.stop();
}
else{
EL_ADD_FadeRUPto50_func();
TextR.play();
}
}
function FadeRDownto50(e=null):void{
if (TextR.PercR <=50)
{
EL_REMOVE_FadeRDownto50_func();
TextR.stop();
}
else{
EL_ADD_FadeRDownto50_func();
TextR.play();
}
}
function FadeRto20(e=null):void{
if (TextR.PercR <=20)
{
EL_REMOVE_FadeRto20_func();
TextR.stop();
}
else{
EL_ADD_FadeRto20_func();
TextR.play();
}
}
//____________________________________________
function PlusRClick(pEvent:MouseEvent):void
{
TextR.AddTo = true;
EL_REMOVE_FadeRDownto50_func();
EL_REMOVE_FadeRto20_func();
/*
TextR.removeEventListener(Event.ENTER_FRAME, FadeRto20);
TextR.removeEventListener(Event.ENTER_FRAME, FadeRDownto50);
TextR.removeEventListener(Event.ENTER_FRAME, FadeRUPto50);
TextR.removeEventListener(Event.ENTER_FRAME, FadeRto80);
*/
if (TextR.PercR < 50)
{
FadeRUPto50();
}
else{
if (TextR.PercR < 80)
{
FadeRto80();
}
}
}
function MinusRClick(pEvent:MouseEvent):void
{
TextR.AddTo = false;
EL_REMOVE_FadeRUPto50_func();
EL_REMOVE_FadeRto80_func();
//EL_active_FadeRUPto50 = false;
/*
TextR.removeEventListener(Event.ENTER_FRAME, FadeRto20);
TextR.removeEventListener(Event.ENTER_FRAME, FadeRDownto50);
TextR.removeEventListener(Event.ENTER_FRAME, FadeRUPto50);
TextR.removeEventListener(Event.ENTER_FRAME, FadeRto80);
*/
if (TextR.PercR > 50)
{
FadeRDownto50();
}
else
{
if (TextR.PercR > 20)
{
FadeRto20();
}
}
//TextR.text = PercentR + "%"
}
BtnPlusR.addEventListener(MouseEvent.MOUSE_UP, PlusRClick);
BtnMinusR.addEventListener(MouseEvent.MOUSE_UP, MinusRClick);
BtnPlusL.addEventListener(MouseEvent.MOUSE_UP, MinusRClick);
BtnMinusL.addEventListener(MouseEvent.MOUSE_UP, PlusRClick);
function CapAlpha()
{
if(WindBlue50.alpha >= 1)
{
WindBlue50.alpha = 1;
}
if(WindBlue50.alpha <= 0)
{
WindBlue50.alpha = 0;
}
if(WindRed50.alpha >= 1)
{
WindRed50.alpha = 1;
}
if(WindRed50.alpha <= 0)
{
WindRed50.alpha = 0;
}
if(WindWhite50.alpha >= 1)
{
WindWhite50.alpha = 1;
}
if(WindWhite50.alpha <= 0)
{
WindWhite50.alpha = 0;
}
}
//____________________________________________
function FadeBlueIn(event:Event)
{
if(WindBlue50.alpha <= 1)
{
WindBlue50.addEventListener(Event.ENTER_FRAME, FadeBlueIn);
//zeroAlpha()
WindBlue50.alpha += 0.1;
WindRed50.alpha -= 0.1;
WindWhite50.alpha -= 0.1;
WindBlue50.play();
CapAlpha();
i++
trace("i=" + i)
}
if(i >= 10)
{
i = 0;
trace("Event Listeners removed BLUE");
WindBlue50.removeEventListener(Event.ENTER_FRAME, FadeBlueIn);
WindRed50.stop();
WindWhite50.stop();
}
}
function FadeRedIn(event:Event)
{
if(WindRed50.alpha <= 1)
{
WindRed50.addEventListener(Event.ENTER_FRAME, FadeRedIn);
//zeroAlpha()
WindBlue50.alpha -= 0.1;
WindRed50.alpha += 0.1;
WindWhite50.alpha -= 0.1;
WindRed50.play();
CapAlpha();
i++
trace("i=" + i)
}
if(i >= 10)
{
i = 0;
trace("Event Listeners removed RED");
WindRed50.removeEventListener(Event.ENTER_FRAME, FadeRedIn);
WindBlue50.stop();
WindWhite50.stop();
}
}
function FadeWhiteIn(event:Event)
{
if(WindWhite50.alpha <= 1)
{
WindWhite50.addEventListener(Event.ENTER_FRAME, FadeWhiteIn);
//zeroAlpha()
WindBlue50.alpha -= 0.1;
WindRed50.alpha -= 0.1;
WindWhite50.alpha += 0.1;
WindWhite50.play();
CapAlpha();
i++
trace("i=" + i)
trace("Alpha White=" + WindWhite50.alpha)
}
if(i >= 10)
{
i = 0;
trace("Event Listeners removed WHITE");
WindWhite50.removeEventListener(Event.ENTER_FRAME, FadeWhiteIn);
WindBlue50.stop();
WindRed50.stop();
}
}
function resetAlpha():void
{
WindBlue50.alpha = 0;
WindRed50.alpha = 0;
WindWhite50.alpha = 0;
trace("Alpha reset");
}
function zeroAlpha():void
{
WindBlue50.alpha = 0;
WindRed50.alpha = 0;
WindWhite50.alpha = 0;
trace("Alpha zeroed");
}
function PlayAll(event:Event)
{
WindBlue50.play();
WindRed50.play();
WindWhite50.play();
}
function StopAll(event:Event = null):void
{
WindBlue50.stop();
WindRed50.stop();
WindWhite50.stop();
}
//____________________________________________
// Die drei Buttons callen die jewiligen Functions
BtnKalt.addEventListener(MouseEvent.CLICK, FadeBlueIn);
BtnWarm.addEventListener(MouseEvent.CLICK, FadeRedIn);
BtnNormal.addEventListener(MouseEvent.CLICK, FadeWhiteIn);
BtnPlay.addEventListener(MouseEvent.CLICK, PlayAll);
BtnStop.addEventListener(MouseEvent.CLICK, StopAll);
Copy link to clipboard
Copied
that's more code than i'm willing to debug via a forum. sorry.
i suggest you continue to judiciously use the trace function to debug your code, or wait for someone else to critique your code.
Copy link to clipboard
Copied
i thought so...:) I sent you my fla via PM as well... The whole code does work, the problem is only with the Timer Event listeners not getting removed... I think i will try the approach of creating a variable that holds the number of times the timer has to be executed for energy to reach 30. I just thought that this was a common problem and that there was a simple workaround.
Anyways, thanks ALOT for trying to help
kind regards
Copy link to clipboard
Copied
i don't download and correct files unless i'm hired. the only free help i offer is via the forums and because of my mental limitations i'm willing to debug a limited number of lines of code in the forums. if i were smarter maybe i could easily debug your message 16 code, but i cannot easily debug that unless i copy it and use the debugging tools in animate.
Copy link to clipboard
Copied
I completely understand. I would be willing to pay you 50 bucks for an hour of your time, no matter the outcome. we could have a skype/teamviewer debug session tomorrow. Tell me if you are interested...
Copy link to clipboard
Copied
i don't think it will cost that much. i'll download your file and let you know.
p.s. you can contact me via http://www.kglad.com > contact
Copy link to clipboard
Copied
@ps: i fear thats not an option because if energy is currently 50, the timer has to execute 20 times to get to 30, when energy is 70 it has to execute 40 times. i guess i coul calculate a var that says how often the timer has to execute and put that in the timer. I am just curious, why my code does not work...the else condition gets triggered(twice a second) once energy is below 30 but the EL does not get removed and the timer does not stop...??