我正在使用三个课程,并且为了便于阅读而缩短了课程,省略了包装和进口。 MainClass
是我的文档类,Player
是一个链接到movieclip的类,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
静态,还是有一个简单的解决方法?这是不好的做法吗?
答案 0 :(得分:0)
这是我的评论
中的选项1TestMain
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")
}
}
}
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;
}
}
}
选项2正在进行“Singleton”类型的设置,您可以通过静态方法获取类的实例。在这种情况下,只使用该类的一个实例,并且不需要将实例传递给实际上不需要自己处理的对象。在我对OP I的评论中的链接中讨论了强制执行模式的选项,因此没有意外地创建新实例,我只是在这里使用了导致运行时错误的简单选项:
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")
}
}
}
TestMain
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();
}
}
这是强制执行Singleton的另一种方法,它会导致编译时错误,因此您可以在之前看到问题:
http://blog.pixelbreaker.com/actionscript-3-0/as30-better-singletons