3

私はProcessing(Javaで実行)で2人用ゲームを開発しています。1人のユーザーはWASDキーを使用してキャラクターを制御し、もう1人のユーザーは矢印キーを使用して動きを制御します。私が抱えている問題はkeyPressed、矢印が押されたときに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;
    }
}
4

3 に答える 3

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」を配置することをお勧めします。

于 2013-02-12T15:35:02.297 に答える
0

keyグローバル変数ですか?プレーヤーに渡されるのがわかりません。グローバルの場合、一度に保持できるキーは1つだけであるため、一度に2人のプレーヤーを制御することはできません。

于 2013-02-12T07:54:24.623 に答える
0

これは、キーを同時に押す(斜めに移動する)ために使用したArduino/Processingコードです。@Brannonで示されるエラーの問題を修正し、boolean[] cannot be cast to int[]keyの代わりに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;
}
于 2016-06-24T22:06:54.487 に答える