计时器给出错误 AS3
我的计时器有点问题。我的目标是在 MOUSE_OVER 时调用它并在 MOUSE_OUT 时终止它。
启动计时器的函数:
public function timerStart():void {
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
}
停止计时器的函数:
public function timerStop():void {
myTimer.stop();
}
调用计时器的函数:
public function rollOverHandler(e:MouseEvent = null):void
{
timerStart();
}
调用停止计时器的函数:
internal final function rollOutHandler(e:MouseEvent = null):void
{
timerStop(); //this one created the error message
}
无论我尝试什么,我都会收到此错误消息:
1120: Access of undefined property myTimer.
我明白他无法停止他不认识的计时器这一事实。但即使在任何鼠标操作之前我也会收到错误。我看错了什么?
有人知道解决方案吗?
I've some trouble with the timer. My goal is to call it when MOUSE_OVER and to kill it when MOUSE_OUT.
Function to start timer:
public function timerStart():void {
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
}
Function to stop timer:
public function timerStop():void {
myTimer.stop();
}
Function to call timer:
public function rollOverHandler(e:MouseEvent = null):void
{
timerStart();
}
Function to call stop timer:
internal final function rollOutHandler(e:MouseEvent = null):void
{
timerStop(); //this one created the error message
}
Whatever I try, I keep getting this error message:
1120: Access of undefined property myTimer.
I understand the fact that he can't stop a timer which he doesn't recognize. But I am getting the error even before any mouseaction. What am I seeing wrong?
Does someone know a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 范围:您正在声明myTimer 作为局部变量。执行timerStart()后引用将被删除。
将其设为成员变量,一切都会正常工作。
哦,还有:在 rollOutHandler 中执行此操作:
以确保仅在设置了计时器时才调用它。
The problem is scope: You are declaring myTimer as a local variable. The reference will be deleted after timerStart() is executed.
Make it a member variable, and everything should work fine.
Oh, and also: Do this in the rollOutHandler:
to make sure it only gets called if a timer has been set.
谢谢韦尔特朗皮拉特!你的回答拯救了我的一天!
Thanks Weltraumpirat! Your answer just saved my day!