我想保留我的键盘事件,但也可以添加鼠标事件。我的游戏键盘事件工作正常我只是不知道如何向它们添加鼠标事件。
我的舞台上有一些电影剪辑,左右上下等。
Character.as
[code]
if( m_keyboard.IsKeyDown( eKeyCodes.kLeftArrow ) )
{
walking = true;
x += 1;
this.scaleX = 1;
}
if( m_keyboard.IsKeyDown( eKeyCodes.kRightArrow ) )
etc....
[code]
eKeyCodes.as
[code]
static public const kLeftArrow:int = 37;
static public const kRightArrow:int = 39;
etc....
[/code]
Keyboard.as
[/code]
public class Keyboard
{
private const kNumKeyCodes:int = 255;
private var m_liveKeyState:Vector.<Boolean>;
private var m_keyState:Vector.<Boolean>;
private var m_lastKeyState:Vector.<Boolean>;
public function Keyboard( stage:Stage )
{
// add handlers
stage.addEventListener( KeyboardEvent.KEY_DOWN, OnKeyDown, false, 0, true );
stage.addEventListener( KeyboardEvent.KEY_UP, OnKeyUp, false, 0, true );
// vector is an array of booleans , the array is length is passed into the constructor as 255.
m_keyState = new Vector.<Boolean>( kNumKeyCodes );
m_lastKeyState = new Vector.<Boolean>( kNumKeyCodes );
m_liveKeyState = new Vector.<Boolean>( kNumKeyCodes );
// clear state
for ( var i:int = 0; i<kNumKeyCodes; i++ )
{
// the whole array is false;
m_keyState[i] = false;
m_lastKeyState[i] = false;
m_liveKeyState[i] = false;
}
}
public function Update( ):void
{
for ( var i:int = 0; i<kNumKeyCodes; i++ )
{
m_lastKeyState[i] = m_keyState[i];
m_keyState[i] = m_liveKeyState[i];
}
}
private function OnKeyDown( e:KeyboardEvent ):void
{
m_liveKeyState[e.keyCode] = true;
trace("m_liveKeyState = " + e.keyCode);
}
private function OnKeyUp( e:KeyboardEvent ):void
{
m_liveKeyState[e.keyCode] = false;
}
public function IsKeyDown( key:int ):Boolean
{
// if key is down leftarrow ( 36 ) && 36 < 255 do no error; as key is a valid key;
Assert( key >= 0 && key <= kNumKeyCodes, "Keyboard.IsKeyDown(): invalid key!" );
return m_keyState[key];
}
/// Is key transitioning
public function IsKeyDownTransition( key:int ):Boolean
{
Assert( key >= 0 && key <= kNumKeyCodes, "Keyboard.IsKeyDownTransition(): invalid key!" );
return !m_lastKeyState[key] && m_keyState[key];
}
}
}
[/code]
我在想类似的东西......
TouchButtons.as
[code]
icon.leftButton.addEventListner(MouseEvent.MOUSE_DOWN, leftButtonPressed, false, 0, true);
icon.RightButton.addEventListner(MouseEvent.MOUSE_DOWN, rightButtonPressed, false, 0, true);
public function leftButtonPressed( event:MouseEvent ):void
{
m_keyboard.IsKeyDown( eKeyCodes.kLeftArrow );
}
etc...
[code]
但这似乎不起作用,有没有一种简单的方法可以做到这一点。