(AS3)修复Frogger游戏ActionScript 3

时间:2013-04-06 17:15:49

标签: actionscript-3 flash

我有10个错误(1120)与未定义属性的访问相关联。 onEnterFrame,onClick,onLeftClick,MouseUp,onUpClick,MouseUp,onDownClick,MouseUp,onRightClick和MouseUp。

从.addEventListener fron第58行到第73行抛出错误

package 
{
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.events.Event;
    import flash.events.MouseEvent;


    public class FrogKatta extends MovieClip
    {
        // Create instances of library symbols 
        var car1:Car = new Car();
        var car2:Car = new Car();
        var truck1:Truck = new Truck();
        var truck2:Truck = new Truck();
        var frog:Frog = new Frog();
        var FinishLine:finishLine = new finishLine();
        var HealthBar:healthBar = new healthBar();
        var vx:int = 0;
        var vy:int = 0;

        var hit:Boolean = false;
        var carStart:Boolean = false;
        var truckStart:Boolean = false;



        public function frogKatta()
        {
            frog.x = 230;
            frog.y = 375;

            truck1.x = 545;
            truck1.y = 230;

            truck2.x = 560;
            truck2.y = 235;

            car1.x = 550;
            car1.y = 78;

            car2.x = 560;
            car2.y = 80;

            // add created instances to stage 
            addChild(frog);
            addChild(truck1);
            addChild(truck2);
            addChild(car1);
            addChild(car2);
            // * PROBLEM WITH STAGE.ADDEVENTLISTENER
            // Add event listener for keyboard 

// PROBLEM STARTS HERE ***********


            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);


            addEventListener(Event.ENTER_FRAME, onEnterFrame);

            // Add Event for mouse click 
            stage.addEventListener(MouseEvent.CLICK, onClick);
            // creating instance of onClick from the MouseEvent.CLICK
            //Keyboard 
            left.addEventListener(MouseEvent.MOUSE_DOWN, onLeftClick);
            // to move
            left.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
            // to stop 
            up.addEventListener(MouseEvent.MOUSE_DOWN, onUpClick);
            up.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
            down.addEventListener(MouseEvent.MOUSE_DOWN, onDownClick);
            down.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
            right.addEventListener(MouseEvent.MOUSE_DOWN, onRightClick);
            right.addEventListener(MouseEvent.MOUSE_UP, MouseUp);

//  PROBLEM ENDS HERE****


            // Keyboard functionality 
            function onKeyDown(event:KeyboardEvent):void
            {
                if (event.keyCode == Keyboard.LEFT)
                {
                    vx = -5;
                    frog.play();
                }
                else if (event.keyCode == Keyboard.RIGHT)
                {
                    vx = 5;
                    frog.play();
                }
                else if (event.keyCode == Keyboard.DOWN)
                {
                    vy = 5;
                    frog.play();
                }
                else if (event.keyCode == Keyboard.UP)
                {
                    vy = -5;
                    frog.play();
                }
            }

            // Keyboard again to stop 
            function onKeyUp(event:KeyboardEvent):void
            {
                if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
                {
                    vx = 0;
                    frog.stop();
                }
                else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
                {
                    vy = 0;
                    frog.stop();
                }

                // Clicking Functionality
                function MouseUp(event:MouseEvent):void
                {
                    vy = 0;
                    vx = 0;
                    frog.stop();
                }
                // on"librarybutton name left" Click
                function onLeftClick(event:MouseEvent):void
                {
                    vx = -5;
                    frog.play();
                }
                function onUpClick(event:MouseEvent):void
                {
                    vy = -5;
                    frog.play();
                }
                function onDownClick(event:MouseEvent):void
                {
                    vy = 5;
                    frog.play();
                }
                function onRightClick(event:MouseEvent):void
                {
                    vx = 5;
                    frog.play();
                }


                // on enter frame 
                function onEnterFrame(event:Event):void
                {
                    frog.x +=  vx;
                    frog.y +=  vy;

                    truck1.x +=  -10;
                    car1.x +=  -20;


                    //Car1 & 2 SCREEN WRAPPING 
                    if (car1.x <= -400)
                    {
                        car1.x = 550;
                    }
                    // If car 1 leaves screen add car to other side of the screen

                    if (car2.x <= -400)
                    {
                        car2.x = 550;
                    }

                    if (car1.x <= 135)
                    {
                        carStart = true;
                    }
                    // Screen wrap so if car is less than or equal to 135 in distance add another car 

                    if (carStart)
                    {
                        car2.x +=  -15;
                    }

                    //Truck1 & 2 
                    if (truck1.x <= -400)
                    {
                        truck1.x = 550;
                    }

                    if (truck2.x <= -400)
                    {
                        truck2.x = 550;
                    }


                    if (truck1.x <= 135)
                    {
                        truckStart = true;

                    }

                    if (truckStart)
                    {
                        truck2.x +=  -10;

                    }
                    if (frog.hitTestObject(truck1) || frog.hitTestObject(truck2) || frog.hitTestObject(car1) || frog.hitTestObject(car2))
                    {
                        frog.y +=  10;
                        hit = true;

                    }
                    if (hit)
                    {
                        if (HealthBar.width < 10)
                        {
                            HealthBar.width = 0;
                        }
                        else
                        {
                            HealthBar.width -=  10;
                        }
                    }

                    if (HealthBar.width < 1)
                    {
                        //S.O.P you lose);
                    }




                    if (frog.hitTestObject(FinishLine))
                    {

                        removeChild(frog);
                        removeChild(FinishLine);
                        removeChild(car1);
                        removeChild(car2);
                        removeChild(truck1);
                        removeChild(truck2);
                        // addchild of you winning

                    }

                }
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我相信你有一个括号问题。

在你的onKeyDown函数之前你需要一个'}'。

检查所有括号,在插入我告诉你的那个之后,你可能还会在文件的末尾添加一个。

这些行是个问题,因为代码中不存在上/下/左/右。如果它们是舞台实例,则它们可能未命名,因此您的代码将无法使用它们。如果是这种情况,请通过属性面板命名实例。

    left.addEventListener(MouseEvent.MOUSE_DOWN, onLeftClick);
    // to move
    left.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
    // to stop 
    up.addEventListener(MouseEvent.MOUSE_DOWN, onUpClick);
    up.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
    down.addEventListener(MouseEvent.MOUSE_DOWN, onDownClick);
    down.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
    right.addEventListener(MouseEvent.MOUSE_DOWN, onRightClick);
    right.addEventListener(MouseEvent.MOUSE_UP, MouseUp);

如果不关闭onKeyUp功能,还会遇到另一个支架问题。

这导致它认为你的所有事件处理程序都在该函数中。

但正如我在评论中所说,这不是一个调试您的应用程序论坛。

请按照我的要求花时间验证您的括号。当你没有花时间去检查那些被发现是一个问题的东西时,它并没有表现出对某人时间的太多欣赏。请花点时间验证每个功能的所有括号,这样可以节省很多时间。