-
Removing a Child within the Timer Class
I want to add a child to the stage for 5 seconds and then remove it. When I use the following code I get a "1120: Access of undefined property WordoScreen." error.
In particular the problem only occurs when I use the "removeChild(WordonScreen);" using the TIMER_COMPLETE. If I remove that statement I don't have a problem.
Here's the code:
import flash.utils.Timer;
stop();
//Adding a child to the screen
var myTestTimer:Timer = new Timer(300, 1);
myTestTimer.addEventListener(TimerEvent.TIMER, addaGraphic);
function addaGraphic (e:TimerEvent):void{
var WordonScreen:tapWords = new tapWords
addChild(WordonScreen);
WordonScreen.x = 117;
WordonScreen.y = 78;
}
myTestTimer.start();
//When timer is finished... it should go away.
myTestTimer.addEventListener(TimerEvent.TIMER_COMP LETE,timerCompleted;
function timerCompleted (e:TimerEvent):void {
removeChild(WordonScreen);
}
What am I doing wrong?
-
The WordonScreen instance is currently a local variable and only has scope inside the addaGraphic function. Make sure the WordonScreen instance is outside the function.
-
Thanks for your response. I have two questions:
1) I understand your recommendation for keeping the variable outside of the function. However, in the coding scenario in question how do I create a global variable when I have already defined it with the function (var WordonScreen:tapWords = new tapWords)? Isn't that where the variable is defined? Do simple move the statement "var WordonScreen:tapWords = new tapWords" above the function?
2) I have decided to actually do it another way. I'm starting out with an instance on the screen and then removing it after 4 seconds. However, I was wondering would there be any optimization issues (i.e. the timer still running etc.) with the following code?
//Removes instance already on screen after 4 seconds
var myTestTimer:Timer = new Timer(4000, 1);
myTestTimer.addEventListener(TimerEvent.TIMER, RemoveGraphic);
function RemoveGraphic (e:TimerEvent):void{
getChildByName("TextonScreen")
removeChild(getChildByName("TextonScreen"))
}
myTestTimer.start();
//When timer is finished... does something.
myTestTimer.addEventListener(TimerEvent.TIMER_COMP LETE, timerCompleted);
function timerCompleted (e:TimerEvent):void {
myTestTimer.stop();
}
-
Outside like this
PHP Code:
var WordonScreen:tapWords;
function addaGraphic (e:TimerEvent):void{
WordonScreen = new tapWords()
addChild(WordonScreen);
WordonScreen.x = 117;
WordonScreen.y = 78;
}
-
-
http://help.adobe.com/en_US/ActionSc...0204-7f9d.html
Take a look at 'Understanding variable scope' section for more information on local and global variables.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|