1

我有点闪现,但我现在正在编写游戏,我试图让我的角色移动,但我收到错误 #1009 这是我在“GameState”中的代码。基本上它在任何按键上都会出错,我的角色名为播放器(库中的播放器),并且其中有另一个名为 WalkDown 的电影剪辑(我在时间轴上给它一个实例名称 walkDown)我不太确定发生了什么上。具体来说,它在调用框架名称的行上出错。任何帮助,将不胜感激!包裹 {

import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.Point;

public class GameState extends MovieClip {

    private var player:MovieClip;
    private var walking:Boolean = false;


        // is the character shooting
        //private var shooting:Boolean = false;

        // wlaking speed
        private var walkingSpeed:Number = 5;
        private var xVal:Number = 0;
        private var yVal:Number = 0;




    public function GameState() {

        // constructor code
        player = new Player();
        addChild(player);
        player.x = 300;
        player.y = 300;
        player.gotoAndStop("stance");



    this.addEventListener(Event.ADDED_TO_STAGE, initialise);


    }
    private function initialise(e:Event){
        // add a mouse down listener to the stage
        //addEventListener(MouseEvent.MOUSE_DOWN, startFire);

        // add a mouse up listener to the stage
        //addEventListener(MouseEvent.MOUSE_UP, stopFire);
        player.addEventListener(Event.ENTER_FRAME,motion);
        stage.addEventListener(KeyboardEvent.KEY_UP,onKey); 
        // add a keyboard down listener
        stage.addEventListener(KeyboardEvent.KEY_DOWN, offKey);
        stage.focus = stage;

        // Add keyboard events


    }
    private function motion(e:Event):void{
        // if we are currently holding the mouse down
        //if (shooting){
            //FIRE
            //fire();

        //}
       player.x +=  xVal;
       player.y +=  yVal;

        }



    //private function startFire(m:MouseEvent){
        //shooting = true;
    //}
    //private function stopFire(m:MouseEvent){
        //shooting = false;
    //}
    private function onKey(evt:KeyboardEvent):void
    {
        trace("key code: "+evt.keyCode);
        switch (evt.keyCode)
        {
            case Keyboard.W :
               yVal =  walkingSpeed;

                if (! walking)
                {
                    trace("walking up");
                    player.walkDown.gotoAndPlay("walking");
                    walking = true;
                }
                break;
            case Keyboard.S :
                yVal = - walkingSpeed;
                if (! walking)
                {
                    player.walkDown.gotoAndPlay("walking");
                    walking = true;
                }
                break;
            case Keyboard.A :
            xVal = walkingSpeed;
                if (! walking)
                {
                     player.walkDown.gotoAndPlay("walking");
                    walking = true;
                }
                break;
            case Keyboard.D :
               xVal = walkingSpeed;
                if (! walking)
                {
                    player.walkDown.gotoAndPlay("walking");
                    walking = true;
                }
             break;
        }
    }
    private function offKey(evt:KeyboardEvent):void
    {
        switch (evt.keyCode)
        {
            case Keyboard.W :
                //for now just reset velocity to zero
                yVal = 0;
            //also stop walk cycle etc.
               player.gotoAndStop("stance");
                //don't forget to update your Boolean
                walking = false;
                break;
                 case Keyboard.S :
                //for now just reset velocity to zero
                yVal = 0;
            //also stop walk cycle etc.
               player.gotoAndStop("stance");
                //don't forget to update your Boolean
                walking = false;
                break;
                 case Keyboard.A :
                //for now just reset velocity to zero
                xVal = 0;
            //also stop walk cycle etc.
               player.gotoAndStop("stance");
                //don't forget to update your Boolean
                walking = false;
                break;
                     case Keyboard.D :
                //for now just reset velocity to zero
                xVal = 0;
                //also stop walk cycle etc.
               player.gotoAndStop("stance");
                //don't forget to update your Boolean
                walking = false;
                break;
        }
    }
    // Players Motion




    private function fire():void{
        var b= new Bullet();
        // set the position and rotation of the bullet
        b.rotation = rotation;
        b.x = x;
        b.y = y;
        // add bullets to list of bullets
        MovieClip(player).bullets.push(b);

        // add bullet to parent object
        player.addChild(b);
        // play firing animation        
        player.shooting.gotoAndPlay("fire");
    }


}


}
4

1 回答 1

0

您说的是错误 #1009(无法访问空对象引用的属性或方法),当您这样做时

player.walkDown.gotoAndPlay("walking");

如果是这样,那是因为您必须先转到 WalkDown 影片剪辑所在的帧,然后才能访问它。

如果说您的“姿势”帧在第 1 帧,而您的 WalkDown MovieClip 在第 2 帧。您在 onKey 函数中的代码应如下所示:

case Keyboard.W :
    yVal =  walkingSpeed;

    if (! walking)
    {
        trace("walking up");
        player.gotoAndStop(2); // player.walkDown is now accessible //
        player.walkDown.gotoAndPlay("walking");
        walking = true;
    }
    break;
于 2013-02-07T10:16:19.343 回答