0
我是新来处理和修改某人的代码。除了为什么我的精灵会随机停止之外,我明白了所有这一切。我最终失去了所有似乎是随机点的运动。任何帮助?处理:精灵移动随机停止
PImage img;
sprite player;
wall[] walls;
void setup() {
size(750, 750);
img = loadImage("sprite.png");
player = new sprite(50,300);
frameRate(60);
smooth();
walls = new wall[3];
walls[0] = new wall(250,0,40,500);
walls[1] = new wall(500,250,40,500);
walls[2] = new wall(300,200,40,500);
}
void draw() {
background(255, 255, 255);
noStroke();
player.draw();
player.move(walls);
for(int i = 0; i < walls.length; i++){
walls[i].draw();
}
}
class sprite {
float x;
float y;
sprite(float _x, float _y){
x = _x;
y = _y;
}
void draw(){
image(img,x,y);
}
void move(wall[] walls){
float possibleX = x;
float possibleY = y;
if (keyPressed==true) {
println(key);
if (key=='a') {
possibleX= possibleX - 2;
}
if (key=='d') {
possibleX = possibleX + 2;
}
if (key=='w') {
possibleY = possibleY - 2;
}
if (key=='s') {
possibleY = possibleY + 2;
}
}
boolean didCollide = false;
for(int i = 0; i < walls.length; i++){
if(possibleX > walls[i].x && possibleX < (walls[i].x + walls[i].w) && possibleY > walls[i].y && possibleY < walls[i].y + walls[i].h){
didCollide = true;
}
}
if(didCollide == false){
x = possibleX;
y = possibleY;
}
}
}
class wall {
float x;
float y;
float w;
float h;
wall(float _x, float _y, float _w, float _h){
x = _x;
y = _y;
w = _w;
h = _h;
}
void draw(){
fill(0);
rect(x,y,w,h);
}
}
如果你将问题缩小到[mcve],你会有更好的运气。请注意,这不应该是你的整个草图。例如,您的问题与绘制图像无关,因此您不需要这些。把它缩小到一个硬编码矩形与另一个硬编码矩形相碰撞。你有没有尝试[调试](http://happycoding.io/tutorials/processing/debugging)你的代码? –