views:

375

answers:

5

I'm creating a DDR-like game for an assignment and my keyboard seems to respond and trace it, but only if I press on the screen with the mouse first.

How can I get rid of that and just have it respond right away?

  • right_mc is the arrow that's moving
  • ArrowRight_mc is the arrow at the top
  • perfect_mc should pop up briefly
  • and so should a glowing arrow where it hits.

    if(rightDown){
        trace("right arrow");
        if(right_mc){
            if(right_mc.y >= ArrowRight_mc.y){
                perfect_mc.visible = true;
                glowRight_mc.visible = true;
            }
        }
    }
    
A: 

The issue here is that the embedded SWF file does not have focus when the screen first loads. You can assign focus to it using JavaScript but in my experience, this does not always work 100% of the time because of variations in the way browsers interpret JS. What a lot of people do is have a big START button following the load of the game so that players have to click on the SWF to begin playing. Some sites, even use JS to detect when the game has lost focus and will pause the game and alert the user.

I suppose I haven't exactly answered your question because I'm not that great at JavaScript but I hope this points you in the right direction.

Mims H. Wright
FYI: you can also fire an event on the flash client when it's loosing focus, no need to use javascript for that.
Tom
+4  A: 

This has been a long standing issue for Flash developers. Flash needs keyboard focus before it can detect keyboard events.

The problem is that the browser does not give focus to the SWF until the user clicks somewhere inside the SWF. This does make sense though. I don't want the web page I am on to lose focus just because there is a flash movie embedded somewhere. This is a security feature, to stop things like Flash banner ads being silent key loggers. However there are some instances that it makes sense to force the focus e.g. a Flash game where its the only thing on the HTML page.

Usually the best thing to do is have start menu screen with a "play" button. This forces the user to click on the SWF without even knowing about this "focus issue".

There is more info at the Adobe Technote - Giving keyboard focus to an embedded Flash movie.

*EDIT** Whether the Flash has focus or not, only affects keyboard events. It will not affect code from running, movieclips from playing, or sounds/video from playing.

TandemAdam
A: 

Thanks guys! That definitely explains it and makes sense.

The only thing that doesn't... is the fact that my songs don't start until you mouse click on the level button that you want.

May
Belongs as a **Edit** to the original Question or a **Comment**, not an independent **Answer** -- StackOverflow does not maintain ordering between answers, please don't use them for responses to other answers.
ephemient
This is a separate issue. Probably unrelated to the focus issue. To test, just click anywhere in the flash (not on the "level" button).
TandemAdam
A: 

In response to your comment... I'm unclear on something. If you have to click to start the song then you've already clicked on the SWF and you should be getting keyboard events, right? So if you're having to click to start then click again, maybe you need to make sure your mouse listener is at the root of your display list.

// in your document class / main AS file...
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

Or perhaps you need to poll for input during an EnterFrame loop rather than listen for key events.

Mims H. Wright
the following four are all in the constructor.home_btn.addEventListener(MouseEvent.CLICK, home);menu_btn.addEventListener(MouseEvent.CLICK, menu; stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);so I'm not sure what's causing it. You're already clicking once to get to the menu. Then you're clicking again one of 5 buttons.Much appreciated!
May
p.s. sorry for the format. no idea how to fix it.
May
A: 

I know what you mean. You have a specific object on the stage that needs focus before keyboard events will trigger. I'm having the same issues with a game here. A sprite needs to move with the keyboard arrows but the container needs focus so it will respond. I'm just setting the object I want to move to be tabEnabled as my requirements are only for accessibility purposes so tabbing to the object first will give it keyboard control.

Lee Probert