package
{
import flash.display.MovieClip;
import flash.events.*;
public class untitledCode extends MovieClip
{
private var speed:Number = 5;
public function untitledCode()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, whenKey1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, whenKey2);
}
private function whenKey1(event:KeyboardEvent):void
{
if (event.keyCode == 38)
{
mcPlayer1.y -= speed;
}
if (event.keyCode == 40)
{
mcPlayer1.y += speed;
}
if (event.keyCode == 37)
{
mcPlayer1.x -= speed;
}
if (event.keyCode == 39)
{
mcPlayer1.x += speed;
}
if (event.keyCode == 96)
{
mcPlayer1.play();
}
}
private function whenKey2(event:KeyboardEvent):void
{
if (event.keyCode == 87)
{
mcPlayer2.y -= speed;
}
if (event.keyCode == 83)
{
mcPlayer2.y += speed;
}
if (event.keyCode == 65)
{
mcPlayer2.x -= speed;
}
if (event.keyCode == 68)
{
mcPlayer2.x += speed;
}
if (event.keyCode == 90)
{
mcPlayer2.play();
}
}
}
}
有2个MovieClip,mcPlayer1和mcPlayer2。当我按住一个MovieClip的键然后按一个键进行另一个键时,第一个MovieClip停止(反之亦然)。如何让它们同时移动。
答案 0 :(得分:0)
我建议你只添加一个按键事件监听器并保存每个按下的按键并添加一个按键事件监听器来删除,这样,你可以控制何时只按下一个按键或多个按键。
我创建了一个简单的例子(只是为了解释主要逻辑),你应该根据自己的需要调整和优化它。
private var keyControl:Vector.<uint>;
public function Test()
{
keyControl = new Vector.<uint>();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDownHandler, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, onUpHandler, false, 0, true);
}
private function onUpHandler(event:KeyboardEvent):void
{
keyControl.splice(keyControl.indexOf(event.keyCode), 1);
}
private function onDownHandler(event:KeyboardEvent):void
{
if(!isKeyPressed(event.keyCode)) keyControl.push(event.keyCode);
/*
to check if some key is being pressed use
if(isKeyPressed(Keyboard.SPACE))
{
// do something
}
if(isKeyPressed(Keyboard.SPACE) && isKeyPressed(Keyboard.A))
{
// do something
}
*/
}
private function isKeyPressed(key:uint):Boolean
{
var returnValue:Boolean;
for (var i:int = 0, numKeys:uint = keyControl.length; i < numKeys; i++)
{
if(keyControl[i] == key)
{
returnValue = true;
break;
}
}
return returnValue;
}
答案 1 :(得分:0)
基于另一个answer,我告诉你这个已经在SO上,我测试了他们的答案,虽然它有效但是它们的实现存在键盘滞后。所以我使用ENTER_FRAME
事件...
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip
{
private var speed:Number = 5;
private var _keyDownStatus:Object = { };
public function Main()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
}
private function onUp(e:KeyboardEvent):void {
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void {
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(e:Event):void {
// Player 1 Events
if (_keyDownStatus[Keyboard.LEFT]) {
mcPlayer1.x -= speed;
}
if (_keyDownStatus[Keyboard.RIGHT]) {
mcPlayer1.x += speed;
}
if (_keyDownStatus[Keyboard.UP]) {
mcPlayer1.y -= speed;
}
if (_keyDownStatus[Keyboard.DOWN]) {
mcPlayer1.y += speed;
}
if (_keyDownStatus[Keyboard.NUMPAD_0]) {
mcPlayer1.play();
}
//Player 2 Events
if (_keyDownStatus[Keyboard.A]) {
mcPlayer2.x -= speed;
}
if (_keyDownStatus[Keyboard.D]) {
mcPlayer2.x += speed;
}
if (_keyDownStatus[Keyboard.W]) {
mcPlayer2.y -= speed;
}
if (_keyDownStatus[Keyboard.S]) {
mcPlayer2.y += speed;
}
if (_keyDownStatus[Keyboard.Z]) {
mcPlayer2.play();
}
}
}
}
我在Flash CC 2014中对此进行了测试,效果非常好!我确实在我的示例中更改了类的名称,以便我的示例正常工作,因此请将其更改为您需要的任何类名。