为什么我的 KeyboardEvent 在我的游戏中不起作用?
我需要使用 flash as3 来创建游戏,并且我尝试使用 3 层来加载我的 swf。我的游戏在第三层,第一层和第二层只是预加载脚本。
我的问题是,当游戏加载到第一层的舞台上时,我的 KeyboardEvent
函数在我按下舞台之前无法工作。
我尝试使用 Event.ADDED_TO_STAGE 来解决它,但我也遇到了同样的错误。
这是我的预加载器代码
var request:URLRequest = new URLRequest("game.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
function loadProgress(event:ProgressEvent):void
{
var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
percentLoaded = Math.round(percentLoaded * 100);
//this.percentLoaded.text = String(uint(percentLoaded)) + "%";
}
function loadComplete(event:Event):void
{
trace("Load Complete");
}
loader.load(request);
this.addChild(loader);
这是我在时间线上的游戏脚本
if(stage != null) {
stageAddHandler(null);
} else {
addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
}
function stageAddHandler(e:Event = null):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
对此有什么想法吗?
感谢您的提前
I need to use flash as3 to create a game, and I have tried to use 3 layer to load my swf. My game is in the third layer, and the first and second layers are just a preloader script.
My problem is when the game is loaded onto the stage of first layer my KeyboardEvent
function is not work until I press the stage.
I have try to use Event.ADDED_TO_STAGE
to solve it, but I also get a same error.
this is my code for preloader
var request:URLRequest = new URLRequest("game.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
function loadProgress(event:ProgressEvent):void
{
var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
percentLoaded = Math.round(percentLoaded * 100);
//this.percentLoaded.text = String(uint(percentLoaded)) + "%";
}
function loadComplete(event:Event):void
{
trace("Load Complete");
}
loader.load(request);
this.addChild(loader);
And this is my game script on the timeline
if(stage != null) {
stageAddHandler(null);
} else {
addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
}
function stageAddHandler(e:Event = null):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
Any idea on this?
Thanks for advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Flash Player 的正常安全功能。用户必须首先单击以将焦点设置在舞台上才能接收键盘事件。
您可以设计解决此限制的方法,例如,让您的用户在文本字段中输入他们的姓名(他们必须首先用鼠标选择该姓名),或者让他们单击“开始游戏!”游戏开始前的按钮。
[编辑]
解决方法可能涉及通过 ExternalInterface 调用 javascript 函数 这会将焦点集中在您的嵌入式 swf 上。我还没有测试过这个,但是这个(或类似的东西)应该可以工作:
ActionScript:
JavaScript:
this is a normal, security feature of the Flash Player. users must first click to set focus on the stage in order to receive keyboard events.
you can design your way around this limitation, for example, by having your users enter their name in a text field, which they would have to first select with the mouse, or have them click a "Start Game!" button before the game starts.
[EDIT]
a work around could involve calling a javascript function via ExternalInterface which will set focus on your embedded swf. i haven't tested this but this (or something like this) should work:
ActionScript:
JavaScript:
存在三个潜在问题可能导致键盘事件无法按预期工作;
html 页面中的 Flash 对象未处于活动状态(即未处于焦点状态)。这可以通过上面的 javascript 解决方案来解决,但通常情况下,您将让用户在需要键盘输入之前单击应用程序中的某些内容。
您添加了 KeyboardEvent 的对象未获得焦点。要解决此问题,请将侦听器添加到舞台。
如果您使用“包装器”或“加载器”swf 在主应用程序中加载,并且您的第一次也是唯一一次点击是在包装器 swf 中,则您将遇到对象焦点问题,因此当您的主应用程序启动并添加KeyboardEvents后,将舞台的焦点对象设置为舞台(this.stage.focus = this.stage)。
There are three potential issues that can cause keyboard events to not work as expected;
The flash object within in the html page isn't active (ie not in focus). This could be solved via javascript solution above but generally, you'll have the user click something in an app before keybaord input is required.
The object you've added the KeyboardEvent to isn't in focus. To fix this, add the listener to the stage.
If you're using a 'wrapper' or 'loader' swf to load in your main application and your first and only click is in the wrapper swf, you'll have issues with object focus so when your main app inits and the KeyboardEvents are added, set the stage's focus object to be the stage (this.stage.focus = this.stage).