class 中的平台在调用时不动
Platforms in a class not moving when called in
为了大学课程,我正在和朋友一起制作游戏。一般的想法是,我们有一些平台从右向左移动,每次离开屏幕时,它都会在右侧随机 x
和 y
位置生成(在一定范围内)。会有一个小精灵从一个平台跳到另一个平台。
我们遇到了一个不确定如何解决的问题。我们拥有所有正确的代码和一切,但平台不会移动。它们应该以每帧 -4
像素 (rectVelocity
) 的恒定速度向左移动。
不过,我们无法让他们移动;它们在屏幕上静止不动,位于每个最初调用的位置。
这是我尽可能精简的代码:
Platforms [] mainPlats;
void setup() {
size(750, 400);
mainPlats = new Platforms[3];
}
void draw() {
level();
}
void level() {
//This is the code for the first platform
mainPlats[0] = new Platforms(200, 200, 100, 15); //These values need to be set inside the class so that
//they aren't constantly overwriting the movement variables in the class
mainPlats[0].displayPlat();
mainPlats[0].platTransition();
//This is the code for the second platform
mainPlats[1] = new Platforms(420, 300, 100, 15);
mainPlats[1].displayPlat();
mainPlats[1].platTransition();
//This is the code for the third platform
mainPlats[2] = new Platforms(570, 350, 100, 15);
mainPlats[2].displayPlat();
mainPlats[2].platTransition();
}
class Platforms {
PImage platform;
int rectX, rectY, rectWidth, rectHeight;
int rectVelocity = 4;
Platforms(int x, int y, int w, int h) {
rectX = x;
rectY = y;
// rectX = (int(random(600, 800))); //Tried to randomise start position, failed hilariously
//rectY = (int(random(150, 350)));
rectWidth = w;
rectHeight = h;
}
void displayPlat() {
platform = loadImage ("images/tiles.png");
//imageMode(CENTER);
image(platform, rectX, rectY, 100, 15); //rectangle platforms replaced with images
}
void platMove() {
rectX -= rectVelocity;
}
void platTransition() {
if (rectX < -200) {
rectX = (int(random(700, 1000)));
rectY = (int(random(150, 350)));
}
}
}
当您构建平台时,您将 rectX 设置为正值 (>0),但当您调用 platTransition
时,您正在检查 rectX < -200
,这就是它从不执行任何操作的原因。
从 draw() 函数中调用 level() 函数,它 初始化 你的平台数组每一帧。
这意味着您在每一帧的起始位置创建新个平台。你永远不会看到平台移动,因为一旦你移动它们,你就会在起始位置再次用新平台替换它们。
所以第一步是将它们的初始化移出 level() 函数,并且只调用它们一次,在草图的开头 - setup() 函数将是您可以放置它们的地方。
您的另一个问题是您从未真正调用过 platMove() 函数。所以第二步是确保调用该函数。
解决方案可能如下所示:
Platforms [] mainPlats;
void setup() {
size(750, 400);
mainPlats = new Platforms[3];
mainPlats[0] = new Platforms(200, 200, 100, 15);
mainPlats[1] = new Platforms(420, 300, 100, 15);
mainPlats[2] = new Platforms(570, 350, 100, 15);
}
void draw() {
level();
}
void level() {
mainPlats[0].displayPlat();
mainPlats[0].platMove();
mainPlats[0].platTransition();
mainPlats[1].displayPlat();
mainPlats[1].platMove();
mainPlats[1].platTransition();
mainPlats[2].displayPlat();
mainPlats[2].platMove();
mainPlats[2].platTransition();
}
另请注意,您也不应每帧都加载图像。您应该只在启动时加载一次。您可能还想使用 for 循环遍历您的平台,而不是引用每个索引。但是这些并不能真正影响你的问题。
为了大学课程,我正在和朋友一起制作游戏。一般的想法是,我们有一些平台从右向左移动,每次离开屏幕时,它都会在右侧随机 x
和 y
位置生成(在一定范围内)。会有一个小精灵从一个平台跳到另一个平台。
我们遇到了一个不确定如何解决的问题。我们拥有所有正确的代码和一切,但平台不会移动。它们应该以每帧 -4
像素 (rectVelocity
) 的恒定速度向左移动。
不过,我们无法让他们移动;它们在屏幕上静止不动,位于每个最初调用的位置。
这是我尽可能精简的代码:
Platforms [] mainPlats;
void setup() {
size(750, 400);
mainPlats = new Platforms[3];
}
void draw() {
level();
}
void level() {
//This is the code for the first platform
mainPlats[0] = new Platforms(200, 200, 100, 15); //These values need to be set inside the class so that
//they aren't constantly overwriting the movement variables in the class
mainPlats[0].displayPlat();
mainPlats[0].platTransition();
//This is the code for the second platform
mainPlats[1] = new Platforms(420, 300, 100, 15);
mainPlats[1].displayPlat();
mainPlats[1].platTransition();
//This is the code for the third platform
mainPlats[2] = new Platforms(570, 350, 100, 15);
mainPlats[2].displayPlat();
mainPlats[2].platTransition();
}
class Platforms {
PImage platform;
int rectX, rectY, rectWidth, rectHeight;
int rectVelocity = 4;
Platforms(int x, int y, int w, int h) {
rectX = x;
rectY = y;
// rectX = (int(random(600, 800))); //Tried to randomise start position, failed hilariously
//rectY = (int(random(150, 350)));
rectWidth = w;
rectHeight = h;
}
void displayPlat() {
platform = loadImage ("images/tiles.png");
//imageMode(CENTER);
image(platform, rectX, rectY, 100, 15); //rectangle platforms replaced with images
}
void platMove() {
rectX -= rectVelocity;
}
void platTransition() {
if (rectX < -200) {
rectX = (int(random(700, 1000)));
rectY = (int(random(150, 350)));
}
}
}
当您构建平台时,您将 rectX 设置为正值 (>0),但当您调用 platTransition
时,您正在检查 rectX < -200
,这就是它从不执行任何操作的原因。
从 draw() 函数中调用 level() 函数,它 初始化 你的平台数组每一帧。
这意味着您在每一帧的起始位置创建新个平台。你永远不会看到平台移动,因为一旦你移动它们,你就会在起始位置再次用新平台替换它们。
所以第一步是将它们的初始化移出 level() 函数,并且只调用它们一次,在草图的开头 - setup() 函数将是您可以放置它们的地方。
您的另一个问题是您从未真正调用过 platMove() 函数。所以第二步是确保调用该函数。
解决方案可能如下所示:
Platforms [] mainPlats;
void setup() {
size(750, 400);
mainPlats = new Platforms[3];
mainPlats[0] = new Platforms(200, 200, 100, 15);
mainPlats[1] = new Platforms(420, 300, 100, 15);
mainPlats[2] = new Platforms(570, 350, 100, 15);
}
void draw() {
level();
}
void level() {
mainPlats[0].displayPlat();
mainPlats[0].platMove();
mainPlats[0].platTransition();
mainPlats[1].displayPlat();
mainPlats[1].platMove();
mainPlats[1].platTransition();
mainPlats[2].displayPlat();
mainPlats[2].platMove();
mainPlats[2].platTransition();
}
另请注意,您也不应每帧都加载图像。您应该只在启动时加载一次。您可能还想使用 for 循环遍历您的平台,而不是引用每个索引。但是这些并不能真正影响你的问题。