2013-02-12 104 views
3

我正在开发一个处理(在Java上运行)的双人游戏。一个用户将使用WASD键控制他的角色,另一个用户使用箭头键控制移动。我遇到的问题是使用keyPressed时,按下箭头时将WASD取消,反之亦然。我一直在搞这件事很长时间。有没有人知道一个工作或注意到我做错了什么?同时使用WASD和箭头键

//global variables 
int wide = 600; //canvas width 
int tall = 600; //canvas height 
int s = 50; //player size 
float speed = 2.5; //player movement speed 

//colors 
int redColor = #CB4646; //player 1 color 
int blueColor = #4652CB; //player 2 color 
int backgroundColor = #DBE3B3; //background color 
float player1X = 600/3-s; //HOW COME width/3 DOESN'T WORK?????????? 
float player2X = 600*2/3; 
float playerY = 600/2-(s/2); 

//players 
Player player1 = new Player(player1X, playerY, s, speed, "wasd", redColor); //player 1 
Player player2 = new Player(player2X, playerY, s, speed, "arrows", blueColor); //player 2 

//setup 
void setup(){ 
    background(backgroundColor); 
    size(wide, tall); 
    smooth(); 
    println(player2.controls); 
} 

//draw 
void draw(){ 
    background(backgroundColor); 
    player1.usePlayer(); 
    player2.usePlayer(); 
} 

class Player{ 

    //class variables 
    float x; // x position 
    float y; // y position 
    int s; //size 
    float speed; //speed 
    String controls; //controls 
    int colors; //player color 
    char keyControls [] = new char [4]; 

    //construct 
    Player(float tempX, float tempY, int tempS , float tempSpeed, String tempControls, int tempColors){ 
    x = tempX; 
    y = tempY; 
    s = tempS; 
    speed = tempSpeed; 
    controls = tempControls; 
    colors = tempColors; 
    } 

    void usePlayer(){ 

    // draw player 
    fill(colors); 
    rect(x, y, s, s); 

    //move player 
    keyPressed(); 

    //wraparound 
    boundaries(); 

    } 

    void keyPressed(){ 

    //sets controls for wasd 
    if(controls == "wasd"){ 
      if(key == 'w' || key == 'W'){ 
      y -= speed; //move forwards 
      } 
      if(key == 's' || key == 'S'){ 
      y += speed; //move backwards 
      } 
      if(key == 'd' || key == 'D'){ 
      x += speed; //move right 
      } 
      if(key == 'a' || key == 'A'){ 
      x -= speed; //move left 
      } 
     } 

     //sets controls for arrows 
     if(controls == "arrows"){ 
     if(key == CODED){ 
      if(keyCode == UP){ 
      y -= speed; //move forwards 
      } 
      if(keyCode == DOWN){ 
      y += speed; //move backwards 
      } 
      if(keyCode == RIGHT){ 
      x += speed; //move right 
      } 
      if(keyCode == LEFT){ 
      x -= speed; //move left 
      } 
     } 
     } 
     } 

    //pacman style wraparound 
    void boundaries(){ 
    if(x == width) x = 2; 
    if(y == height) y = 2; 
    if(x == 0) x = width-s; 
    if(y == 0) y = height-s; 
    } 
} 
+0

是否与键盘无法一次注册超过2-6个键(取决于哪个键)相关? – Patashu 2013-02-12 03:17:09

+0

@Patashu,我不这么认为,因为当我同时按下两个按钮时它不起作用。 – Brannon 2013-02-12 04:58:29

+1

请注意,这实际上不是一个Java问题,而是一个Processing问题。 Processing是一个非常具体的API,它的程序设计模型与Java稍有不同(它被重写为Java,以便在JVM中脱机使用,但在浏览器中在线使用时会重写为JavaScript) – 2013-02-12 15:35:52

回答

3

独立跟踪您的密钥,不要依赖事件全局变量。

boolean[] keys = new int[255]; 

void keyPressed() { 
    keys[keyCode] = true; 
} 

void keyReleased() { 
    keys[keyCode] = false; 
} 

void draw() { 
    updatePlayers(); 
    drawStuff(); 
} 

void updatePlayers() { 
    if(keys[LEFT]) { p1.move(-1,0); } 
    if(keys[RIGHT]) { p1.move(1,0); } 
    if(keys[UP]) { p1.move(0,-1); } 
    if(keys[DOWN]) { p1.move(0,1); } 

    if(keys['a']) { p2.move(-1,0); } 
    if(keys['d']) { p2.move(1,0); } 
    if(keys['w']) { p2.move(0,-1); } 
    if(keys['s']) { p2.move(0,1); } 
} 

请注意,这必须是一系列if语句,因为您要处理所有按下的键。如果某人左右举行,p1会左移,右移。

另请注意,此示例代码不会过滤出为特殊键获得的高于255的代码,因此您可能希望在事件开始时放置“if(keyCode> 255)return”处理程序。

+0

林在这背后的逻辑有点麻烦。我很抱歉,我是一般处理和编程的新手,我非常感谢您的帮助。你是否建议我删除usePlayer方法,并用一个移动方法替换它,该方法接收我的'速度'浮点数作为参数增加到'x'和'y'?我只是想了解如何将其转换为我的'Player'类。此外,我得到错误信息**不能从布尔转换为int **关于此代码'keys [keyCode] = true;'。 – Brannon 2013-02-12 17:19:51

+1

是的:使用一些显式的逻辑来移动你的播放器的左/右和上/下值,并根据当前注册为关闭的每个键确定该值。所以,在你画出你的球员之前,你需要处理这个画面下的所有键,然后你调用球员的画图函数。此外,“不能转换...”的错误是因为我很傻,不小心让你用int []代替布尔值[]> _> – 2013-02-12 23:33:16

+0

非常感谢你的帮助!我只是将这个逻辑应用到我的程序中,它完美地移动了箭头键。它会注册两个键一次按下并对角移动!然而,我很难让WASD键工作。我认为这与你提出的有关'key'而不是'keyCode'值的事实有关。让他们俩都能工作的看起来会是什么样子? – Brannon 2013-02-14 00:58:27

0

key一个全局变量?我没有看到它传递给玩家。如果它是全球性的,那么它一次只能持有一把钥匙,而不能一次控制两名玩家。

0

这是我用来处理同时按键(对角移动)的Arduino/Processing代码。它通过@Brannon解决了boolean[] cannot be cast to int[]错误的问题,并使用keyCodes代替密钥。

import processing.serial.*; 
​ 
boolean[] keys = new boolean[255]; 
Serial port; 
​ 
void setup() { 
port = new Serial(this, Serial.list()[1], 9600); 
​ 
} 
​ 
void draw() { 

    // loop through boolean array and see which ones (index = keyCode) 
    // are true, then write to them. 
    for(int i = 0; i < 255; i++) { 
    if(keys[i]) { 
     if (i == 87) { port.write('w'); } 
     if (i == 65) { port.write('a'); } 
     if (i == 83) { port.write('s'); } 
     if (i == 68) { port.write('d'); } 
    } 
    } 
} 
​ 
void keyPressed() { 
    keys[keyCode] = true; 
} 
​ 
void keyReleased() { 
    keys[keyCode] = false; 
}