我尝试在处理过程中创建平滑的运动,但是现在无法正常工作,而且我不确定为什么它不起作用。我是编码的初学者,所以在解释:)时,请不要对我来说太复杂。角色现在动作非常缓慢,我不能同时按下多个按钮,如果有人可以帮助我,那就太好了。预先谢谢你。
class Player1 {
int x = width/2;
int y;
float speedX = 5;
float speedY = 5;
int size;
boolean up, down, left, right;
Player1(int newX, int newY, int newSize) {
x = newX;
y = newY;
size = newSize;
}
void run() {
display();
movePlayer1();
}
void display() {
ellipse(x, y, size, size);
}
void movePlayer1() {
if (keyPressed) {
if (key == 'a' || key == 'A') {
left = true;
}
if (key == 'd' || key == 'D') {
right = true;
}
if (key == 'w' || key == 'W') {
up = true;
}
if (key == 's' || key == 'S') {
down = true;
}
if (key == 'a' || key == 'A') {
x -= speedX;
}
if (key == 'd' || key == 'D') {
x += speedX;
}
if (key == 'w' || key == 'W') {
y -= speedY;
}
if (key == 's' || key == 'S') {
y += speedY;
}
}
}
void keyReleased() {
if (key == 'a' || key == 'A') {
left = false;
}
if (key == 'd' || key == 'D') {
right = false;
}
if (key == 'w' || key == 'W') {
up = false;
}
if (key == 's' || key == 'S') {
down = false;
}
}
}
这是主代码页
Stars stars[] = new Stars[100];
Player1 player1;
Player2 player2;
void setup() {
size(600, 800);
player1 = new Player1(width/2, 100, 30);
player2 = new Player2(width/2, 700, 30);
for (int i = 0; i < stars.length; i++) {
stars[i] = new Stars((int)random(width), (int)random(height));
}
}
void draw() {
background(0);
stroke(255);
strokeWeight(3);
line(0, height/2, width, height/2);
//draw stars
for (int i = 0; i < stars.length; i++) {
stars[i].run();
}
// draw players
player1.run();
player2.run();
}
答案 0 :(得分:0)
首先,我建议分别更改类Player1
Player2
。对于行为相同的对象,请勿使用不同的类。注意,唯一的区别是播放器是通过不同的键移动的,不应由不同的对象来处理。
创建单个类Player
。新方法pressed
和released
的类是ah,它们根据输入参数dx
更改当前的移动方向(dy
,left
), right
,up
和down
。例如:
class Player {
int x, y;
int dx = 0, dy = 0;
float speedX = 5;
float speedY = 5;
int size;
Player(int newX, int newY, int newSize) {
x = newX;
y = newY;
size = newSize;
}
void run() {
display();
movePlayer();
}
void movePlayer() {
x += dx * speedX;
y += dy * speedY;
}
void display() {
ellipse(x, y, size, size);
}
void pressed(boolean left, boolean right, boolean up, boolean down) {
if (left) dx = -1;
if (right) dx = 1;
if (up) dy = -1;
if (down) dy = 1;
}
void released(boolean left, boolean right, boolean up, boolean down) {
if (left) dx = 0;
if (right) dx = 0;
if (up) dy = 0;
if (down) dy = 0;
}
}
使用电子键盘回调keyPressed()
和keyReleased()
来改变演奏者的动作。根据按下或释放的键来设置参数left
,right
,up
和down
。例如:
Player player1;
Player player2;
void setup() {
// [...]
}
void draw() {
// [...]
}
void keyPressed() {
player1.pressed((key == 'a' || key == 'A'), (key == 'd' || key == 'D'),
(key == 'w' || key == 'W'), (key == 's' || key == 'S'));
player2.pressed(keyCode == LEFT, keyCode == RIGHT, keyCode == UP, keyCode == DOWN);
}
void keyReleased() {
player1.released((key == 'a' || key == 'A'), (key == 'd' || key == 'D'),
(key == 'w' || key == 'W'), (key == 's' || key == 'S'));
player2.released(keyCode == LEFT, keyCode == RIGHT, keyCode == UP, keyCode == DOWN);
}
这使您可以同时处理多个键的状态。