所以在我的代码中,我试图让它成为你有两个玩家的地方,每个玩家都有自己的按键和键盘处理程序,在我的附加代码中清楚地标记,并且在我处理动作时,玩家一个有WSAD而玩家2使用左,右,上和下键,但是,当我按下相对按键处理程序时,它会立即直接触发按键处理程序,我不知道为什么,这里是我的代码,如果有人可以帮助那将是伟大的! :)
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip{
var playerOneLeft:Boolean = new Boolean(false);
var playerOneRight:Boolean = new Boolean(false);
var playerOneUp:Boolean = new Boolean(false);
var playerOneDown:Boolean = new Boolean(false);
var playerTwoLeft:Boolean = new Boolean(false);
var playerTwoRight:Boolean = new Boolean(false);
var playerTwoUp:Boolean = new Boolean(false);
var playerTwoDown:Boolean = new Boolean(false);
var playerSpeed:int = 5;
var gamePage:GamePage;
public function Main(){
gamePage = new GamePage();
addChild(gamePage);
addAllPlayerHandlers();
}
public function playerOneKeyDownHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.A){
playerOneLeft = true;
trace("a");
}
else if(event.keyCode == Keyboard.D){
playerOneRight = true;
trace("d");
}
else if(event.keyCode == Keyboard.W){
playerOneUp = true;
trace("w");
}
else if(event.keyCode == Keyboard.S){
playerOneDown = true;
trace("s");
}
}
public function playerOneKeyUpHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.A){
playerOneLeft = false;
trace("a");
}
else if(event.keyCode == Keyboard.D){
playerOneRight = false;
trace("d");
}
else if(event.keyCode == Keyboard.W){
playerOneUp = false;
trace("w");
}
else if(event.keyCode == Keyboard.S){
playerOneDown = false;
trace("s");
}
}
public function playerTwoKeyDownHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.LEFT){
playerTwoLeft = true;
trace("left");
}
else if(event.keyCode == Keyboard.RIGHT){
playerTwoRight = true;
trace("right");
}
else if(event.keyCode == Keyboard.UP){
playerTwoUp = true;
trace("up");
}
else if(event.keyCode == Keyboard.DOWN){
playerTwoDown = true;
trace("down");
}
}
public function playerTwoKeyUpHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.LEFT){
playerTwoLeft = false;
trace("left");
}
else if(event.keyCode == Keyboard.RIGHT){
playerTwoRight = false;
trace("right");
}
else if(event.keyCode == Keyboard.UP){
playerTwoUp = false;
trace("up");
}
else if(event.keyCode == Keyboard.DOWN){
playerTwoDown = false;
trace("down");
}
}
public function playerMovementHandler(event:Event):void{
if(playerOneLeft){
gamePage.playerOne.x -= playerSpeed;
}
else if(playerOneRight){
gamePage.playerOne.x += playerSpeed;
}
else if(playerOneUp){
gamePage.playerOne.y += playerSpeed;
}
else if(playerOneDown){
gamePage.playerOne.y -= playerSpeed;
}
if(playerTwoLeft){
gamePage.playerTwo.x -= playerSpeed;
}
else if(playerTwoRight){
gamePage.playerTwo.x += playerSpeed;
}
else if(playerTwoUp){
gamePage.playerTwo.y += playerSpeed;
}
else if(playerTwoDown){
gamePage.playerTwo.y -= playerSpeed;
}
}
public function playerOneCollision(event:Event):void{
if(gamePage.playerOne.hitTestObject(gamePage.leftWall)){
gamePage.playerOne.x += playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.rightWall)){
gamePage.playerOne.x -= playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.topWall)){
gamePage.playerOne.y -= playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.bottomWall)){
gamePage.playerOne.y += playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.middleWall)){
gamePage.playerOne.x -= playerSpeed;
}
}
public function playerTwoCollision(event:Event):void{
if(gamePage.playerTwo.hitTestObject(gamePage.leftWall)){
gamePage.playerTwo.x += playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.rightWall)){
gamePage.playerTwo.x -= playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.topWall)){
gamePage.playerTwo.y -= playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.bottomWall)){
gamePage.playerTwo.y += playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.middleWall)){
gamePage.playerTwo.x += playerSpeed;
}
}
public function addPlayerMovementHandler(){
stage.addEventListener(Event.ENTER_FRAME,playerMovementHandler);
}
public function addPlayerKeyHandlers(){
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyUpHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyUpHandler);
}
public function addPlayerCollisionHandlers(){
gamePage.playerOne.addEventListener(Event.ENTER_FRAME,playerOneCollision);
gamePage.playerTwo.addEventListener(Event.ENTER_FRAME,playerTwoCollision);
}
public function addAllPlayerHandlers(){
addPlayerKeyHandlers();
addPlayerCollisionHandlers();
addPlayerMovementHandler();
}
}
答案 0 :(得分:0)
当我按下相对按键向下处理程序时,它会在
之后立即触发按键向上处理程序
这是因为您将其注册为
gem install pry
你应该学到的教训是你的副本&粘贴编写代码的方法是一种不好的做法。你不应该重复自己。所有这些重复的代码都会导致类似手头的问题。