flash on 2012-2-2

by dimitris.1972g
♥0 | Line 53 | Modified 2012-02-03 00:11:18 | MIT License
play

ActionScript3 source code

/**
 * Copyright dimitris.1972g ( http://wonderfl.net/user/dimitris.1972g )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fles
 */

package {
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
public function KeyPoll( displayObj:DisplayObject )
{
states = new ByteArray();
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
states.writeUnsignedInt( 0 );
dispObj = displayObj;
dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true );
dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true );
dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true );
}

private function keyDownListener( ev:KeyboardEvent ):void
{
states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
}

private function keyUpListener( ev:KeyboardEvent ):void
{
states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
}

private function activateListener( ev:Event ):void
{
for( var i:int = 0; i < 8; ++i )
{
states[ i ] = 0;
}
}

private function deactivateListener( ev:Event ):void
{
for( var i:int = 0; i < 8; ++i )
{
states[ i ] = 0;
}
}

/**
* To test whether a key is down.
*
* @param keyCode code for the key to test.
*
* @return true if the key is down, false otherwise.
*
* @see isUp
*/
public function isDown( keyCode:uint ):Boolean
{
return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
}

/**
* To test whether a key is up.
*
* @param keyCode code for the key to test.
*
* @return true if the key is up, false otherwise.
*
* @see isDown
*/
public function isUp( keyCode:uint ):Boolean
{
return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0;
}
}
}