Hey all,

I'm working on some physics/game stuff... not typical Flex area of concern, 
but... well 
eventually I'll be making use of the Flex specific stuff, and for now I want to 
write in in AS3 
and am on a Mac, so no AS3 Preview for me... :(

Anyways, I wrote a little function to try and capture "persistent" key press... 
as the 
Key.isDown() got deprecated.  It looks like this:

private var l:uint; //left
private var u:uint; //up
private var r:uint; //right
private var d:uint; //down

private function persistKeyDown(event:KeyboardEvent):void
{
        if(event.keyCode == Keyboard.LEFT)
        {
                l = setInterval(control, 10, event);
        }
        
        if(event.keyCode == Keyboard.UP)
        {
                u = setInterval(control, 10, event);
        }
        
        if(event.keyCode == Keyboard.RIGHT)
        {
                r = setInterval(control, 10, event);
        }
        
        if(event.keyCode == Keyboard.DOWN)
        {
                d = setInterval(control, 10, event);
        }
        
        //tFrame.text = "keyDown: " + event.keyCode;
}

private function persistKeyUp(event:KeyboardEvent):void
{
        if(event.keyCode == Keyboard.LEFT)
        {
                clearInterval(l);
        }
        
        if(event.keyCode == Keyboard.UP)
        {
                clearInterval(u);
        }
        
        if(event.keyCode == Keyboard.RIGHT)
        {
                clearInterval(r);
        }
        
        if(event.keyCode == Keyboard.DOWN)
        {
                clearInterval(d);
        }
        
        //tFrame.text = "keyUp: " + event.keyCode;
}

The control function just applies forces to an object on the screen.  I have 
the seperate 
intervals for each key so that I can catch simultaneous keys being held down... 
is that 
right?  I think I must be doing something wrong, because, though it works fine 
in FireFox, 
it behaves all weird in Safari.

You can check it out here:

http://www.noncehence.com/game/experiments/test05.html

Ideally, the green ball should bounce off the walls and the grey box.  You'll 
have to click 
inside the canvas to give it focus, and you can control the green ball with the 
arrow keys.  
It's actually kind of fun already, but only in FireFox.  In Safari, the green 
ball seems to get 
"stuck" in one direction or another and "magnatize" to a wall.

Is there a difference between how keyDown/Up events are dispatched in various 
browsers?  
That doesn't make any sense to me... but then, I don't know why I'm having this 
problem.

If anyone can help, or has other questions for me, please let me know.

Thanks much,
//Matt

Reply via email to