闪存和Flex SDK/AS3 - 如何保持键盘焦点? [英] Flash & Flex SDK/AS3 - How to keep keyboard focus?

查看:16
本文介绍了闪存和Flex SDK/AS3 - 如何保持键盘焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Flex/AS3 中编写 Flash 应用程序,但我似乎无法为其分配键盘焦点.我在开发的早期就注意到了这个问题,并添加了一个带有立即播放"按钮的启动画面,以吸引用户点击.但是,用户必须在应用程序上再次单击才能使键盘工作!

I'm writing a flash application in Flex/AS3, and I can't seem to assign keyboard focus to it. I was mindful of this problem early on in development and added a splash screen with a "play now" button, to entice the user to click. However, the user must then click a second time on the application for the keyboard to work!

更糟糕的是,我有一个游戏内快捷方式,可以让您返回主菜单.如果您返回主菜单然后单击立即播放"按钮,SWF 将再次失去焦点!

To make matters worse, I have an in-game shortcut that returns you to the main menu. If you return to the main menu then click the "play now" button, the SWF loses focus again!

我可能弄乱了儿童对象或不小心破坏了捕获键盘焦点的对象,但我不太确定它是如何工作的.你能帮我指明正确的方向吗?

I'm probably messing up children objects or accidentally destroying an object that captured the keyboard focus, but I'm not quite sure how it works. Can you help point me in the right direction?

我的应用程序是单个 .SWF 文件,我直接在浏览器中运行它(不是通过网页调用它,但我最终会调用它).这是有问题的文件:

My application is a single .SWF file, and I am running it directly in my browser (not calling it via a webpage, though I will eventually). Here is the file in question:

http://www.space-squid.com/game/Main.swf当您单击正常"时,您必须再次单击才能真正抓住键盘焦点.:( 欢迎提问!

http://www.space-squid.com/game/Main.swf When you click on "Normal" you have to click a second time to actually grab the keyboard focus. :( Feel free to ask questions!

这是我正在使用的一些代码.

Here's some code I'm using.

在我的主类中执行的一些第一批代码:

Some of the first code that executes in my primary class:

empty_sprite = new Sprite();
addChild(empty_sprite);
empty_sprite.stage.addEventListener(keyboard hooks...);

我也试过了,以防万一我应该在我的根对象上设置钩子:

I've also tried this just in case I should have set the hooks on my root object:

this.stage.addEventListener(keyboard hooks...);

如果空精灵导致问题:

background_image = new BackgroundImage();
background_image.x = etc etc alignment data;
addChild(background_image);
background_image.stage.addEventListener(keyboard hooks...)

在所有这些示例中,我的键盘挂钩都可以正常工作,只要我第二次点击..但永远不会第一次.:(

In all of these examples my keyboard hooks work fine, as long as I click the second time.. but never the first. :(

第二次我已经缩小了问题的范围.也许有人可以帮我理顺这个问题,这可能是结构问题:

SECOND Well I've narrowed the problem down. Perhaps someone can help me straighten this out, it's probably a structure problem:

public function Main {
    Some stuff...
    empty_sprite = new Sprite(); // Create a new stage sprite
    addChild(empty_sprite);
    empty_sprite.stage.addEventListener(keyboard hooks...);        

    addChild(BackgroundImage); // I lay down my background image which is persistant

    addChild(PlayNowButton); // I display my PlayNow button to the screen

    More stuff...
}

public function StartGame() {
    removeChild(PlayNowButton); // This is the point of failure; this removes focus.
    removeChild(otherMenuOptions);
    ...
    addChild(gameComponents);
}

如您所见,我创建了立即播放"按钮 - 它似乎成为焦点对象.我的键盘事件没有被困住,因为它是寻找焦点的背景.不确定这是否有意义,希望有人可以解决这个问题!

As you can see, I create the play now button - and it appears that becomes the object of focus. My keyboard events aren't being trapped as it's the background that is looking for the focus. Not sure if that makes sense, hopefully someone can straighten me out with this!

如果我注释掉那一行 (removeChild(PlayNowButton)),游戏就可以正常运行并保持键盘焦点 - 缺点是playnow"按钮永远覆盖在屏幕上.

If I comment out that single line (removeChild(PlayNowButton)) the game works perfectly fine and retains keyboard focus - with the downside of having a "playnow" button overlaid on the screen forever.

老实说,我什至不确定游戏本身是否会在第一次点击上获得关注,但我不确定如何进行测试.

To be honest I'm not even sure if the game itself ever receives focus on the first click but I'm not sure how to test for that.

推荐答案

您要找的代码是:

gameWorldObject.stage.focus = this;

由于 Flash 已获得焦点,因此您的键盘事件处理程序根本没有在 Flash 应用程序本身内获得焦点.您可以使用上述代码切换当前焦点在应用程序中的位置.

Since Flash has obtained focus, your keyboard event handler is simply not focused within the flash application itself. You can switch where the current focus is within the application using the above code.

或者,与其破坏 PlayNow 按钮,不如让它不可见.然后在您需要时再次使其可见.如果是 MovieClip 或 Sprite 对象,则非常简单:

Alternatively, instead of destroying the PlayNow button, making it invisible will do the trick. Then make it visible again later when you need it to. Very easy if it's a MovieClip or a Sprite object:

PlayNowButton.visible = false; // or true obviously, as the case may be

(这样写自己很难)

这篇关于闪存和Flex SDK/AS3 - 如何保持键盘焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆