将敌人和典当添加到舞台的最佳方法?

时间:2013-12-26 15:05:42

标签: actionscript-3 flash class

我正在尝试开发简单的典当(玩家)和敌人类,但有2个故事,每个都有他们的问题。

首先:当pawn和敌人被添加到文档类中的代码时,我有利于按实例名称使用它们,我用它来添加它们,比如命中测试(我猜)

第二:当我将它们从库中拖到舞台时将它们添加到舞台上。通过这种方式,我可以将事件监听器添加到它们内部的阶段(用于移动),但是访问它们会很困难,有时候null阶段是我最大的问题。

任何让事情变得轻松的建议?

例如在下面的pawn类中我向舞台添加了键盘监听器,但是当我通过添加子句将此pawn添加到舞台时,我可以访问null ...错误

package  {

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

public class pwn extends MovieClip {
    public var upPressed:Boolean = false;
    public var downPressed:Boolean = false;
    public var leftPressed:Boolean = false;
    public var rightPressed:Boolean = false;
    public var lastMove:String;
    public static var spd:Number=6;



    public function pwn() {
        addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);


    }
    //Movement
    public function fl_MoveInDirectionOfKey(event:Event){
        if (leftPressed){
            x -= spd;
            if(currentFrame < 18 || currentFrame > 24){
                gotoAndPlay(17);
            }
        }else
        if (rightPressed){
            x += spd;
            if(currentFrame < 26 || currentFrame > 31){
                gotoAndPlay(25);
            }
        }else
        if (upPressed){
            y -= spd;
            if(currentFrame < 10 || currentFrame > 16){
                gotoAndPlay(9);
            }
        }else
        if (downPressed){
            y += spd;
            if(currentFrame < 2 || currentFrame > 8){
                gotoAndPlay(1);
            }
        }
    }
    public function fl_SetKeyPressed(event:KeyboardEvent):void{
        switch (event.keyCode){
            case 87:
            case 38:{
                upPressed = true;
                break;
            }
            case 83:
            case 40:{
                downPressed = true;
                break;
            }
            case 65:
            case 37:{
                leftPressed = true;
                break;
            }
            case 68:
            case 39:{
                rightPressed = true;
                break;
            }
        }
    }
    public function fl_UnsetKeyPressed(event:KeyboardEvent):void{
        switch (event.keyCode){
            case 87:
            case 38:{
                upPressed = false;
                break;
            }
            case 83:
            case 40:{
                downPressed = false;
                break;
            }
            case 65:
            case 37:{
                leftPressed = false;
                break;
            }
            case 68:
            case 39:{
                rightPressed = false;
                break;
            }
        }
    }
}
}

你在这些情况下做什么?

1 个答案:

答案 0 :(得分:1)

这个问题有两个解决方案。首先让我解释一下,当您创建一个新的显示对象并且它尚未添加到舞台或显示列表中的任何对象时,它对该舞台的引用为空。所以这样做的一种方法是在pawn类构造函数中传递对stage的引用:

public function pwn(stageRef:Stage) {
    addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
    stageRef.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
    stageRef.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
}

第二种方法是使用ADDED_TO_STAGE事件:

public function pwn() {
    addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
    this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(e:Event):void {
    stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
    stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
}