[英]Why will my animation not work if I declare my variable locally?
I'm quite new to JS so please excuse my ignorance but I can't figure out why my animation if statement doesn't work if I declare my speed variable locally in the move() function. 我是JS的新手,所以请原谅我的无知,但是如果我在move()函数中本地声明我的速度变量,我无法弄清楚为什么我的动画if语句不起作用。
If I don't declare the speed variable globally, the girl gets to the windowWidth and gets stuck moving a couple of pixels back and forth. 如果我没有全局声明速度变量,那么女孩会进入windowWidth并且卡住来回移动几个像素。 Basically staying there rather than moving the other way.
基本上停留在那里而不是移动其他方式。
let speed = 2;
class Girl {
constructor(x, y) {
this.x = x,
this.y = y
}
body() {
noStroke();
fill(239, 101, 233);
rect(this.x, this.y, 20, 40);
fill(249, 192, 155);
triangle(this.x, this.y, this.x + 20, this.y, this.x + 10, this.y + 15);
}
move() {
if (this.x > windowWidth + 50 || this.x < -50) {
speed = speed * -1;
}
this.x = this.x + speed;
}
}
I should mention I'm using the p5 library in case I'm using any funky functions. 我应该提一下,我正在使用p5库,以防我使用任何时髦的功能。 It works but I'm sure I could tidy this up a little bit.
它有效,但我相信我可以整理一下。 Any advice would be more than welcome.
任何建议都会受到欢迎。
Thanks in advance! 提前致谢!
You should not declare it as a local variable inside the move
method (as that would make it get re-initialised to 2
on every call), but you should make it a property of the instance that gets initialised in the constructor and modified in the move
method (just like x
and y
). 你不应该将它声明为
move
方法中的局部变量(因为这会使它在每次调用时重新初始化为2
),但你应该使它成为在构造函数中初始化并在move
方法(就像x
和y
)。
class Girl {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 2;
}
body() {
noStroke();
fill(239, 101, 233);
rect(this.x, this.y, 20, 40);
fill(249, 192, 155);
triangle(this.x, this.y, this.x + 20, this.y, this.x + 10, this.y + 15);
}
move() {
if (this.x > windowWidth + 50 || this.x < -50) {
this.speed = this.speed * -1;
}
this.x = this.x + this.speed;
}
}
Because the value of speed
is shared across multiple calls to move
. 因为
speed
的值是在多个要move
调用之间共享的。 If you declare it inside move
then it gets created for each call to move
, thus any previous value of speed
will be ignored. 如果你在
move
声明它,则会为每个要move
调用创建它,因此将忽略任何先前的speed
值。
If you don't want speed
to be a global variable, then you can make it a property of the class Girl
: 如果你不想让
speed
成为一个全局变量,那么你可以使它成为Girl
类的属性:
class Girl {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 2; // make 'speed' a property of the class
}
/* ... */
// use 'this.speed' inside 'move' instead of just 'speed'
move() {
if (this.x > windowWidth + 50 || this.x < -50) {
this.speed = this.speed * -1;
}
this.x = this.x + this.speed;
}
}
Problem here is this.x > windowWidth + 50 || this.x < -50
这里的问题是
this.x > windowWidth + 50 || this.x < -50
this.x > windowWidth + 50 || this.x < -50
. this.x > windowWidth + 50 || this.x < -50
。 Try to log this
inside move()
function and you will see that it is referring to move().x
instead of Girl.x
. 尝试
this
内部的move()
函数记录下来,您将看到它指的是move().x
而不是Girl.x
So this.x
is undefined
and undefined > 10 + 50
is always false. 因此
this.x
undefined
this.x
undefined > 10 + 50
始终为false。
Ps I dont know p5 so this is vanilla. Ps我不知道p5所以这是香草。
So to fix this you need to declare another variable to get Girl
scope. 所以要解决这个问题,你需要声明另一个变量来获得
Girl
范围。
var Girl = function(){
var self = this;
//code goes here
function move(){
self.x = setValue;
console.log(this.x) //undefined
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.