0

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

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

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

                    }

                }
            }

        }
    }
}
4

1 回答 1

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 函数时,您还有另一个括号问题。

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

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

请按照我的要求花时间验证您的括号。当您没有花时间检查已发现有问题的事情时,它不会对某人的时间表示太多赞赏。请花时间验证每个功能的所有括号,您将节省大量时间。

于 2013-04-06T17:35:15.170 回答