计时器给出错误 AS3

发布于 2024-10-14 11:01:22 字数 927 浏览 1 评论 0原文

我的计时器有点问题。我的目标是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-10-21 11:01:22

问题是 范围:您正在声明myTimer 作为局部变量。执行timerStart()后引用将被删除。

将其设为成员变量,一切都会正常工作。

哦,还有:在 rollOutHandler 中执行此操作:

if (myTimer != null) timerStop();

以确保仅在设置了计时器时才调用它。

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:

if (myTimer != null) timerStop();

to make sure it only gets called if a timer has been set.

卸妝后依然美 2024-10-21 11:01:22

谢谢韦尔特朗皮拉特!你的回答拯救了我的一天!

public static var myTimer:Timer;
        public function timerStart():void {
             // 1 second
            myTimer = new Timer(1000, 1)
            myTimer.addEventListener(TimerEvent.TIMER, runOnce);
            myTimer.start();
        }

        public function timerStop():void {
            myTimer.stop();
        }


        public function rollOverHandler(e:MouseEvent = null):void 
        {

        timerStart();

Thanks Weltraumpirat! Your answer just saved my day!

public static var myTimer:Timer;
        public function timerStart():void {
             // 1 second
            myTimer = new Timer(1000, 1)
            myTimer.addEventListener(TimerEvent.TIMER, runOnce);
            myTimer.start();
        }

        public function timerStop():void {
            myTimer.stop();
        }


        public function rollOverHandler(e:MouseEvent = null):void 
        {

        timerStart();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文