操作脚本 3.0 错误:无法访问空对象引用的属性或方法



错误 #1009:无法访问空对象引用的属性或方法。 在方法信息-10((。我在运行此代码时收到此错误。谁能告诉我我做错了什么。它没有向我显示错误在哪里,也没有显示错误的行。谢谢

package 
{
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    public class Main extends MovieClip
    {
        public function Main()
        {
            stop();
            start_btn.addEventListener(MouseEvent.CLICK, gothere);
            function gothere(clickInfo:MouseEvent)
            {
                start_btn.removeEventListener(MouseEvent.CLICK, gothere);
                nextFrame();
            }
            stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
            function key_pressed(event:KeyboardEvent):void
            {
                switch (event.keyCode)
                {
                    case Keyboard.UP :
                        {
                            player.y +=  -5;
                            if ((wall1.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)))
                            {
                                player.y +=  5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback);
                                function goback(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback);
                                    prevFrame();
                                }

                            }
                            else if ((wall1.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)))
                            {
                                player.y +=  5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback1);
                                function goback1(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback1);
                                    prevFrame();
                                }
                            }
                            else if (player.hitTestObject(finish))
                            {
                                gotoAndStop(4);
                            }
                            break;


                        };
                    case Keyboard.DOWN :
                        {
                            player.y +=  5;
                            if ((wall1.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)))
                            {
                                player.y +=  -5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback2);
                                function goback2(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback2);
                                    prevFrame();
                                }
                            }
                            else if ((wall1.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)))
                            {
                                player.y +=  -5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback3);
                                function goback3(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback3);
                                    prevFrame();
                                }
                            }
                            else if (player.hitTestObject(finish))
                            {
                                gotoAndStop(4);
                            }
                            break;


                        };
                    case Keyboard.LEFT :
                        {
                            player.x +=  -5;
                            if ((wall1.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)))
                            {
                                player.x +=  5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback4);
                                function goback4(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback4);
                                    prevFrame();
                                }
                            }
                            else if ((wall1.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)))
                            {
                                player.x +=  5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback5);
                                function goback5(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback5);
                                    prevFrame();
                                }
                            }
                            else if (player.hitTestObject(finish))
                            {
                                gotoAndStop(4);
                            }
                            break;


                        };
                    case Keyboard.RIGHT :
                        {
                            player.x +=  5;
                            if ((wall1.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)))
                            {
                                player.x +=  -5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback6);
                                function goback6(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback6);
                                    prevFrame();
                                }
                            }
                            else if ((wall1.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)))
                            {
                                player.x +=  -5;
                                nextFrame();
                                playagain_btn.addEventListener(MouseEvent.CLICK, goback7);
                                function goback7(event:MouseEvent):void
                                {
                                    playagain_btn.removeEventListener(MouseEvent.CLICK, goback7);
                                    prevFrame();
                                }
                            }
                            else if (player.hitTestObject(finish))
                            {
                                gotoAndStop(4);
                            }
                            break;

                    }
                }

            }
        }
    }
};

首先让你的代码不那么严重地超重重复。另外,函数内部定义的函数在函数内部定义?在很多层面上都是错误的。

package
{
    import flash.ui.Keyboard;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    public class Main extends MovieClip
    {
        public function Main()
        {
            stop();
            start_btn.addEventListener(MouseEvent.CLICK, goThere);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
        }
        private function goBack(e:MouseEvent):void
        {
            playagain_btn.removeEventListener(MouseEvent.CLICK, goBack);
            prevFrame();
        }
        private function goThere(clickInfo:MouseEvent):void
        {
            start_btn.removeEventListener(MouseEvent.CLICK, gothere);
            nextFrame();
            stage.focus = stage;
        }
        private function testPlayer():Boolean
        {
            if (wall1.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true) return true;
            if (wall2.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true) return true;
            if (wall1.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true) return true;
            if (wall2.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true) return true;
            return false;
        }
        private function movePlayer(dx:Number, dy:Number):void
        {
            player.x += dx;
            player.y += dy;
            if (player.hitTestObject(finish))
            {
                gotoAndStop(4);
            }
            else if (testPlayer())
            {
                player.x -= dx;
                player.y -= dy;
                nextFrame();
                playagain_btn.addEventListener(MouseEvent.CLICK, goBack);
            }
        }
        private function onKey(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.UP:
                    movePlayer(0,-5);
                    break;
                case Keyboard.DOWN:
                    movePlayer(0,5);
                    break;
                case Keyboard.LEFT:
                    movePlayer(-5,0);
                    break;
                case Keyboard.RIGHT:
                    movePlayer(5,0);
                    break;
            }
        }
    }
}

最新更新