1

我正在使用三个类,并且为了便于阅读而缩短了三个类,省略了包和导入。MainClass是我的文档类,Player是链接到影片剪辑KeyHandler的类,是我用来包含按键功能的类。首先,代码..

public class  TestMain extends Sprite
{
    public var keyHandler:KeyHandler = new KeyHandler();
    public var timer:Timer = new Timer(30);
    public var player:Player = new Player();

    public function TestMain()
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler.KeyPress);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler.KeyRelease);
        timer.addEventListener(TimerEvent.TIMER, onTick);
        timer.start();

        stage.addChild(player);
    }

    public function onTick(timerEvent:TimerEvent)
    {
        player.Move();
    }
}

玩家等级..

public class Player extends Sprite
{
    public var keyHandler:KeyHandler = new KeyHandler();

    public function Player()
    {
    }

    public function Move():void
    {
        trace("this works");
        if (keyHandler.upKeyIsPressed)
        {
            trace("this doesnt work")
        }
    }

}

KeyHandler 类..

public class KeyHandler extends Sprite
{
    public var upKeyIsPressed:Boolean = false;

    public function KeyHandler() 
    {
    }

    public function KeyPress(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = true;
        }
    }

    public function KeyRelease(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = false;
        }
    }

}

按住向上键不会跟踪任何内容,但Move函数会跟踪。我应该做KeyHandler静态,还是有一个简单的修复?这是不好的做法吗?

4

1 回答 1

0

这是我评论中的选项 1

测试主程序

public class  TestMain extends Sprite
{
    public var keyHandler:KeyHandler = new KeyHandler();
    public var timer:Timer = new Timer(30);
    public var player:Player = new Player();

    public function TestMain()
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler.KeyPress);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler.KeyRelease);
        timer.addEventListener(TimerEvent.TIMER, onTick);
        timer.start();
        player.keyHandler = keyHandler;
        stage.addChild(player);
    }

    public function onTick(timerEvent:TimerEvent)
    {
        player.Move();
    }
}

播放器

public class Player extends Sprite
{
    public var keyHandler:KeyHandler;

    public function Player()
    {
    }

    public function Move():void
    {
        trace("this works");
        if (keyHandler.upKeyIsPressed)
        {
            trace("this should be working now")
        }
    }

}

密钥处理程序

public class KeyHandler extends Sprite
{
    public var upKeyIsPressed:Boolean = false;

    public function KeyHandler() 
    {
    }

    public function KeyPress(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = true;
        }
    }

    public function KeyRelease(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = false;
        }
    }

}

选项 2 是进行“单例”类型的设置,您可以在其中通过静态方法获取类的实例。在这种情况下,只使用了该类的一个实例,并且不需要将实例传递给实际上不需要处理它本身的对象。在我对 OP 的评论中的链接中,我讨论了执行模式的选项,因此不会意外创建新实例,我只是在这里使用导致运行时错误的简单选项:

public class KeyHandler extends Sprite
{
    public var upKeyIsPressed:Boolean = false;
    public static var SINGLETON:KeyHandler = new KeyHandler();
    public static function getInstance():KeyHandler
    {
        return SINGLETON;
    }

    public function KeyHandler() 
    {
        if(SINGLETON)
            throw new Error("There can be only one!!!");
    }

    public function KeyPress(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = true;
        }
    }

    public function KeyRelease(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = false;
        }
    }

}

播放器

public class Player extends Sprite
{
    public var keyHandler:KeyHandler = KeyHandler.getInstance();

    public function Player()
    {
    }

    public function Move():void
    {
        trace("this works");
        if (keyHandler.upKeyIsPressed)
        {
            trace("this should be working now")
        }
    }

}

测试主程序

public class  TestMain extends Sprite
{
    public var keyHandler:KeyHandler = KeyHandler.getInstance();
    public var timer:Timer = new Timer(30);
    public var player:Player = new Player();

    public function TestMain()
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler.KeyPress);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler.KeyRelease);
        timer.addEventListener(TimerEvent.TIMER, onTick);
        timer.start();
        player.keyHandler = keyHandler;
        stage.addChild(player);
    }

    public function onTick(timerEvent:TimerEvent)
    {
        player.Move();
    }
}

这是另一种强制执行单例的方法,这会导致编译时错误,因此您可以在前面看到问题:

http://blog.pixelbreaker.com/actionscript-3-0/as30-better-singletons

于 2012-06-15T00:23:15.093 回答