wyman
wyman

Reputation: 289

Why my KeyboardEvent doesn't work in my game?

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

Upvotes: 0

Views: 1158

Answers (2)

Pete Hutch
Pete Hutch

Reputation: 11

There are three potential issues that can cause keyboard events to not work as expected;

  1. 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.

  2. The object you've added the KeyboardEvent to isn't in focus. To fix this, add the listener to the stage.

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

Upvotes: 1

Chunky Chunk
Chunky Chunk

Reputation: 17237

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:

package
{
    //Imports
    import flash.display.Sprite;
    import flash.external.ExternalInterface;

    //Class
    public class AS3DocumentClass extends Sprite
    {
        //Constructor
        public function AS3DocumentClass()
        {
            init();
        }

        //Initialize
        private function init():void
        {
            var extInterface = new ExternalInterface();
            extInterface.call("initSwfFocus");
        }
    }
}

JavaScript:

<script type="text/javascript" language="javascript">
    function initSwfFocus()
    {
        document.getElementById("mySWF").focus();
    }
</script>

Upvotes: 2

Related Questions