错误1009处理

时间:2015-12-14 15:00:23

标签: actionscript-3 error-handling

我想在测验中进行反击。在我的测验中,在结束时它将转到另一帧。确切地说,时间大约是10分钟。在这段代码中,我只需写入31秒即可使其变得简单。 这是我的代码

import flash.events.*;
import flash.utils.Timer;
import flash.utils.getTimer;

stop()

var totSec:int = 31;
var totTime:Number = 1000 * totSec;

var secTimer:Timer = new Timer(1000,totSec);
secTimer.start ();
secTimer.addEventListener (TimerEvent.TIMER, updateClock);

function updateClock (t:TimerEvent) {
    var timePassed:int = totTime - getTimer();
    var second:int = Math.floor(timePassed/1000);
    var minute:int = Math.floor(second/60);
    //trace ("second : " + second);
    second %= 60;
    var sec:String = "";
    sec = String(second);

    if (second < 10)
    {
        sec = "0" + second;
    }
    var showTime:String = minute + " : " + sec;
    timeDisplay.text = String(showTime)
    if (minute == 0 && second == 0 )
    {
        gotoAndPlay(525);
        //addEventListener (Event.ENTER_FRAME, stopTime);
        trace ("Times up");
        secTimer.start ();

    }
}

但是,当帧转到第525帧时,我收到此错误

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at adminserver/updateClock()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

2 个答案:

答案 0 :(得分:0)

我认为为了避免这个错误,你应该在转到另一个帧之前停止model.addAttribute(WebUtils.INCLUDE_FRAGMENT_NAME, WebUtils.MAIN_OVERVIEW_DIV_INCLUDE);

Timer

希望可以提供帮助。

答案 1 :(得分:0)

问题:

执行以下操作后,您的计时器实际上再次打勾:

    gotoAndPlay(525);
    //addEventListener (Event.ENTER_FRAME, stopTime);
    trace ("Times up");
    secTimer.start();  <---------not sure what this is about?

所以当它再次打勾时,你实际上已经进入了另一个框架(525),你的timeDisplay文本字段不再存在(大概) - 所以代码错误。

更改帧时,附加到侦听器的代码会保持运行,即使该代码不在当前帧上

最干净的解决方案

使用实际的计时器事件,而不是自己动手来计算定时器的时间:

secTimer.addEventListener(TimerEvent.TIMER_COMPLETE, .....

这样,您知道计时器在代码运行之前完成并且您更改了帧。

以下是一个完整的示例,以及一些提示:

        stop();

        var totSec:int = 31;

        //no need for this var, total time in your case is secTimer.repeatCount * secTimer.delay
        //var totTime:Number = 1000 * totSec;

        var secTimer:Timer = new Timer(1000,totSec);

        secTimer.addEventListener(TimerEvent.TIMER, updateClock);
        //listen for the complete event when the timer is all done
        secTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
        secTimer.start();

        function updateClock (e:TimerEvent) {
            //the timer's currentCount will be how many times the timer has ticked, 
            //which in this case will be seconds elapsed. 
            //If you subtract that from the total repeat count, you'll get the seconds left, 
            //no need to use getTimer, which is now allows you to pause your timer if you'd like (can't pause using getTimer)
            var second:int = secTimer.repeatCount - secTimer.currentCount;

            var minute:int = Math.floor(second/60);
            //trace ("second : " + second);
            second %= 60;
            var sec:String = String(second);

            if (second < 10){
                sec = "0" + second;
            }
            var showTime:String = minute + " : " + sec;
            timeDisplay.text = String(showTime);
        }

        function timerComplete(e:TimerEvent):void {
                trace ("Times up");

                //secTimer.start();  //you don't really want to start the timer again - which does nothing anyway without a secTimer.reset() first
                //you should also remove the listeners on the timer so it can freed for garbage collection
                secTimer.removeEventListener(TimerEvent.TIMER, updateClock);
                secTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);

                gotoAndPlay(525);
        }