AS3错误1009 root = null

时间:2013-11-09 00:21:14

标签: actionscript-3 flash actionscript

我不太了解ActionScript。如果这很简单,那么道歉。

我已经写了一个.as文件作为其中的一部分我需要能够使用.stop()停止flash动画。

我尝试过几种不同的方法来实现这一点,其中一些方法可行 - 但是他们总是通过这个错误:

  

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在com.albionpreview :: looptimer / startFrame()at   test_fla :: MainTimeline /帧1()

以下是我尝试的所有内容:

this.stop();
root.stop();
MovieClip(root).stop();
MainTimeLine(parent).stop();
MovieClip(parent).stop();
parent.stop();
DisplayObject(stage).stop();
MainTimeline(parent).stop();
Main(root).stop();
Object(root).stop();
this.stage.stop();
MovieClip(this).stop();
MovieClip(this.root).stop();
(root as MovieClip).stop();

必须有一些简单的尝试。有人可以帮忙吗?

如果有帮助,这是我的代码。

http://pastebin.com/HSHCdyr1

1 个答案:

答案 0 :(得分:1)

在新图层上,我会创建一个新的movieClip,它将成为帧的容器并控制通过计时器停止。

只需绘制一个仅填充颜色(无边框)的框。相同宽度&作为舞台的高度,与Top&左(在修改 - >与&#34对齐;对齐到舞台"勾选)。它现在应该是舞台区域的完美契合,然后将其转换为movieClip(确保注册设置为顶部/左侧的黑点..不是居中的点)。

调用它你想要的,但给它animMC的实例名称(你可以稍后更改)。

现在选择动画帧的整个范围,然后选择"剪切帧"在右键菜单上。双击animMC movieClip进入其时间轴,删除框形状并右键单击第一帧,然后粘贴框架#34;。

现在在你的代码中你可以像这样停止它:

package com.preview {

import flash.utils.Timer;
import flash.events.TimerEvent     
import flash.utils.getTimer;
import flash.display.MovieClip;

public class looptimer extends MovieClip
{

var startTime:int = 0;
var allowedTime:int = 5000;
var loopTime:int = 0;


    public function looptimer () 
    {
        var myTimer:Timer = new Timer(5000, 1); //count before doing function "timerListener"  the 1 means do once else it will loop forever
        myTimer.addEventListener(TimerEvent.TIMER, timerListener);
        myTimer.start();

        function timerListener (e:TimerEvent):void
        {
        trace("Timer countdown finished");
        animMC.stop(); //anim stops
        }

    }

}

}

========================

编辑2:

您好此代码将检查您在动画结束时触发的事件。要让这样的事件进入包含框架的MC,并在最后一帧(和空白图层)上添加此动作代码:(这将是

的事情
dispatchEvent(new Event("Banner_end)")); //end frame

这相当于获得你的" end Frame"。在下面的代码中,事件附加到此指令:

  

animMC.addEventListener(" Banner_end",check_Time);

意味着在到达结束帧时会发生一个名为" Banner end"从时间轴发送,这反过来执行check_Time函数和lso另一个函数(见代码)。

现在,下面的代码将检查时间以及"结束帧"调度事件并确定是否有足够的时间进行第二次循环或更多循环。两件事会影响MC循环的次数。首先是时间限制越长,你越循环,你将适应这个限制。第二个是动画MC的长度,如果较短的手段可以在时间限制内适应更多的循环。调整这两件事。最简单的是时间限制,只需更改数字(" allowed_playing_Time")进行测试,但稍后当您无法控制限制时,则根据需要调整横幅(动画MC)帧,即添加/删除帧。

package com.preview 
{
    import flash.utils.*;
    import flash.events.*;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.utils.getTimer;
    import flash.display.MovieClip;
    import flash.events.Event;

    public class looptimer extends MovieClip
    {


        var allowed_playing_Time:int = 10; //time limit in seconds
        var play_Count:int = 0; //tracking number of times played
        var can_play_Amount:int = 1; //total times can be played

        var _startTime:Date;//Number;
        var _endTime:Date;//Number;

        //for time check using actual [computer] clock = more accurate than timer
        var real_Time:Date;
        var anim_start_time:int;
        var anim_end_time:int;
        var anim_play_length:int;

        public function looptimer()
        {
            //starting the Banner
            play_Banner_anim();
        }

        function play_Banner_anim():void
        {
            animMC.stop(); //set strating banner state as paused;

            _startTime = real_Time = new Date();//Real Time value initial set here
            trace("current time  at START is: " + _startTime.minutes + " mins past and " + _startTime.seconds + " secs");

            animMC.addEventListener("Banner_end", check_Time);
            animMC.addEventListener("Banner_end", calculate_Replay);
            anim_start_time = getTimer();
            animMC.play();
        }

        function check_Time(e:Event):void
        {
            animMC.stop(); trace( "Banner has reached last frame");
            animMC.removeEventListener("Banner_end", check_Time);
            play_Count = play_Count + 1;
            trace("Times banner has played / looped): " + play_Count);


            _endTime = real_Time = new Date();//new update of Real Time value
            trace("current time at END is: " + _endTime.minutes + " mins past and " + _endTime.seconds + " secs");

            anim_end_time = getTimer();
            anim_end_time = Math.ceil(anim_end_time);
            anim_play_length = Math.round(Math.floor(anim_end_time - anim_start_time) / 1000);

            anim_play_length = anim_play_length;

            trace("checking if Real Time: ");
            var date1:Date = _startTime;
            var date2:Date = _endTime;

            var elapsed1:Number = Date.UTC(date1.minutes,date1.seconds);
            var elapsed2:Number = Date.UTC(date2.minutes,date2.seconds);

            var playingTime:Number = Math.round( (elapsed2/1000) - (elapsed1/1000) );
            trace ("time elasped (delta) is "  +  playingTime );


            //check seconds elapsed in banner playback (i.e total play length)
            trace("Banner total playing length was " + anim_play_length + " seconds");

            //so how many times can banner play within allowed time limit?
            var can_fit_Num:Number = allowed_playing_Time / anim_play_length;

            trace("can fit this many plays in allowed time limit: " + can_fit_Num);
            can_play_Amount = can_fit_Num;
            trace("can_play_Amount is : " + can_play_Amount);
            if (can_play_Amount <= 0)
            {
                trace("Warning: Banner Anim is either longer than allowed time limit or has a play length of zero");
            }

        }

        function calculate_Replay(e:Event):void
        {
            trace("Calculating replay function :::::: ");
            trace("Play/looped amount is: " + play_Count);
            trace("Can play amount times is : " + can_play_Amount);

            if (play_Count < can_play_Amount)
            {
                trace("anim_play_length is:" + anim_play_length);
                replay_tings();
                trace("replaying anim");
            }
            else
            {
                animMC.stop();
                trace("reached replaying times (" + can_play_Amount + ") available within limit (" + allowed_playing_Time + " secs)");
                if (can_play_Amount <= 0)
                { trace("Warning: Banner Anim is either longer than allowed time limit or has a play length of zero"); }

                //do something else. Currently just stops since no time for replay
            }

        }

        function replay_tings():void
        {

            animMC.addEventListener("Banner_end", calculate_Replay);
            animMC.play();
            play_Count = play_Count + 1;
            trace("Play Count/loops: " + play_Count);

        }

    }

}

希望它有所帮助。我提出了许多痕迹只是为了给你反馈。删除任何您认为不必要的内容,当然,如果发现某些内容,您可以优化代码。