0

开发环境为FLASH BUILDER(Android/iOS应用)。我无法理解以下错误。请修复我的代码。错误:RangeError:错误 #1125:索引 2 超出范围 0。在 views::twoHomeView/sets()[C:\Users\Adobe Flash Builder 4.6\y\src\views\twoHomeView.mxml:162] 处views::twoHomeView/down_clickHandler()[C:\Users\Adobe Flash Builder 4.6\y\src\views\twoHomeView.mxml:115] 在 vi​​ews::twoHomeView/__down_click()[C:\Users\Adobe Flash Builder 4.6 \y\src\views\twoHomeView.mxml:203]

代码:

<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, 4);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
            go.visible=true;
        }
        var kaisu:int =0; 
        public function onTimer(event:TimerEvent):void
        {


                kaisu += 1;
                if(kaisu>=0)
                {
                    go.visible =false;
                }
                display(n[kaisu-1]);
                if(kaisu==5)
                {
                    kaisu = 0;
                }

        }


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


            function display(q:int):void{//矢印を表示させる 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 if(q ==3)
            {
                ue.visible= false;
                migi.visible= false;
                shita.visible= false;
                hidari.visible= true;
            }

            else 
            {
                ue.visible= false;
                migi.visible= false;
                shita.visible= false;
                hidari.visible= false;
            }

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

            display(3);
            sets(3);
        }

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

            display(0);
            sets(0);

        }

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


        protected function down_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            display(2);
            sets(2);
        }



        /*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//配列に乱数を入れ込む。
        {

            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];
        }


        public  var set:Vector.<int> = new Vector.<int>;
        public var points:int=0;
        public var nyuryoku:int =0;
        public var pan:int;

        function sets(q:int){

            if( n[nyuryoku]==set[q]){

        nyuryoku +=1;
        points += 1;
        pan = points;
            tensu.text = pan.toString();
        }
            else
            {
                end();
            }
        }



     function end(){
         bad.visible = true;
         points = 0;
         kaisu = 0;
         display(5);
     }



    ]]>
</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="↓"
          click="down_clickHandler(event)"/>
<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="100" y="53" smooth="true" source="assets/migi.png"/>
<s:Image id="good" x="-21" y="84" width="363" height="103" visible="false" source="assets/good.png"/>
<s:Image id="go" x="28" y="62" width="263" height="153" visible="false" source="assets/go.png"/>
<s:Label id="tensu" x="19" y="31" text="ラベル"/>
<s:Image id="bad" x="24" y="54" visible ="false" source="assets/bad.png"/>
4

1 回答 1

1

A range error means that you are trying to access a member of an Array or Vector, but the array or vector does not have a value at that index.

So for the array exampleArray = [1,2,3,4]

calling exampleArray[3] is valid because the array contains "4" at that index, but exampleArray[4] would generate an error because exampleArray does not contain a 5th index. (Reminder, array and vector indexes begin with zero, not one)

So when you call:

protected function down_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            display(2);
            sets(2);
        }

The line "sets(2)" causes a problem when the code in that function references set[2] since set does not contain 3 members.

于 2013-01-03T20:05:48.433 回答