0

动作脚本

我在使用计时器功能时遇到问题。

我想做跟随程序。第一:生成随机数并放置数组。第二:通过存储的数组以3秒的间隔执行功能。

但是在使用定时器功能时,我不能使用应该执行间隔 3 秒的功能参数。

错误消息: RangeError:错误 #1125:索引 7 超出范围 4。在 views::twoHomeView/onTimer()[C:\Users\Kinect\Adobe Flash Builder 4.6\yattah\src\views\twoHomeView.mxml: 26] 在 flash.utils::Timer/_timerDispatch()

在 flash.utils::Timer/tick()

    <?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[

        import flash.display.Sprite;
        import flash.events.TimerEvent;
        import flash.utils.Timer;


        private var timer:Timer;

        public function Main()
        {
            timer = new Timer(1000, 3);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
        }
        var kaisu:int =0; 
        private function onTimer(event:TimerEvent):void
        {
        for(var i:int;i<n.length;i++){
            kaisu += 1;
            display(n[kaisu]);
        }
        }


        public  var n:Vector.<int> = new Vector.<int>(4);   

        public var a:int =0;
        public var s:int =0;


            function display(q:int):void{//show the picture(ue,migi,shita,hidari) 0=↑1=→2=↓3=←
            if(q ==0)
            {
                ue.visible= true;
                migi.visible= false;
                shita.visible= false;
                hidari.visible= false;
            }
            else if(q ==1)
            {

                ue.visible= false;
                migi.visible= true;
                shita.visible= false;
                hidari.visible= false;
            }
            else if(q ==2)
            {
                ue.visible= false;
                migi.visible= false;
                shita.visible= true;
                hidari.visible= false;
            }
            else
            {
                ue.visible= false;
                migi.visible= false;
                shita.visible= false;
                hidari.visible= true;
            }
                }
        protected function button1_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub

            display(0);
        }

        protected function button2_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub

            display(0);


        }

        protected function right_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            display(1);
        }





        function wait(time:int, handler:Function):void

        {

            var timeri:Timer = new Timer(time, 1);

                timeri.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);

                timeri.start();

            function timerHandler(event:TimerEvent):void

            {

                    handler();

                    timeri.removeEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);

            }

        }   
        function lv1():void{
    }

        function showMC():void//put random number in array
        {

            for(var i:int;i<n.length;i++)
            {   var r:int = Math.floor(Math.random() * 4);
                n[i]=r;
            }   
            point.text = n[0]+""+" "+n[1]+" "+n[2]+" "+n[3];
        }








    ]]>
</fx:Script>

<fx:Declarations>
</fx:Declarations>


<s:Image x="-31" y="-40" width="383" height="486" source="assets/yattah.jpg"/>
<s:Button id="left" x="13" y="304" width="50" height="50" label="←"
          click="button1_clickHandler(event)"/>
<s:Label id ="point" x="19" y="7" text="点"/>
<s:Button id="up" x="60" y="257" width="50" height="50" label="↑"
          click="button2_clickHandler(event)"/>
<s:Button id="right" x="107" y="304" width="50" height="50" label="→"
          click="right_clickHandler(event)"/>
<s:Button id="down" x="59" y="352" width="50" height="50" label="↓"
        />
<s:Button x="260" y="309" width="50" height="50" label="A" click="Main()"/>
<s:Button x="207" y="359" width="50" label="B" click="showMC()"/>
<s:Image id="ue" x="75" y="33" visible="false" smooth="true" source="assets/ue.png"/>
<s:Image id="shita" x="80" y="49" smooth="true" source="assets/shita.png"/>
<s:Image id="hidari" x="75" y="55" smooth="true" source="assets/hidari.png"/>
<s:Image id="migi" x="80" y="49" smooth="true" source="assets/migi.png"/>
<s:Image id="good" x="-21" y="84" width="363" height="103" visible="false" source="assets/good.png"/>

4

1 回答 1

0

好吧,您正在创建一个Vector包含 4 个元素的元素:

public  var n:Vector.<int> = new Vector.<int>(4)

然后在您的计时器事件处理程序中,您从 0 迭代到 5 ...所以您得到了越界错误:

for(var i:int;i<5;i++) {
            kaisu += kaisu+1;
            display(n[kaisu]);
        }

kaisu这会因您以有趣的方式递增的成员变量而变得更加复杂。我相信这会导致在到达 for 循环结束之前发生错误。您可能应该将此变量设为局部变量(在onTimer()函数中声明它,而不是在函数外部声明)......并像这样递增它:

kaisu += 1; // or kaisu++;

最后,如果您查看您的wait()方法,您正在声明一个本地Timer对象。当wait()函数完成时,该计时器可能会被垃圾收集,并且您可能永远不会从该计时器对象中获得任何事件。要解决这个问题,只需将声明移到函数timeri外部wait()(使其成为成员变量)。这与错误无关,但以后可能会成为问题。

于 2013-01-02T17:08:58.767 回答