[英]Beginner here. Code isn't obeying. I think the problem has something to do with accessing a variable that's within a class?
So I'm learning about how to use classes and I've made this code (it's exactly 100 lines long.) using P5.JS which is just supposed to draw three rectangles and three circles, One of the circles is able to be moved with the arrow keys.所以我正在学习如何使用类,我已经使用 P5.JS 制作了这段代码(它正好有 100 行长。)它应该画三个矩形和三个圆,其中一个圆可以移动用箭头键。 and the other two follow that one around: Here's the part that doesn't work, when the follower circles move over the squares.
其他两个跟随那个:这是不起作用的部分,当跟随者圆圈移动到正方形上时。 it's supposed to make them slow down as they move over it.
它应该让他们在移动时放慢速度。 I've tried everything.
我什么都试过了。
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
character1 = new character(250, 200, 30, 4);
follower1 = new enemy(100, 20, 30, 2);
follower2 = new enemy(200, 20, 30, 2);
obstacle1 = new obstacle(200, 200, 100, 50, follower1.x, follower1.y, follower1.s);
obstacle2 = new obstacle(300, 300, 50, 25, follower1.x, follower1.y, follower1.s);
obstacle3 = new obstacle(200, 400, 25, 50, follower1.x, follower1.y, follower1.s);
}
function draw() {
background(0);
character1.drawCharacter();
follower1.drawEnemy();
follower2.drawEnemy();
obstacle2.drawObstacle();
obstacle1.drawObstacle();
obstacle3.drawObstacle();
}
class character {
constructor(x, y, d, s) {
this.x = x;
this.y = y;
this.d = d;
this.s = s;
}
drawCharacter() {
fill(255);
ellipse(character1.x, character1.y, character1.d);
if (keyIsDown(UP_ARROW)) {
character1.y -= character1.s;
}
if (keyIsDown(DOWN_ARROW)) {
character1.y += character1.s;
}
if (keyIsDown(LEFT_ARROW)) {
character1.x -= character1.s;
}
if (keyIsDown(RIGHT_ARROW)) {
character1.x += character1.s;
}
}
}
class enemy {
constructor(x, y, d, s) {
this.x = x;
this.y = y;
this.d = d;
this.s = s;
}
drawEnemy() {
fill(255, 0, 0);
ellipse(this.x, this.y, this.d);
if (this.x < character1.x - this.d) {
this.x += this.s;
}
if (this.x > character1.x + this.d) {
this.x -= this.s;
}
if (this.y < character1.y - this.d) {
this.y += this.s;
}
if (this.y > character1.y + this.d) {
this.y -= this.s;
}
}
}
class obstacle {
constructor(x, y, w, h, enemyX, enemyY, enemyS) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
enemyX = enemyX;
enemyY = enemyY;
enemyS = enemyS;
}
drawObstacle(enemyX, enemyY, enemyS) {
fill(255);
rectMode(CENTER);
rect(this.x, this.y, this.w, this.h);
if (
this.x - this.w / 2 - 10 < enemyX &&
this.x + this.w / 2 + 10 > enemyX &&
this.y - this.h / 2 - 10 < enemyY &&
this.y + this.h / 2 + 10 > enemyY
) {
enemyS = 0.5;
} else {
enemyS = 2.5;
}
}
}
Try only having one obstacle.尝试只设置一个障碍。 Here's my guess:
这是我的猜测:
draw calls obstacle.drawObstacle
three times, and if an enemy should be slowed down by obstacle1
or obstacle2
, it doesn't matter because enemy.s
will be reset by obstacle3.drawObstacle
draw 调用了三次
obstacle3.drawObstacle
,如果一个敌人应该被obstacle.drawObstacle
1 或obstacle1
2 减速,没关系,因为敌人会被obstacle2
enemy.s
重置
I highly recommend that each of your character and enemy class have a draw
and a move
method, and put everything about how enemies move within the enemy class.我强烈建议你的每个角色和敌人 class 都有一个
draw
和move
方法,并将所有关于敌人如何移动的信息都放在敌人 class 中。 Maybe the obstacle class should have a draw
and maybe a isCollidingWith
method that enemy.move
would call.也许障碍 class 应该有一个
draw
,也许是一个敌人enemy.move
会调用的isCollidingWith
方法。
Keeping all your logic for an class within the class means that you can do variants much more easily.将 class 的所有逻辑保留在 class 中意味着您可以更轻松地进行变体。 For example, maybe later you'll want to have an enemy that's much slower but isn't hindered by obstacles at all.
例如,也许稍后你会想要一个速度慢得多但完全不受障碍物阻碍的敌人。 Having all the logic within the class makes that easy.
拥有 class 中的所有逻辑使这变得容易。 Putting the logic within the obstacle class would quickly become a nightmare if you have enough different types of enemies.
如果你有足够多不同类型的敌人,将逻辑置于障碍 class 将很快成为一场噩梦。
Also, you could have a game
object that holds everything about the game including character
, enemies
, and obstacles
properties and draw
and move
methods that call the respective methods on character, enemy, and obstacle objects, and a destroy
or quit
method that clears its properties and such.此外,您可以拥有一个
game
object 包含游戏的所有内容,包括character
、 enemies
和obstacles
属性以及调用角色、敌人和障碍物对象的相应方法的draw
和move
方法,以及清除其对象的destroy
或quit
方法属性之类的。 (In some languages this is important, because if you were to add a game
property to each piece in the game, they'd have handles to each other and since (in some languages) garbage collection only reaps objects that have no references to them, they'll take up memory much longer than you want.) (在某些语言中,这很重要,因为如果您要为游戏中的每个部分添加
game
属性,它们将具有彼此的句柄,并且因为(在某些语言中)垃圾收集只获取没有引用它们的对象,它们占用 memory 的时间会比你想要的长得多。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.