Flash和放大器;的Flex SDK / AS3 - 如何保持键盘焦点?放大器、键盘、焦点、Flash

2023-09-08 12:38:40 作者:吧唧一口甜甜

我写的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!

编辑:下面是一些code我用

Here's some code I'm using.

有些是执行我的主类中的第一个code:

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...);

在情况下,否则空的Sprite是造成问题:

In case the otherwise empty sprite was causing issues:

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.

推荐答案

您正在寻找的code是:

The code you are looking for is:

gameWorldObject.stage.focus = this;

由于Flash获得焦点,键盘事件处理程序根本就不是专注于Flash应用程序本身。您可以切换,其中目前的重点是使用上述code中的应用程序中。

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.

另外,。然后让它以后再次可见,当你需要它。如果它是一个影片剪辑或者一个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

(这是很难写我自己这样)

(It's hard writing to myself this way)