javascript 및 processing.js를 사용하여 간단한 pong 복제본을 만들고 있습니다. 저는 패들에 대한 클래스를 만들었습니다. 클래스는 플레이어가 제어 할 클래스를 만들기 위해 확장됩니다. 현재, 플레이어 제어 클래스 내에서 키보드 입력 처리를 구현하려고 시도하고 있습니다. 제 의도는 w
또는 s
을 누르면 플레이어 클래스 내에서 pVector
변수로 표시되는 속도로 플레이어의 패들의 위치를 업데이트한다는 것입니다.processing.js를 사용하여 자바 스크립트에서 직사각형을 이동하는 속도 사용
그러나 해당 키를 누르면 현재 패들이 사라집니다.
스크립트
는 jsfiddle here에서 볼 수 있으며, 다음과 같이 내 코드입니다 : 내가 잘못 뭐하는 거지// For reference
// standard rectangle constructor is: rect(x, y, width, height);
class Paddle
{
//width of paddle
float pWidth;
//height of paddle
float pHeight;
//initial paddle x coordinate
float x;
//initial paddle y coordinate
float y;
Paddle(float w, float h, float startX, float startY)
{
//set width
paddleWidth = w;
//set height
paddleHeight = h;
//set start x
x = startX;
//set start y
y = startY;
}
void update()
{
}
void draw()
{
//draw and fill rectangle with white
fill(255)
rect(x,y,paddleWidth,paddleHeight)
}
}
class Player extends Paddle
{
Player(float w, float h, float startX, float startY)
{
super(w,h,startX,startY);
}
}
class PlayerOne extends Paddle
{
pVector playerVelocity = (0,0);
PlayerOne(float w, float h, float startX, float startY)
{
super(w,h,startX,startY);
}
void update()
{
debugger;
if(keyPressed)
{
if(key == 'w')
{
y -= playerVelocity.y;
}
else if(key == 's')
{
y += playerVelocity.y;
}
}
}
}
//array list to hold the player paddles
ArrayList<Paddle> paddles = new ArrayList<Paddle>();
//middle x and middle y
float mx, my, pw, ph;
void setup()
{
mx = width/2;
my = height/2;
pw = 10;
ph = 50;
player1 = new PlayerOne(pw,ph,10,10);
player2 = new Player(pw,ph,385,10);
paddles.add(player1);
paddles.add(player2);
size(400,400);
}
void draw()
{
background(0);
fill(100,100,0);
// update each paddle added to array list
for(Paddle p: paddles)
{
p.update();
p.draw();
}
}
?
업데이트 : if(keyPressed)
:
나는 키 누름에 조건 후 라인 debugger
에 중단 점을 넣어. 키를 한 번 누르면 모든 이유로 업데이트가 반복적으로 감지됩니다.
1. 시도가 사용 : 그래서 playerVelocity가 제대로 초기화되지 위치에 추가 할 때 견과류 간다 ... 시도 자바 스크립트 디버거를 사용하고 있습니까? 예 : 파이어 폭스 용 파이어 버그 추가 기능. 이와 같은 문제를 파악하는 데 정말 유용합니다. 빠른 시선으로 당신은 한계를 벗어나는 일을 처리하지 않는 것 같습니다 ... – Floris
나는 jsfiddle을 통해 모든 일을 정말로 해왔습니다. 브라우저 js 디버거가 jsfiddle를 통해 실행되는 코드의 디버깅을 허용합니까? 내 가정은 그렇습니다. 스크립트가 사이트를 통해 실행된다고 가정하기 때문에, 어리석은 질문 인 경우 내 무지를 용서하십시오. – Christian
Nevermind, 키 누름 조건 :'if (keyPressed)'다음에'debugger' 행에 중단 점을 넣을 수있었습니다. 키를 한 번 누르면 모든 이유로 업데이트가 반복적으로 감지됩니다. – Christian