简体   繁体   English

如果我在本地声明我的变量,为什么我的动画不起作用?

[英]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方法(就像xy )。

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.

相关问题 为什么我的动画不起作用? - Why wont my Animation work? 为什么动画无法在我的滑块中正常播放? - why the animation do not work properly in my slider? 为什么我的AngularJS动画不起作用? - Why won't my AngularJS animation work? 为什么不能声明变量,将其转换为整数并在代码中使用该整数? - why can't I declare a variable, convert it to an integer and use that integer in my code? 为什么我的关键帧动画无法在div上工作? - Why won't my key frames animation work on my div? 我的Javascript在本地工作,但不在服务器上工作。 为什么? - My Javascript working locally but dont work on server. Why? 为什么Chrome使我在此循环中声明我的变量? - Why is Chrome making me declare my variable in this loop? 为什么我的 addEventListener function 为变量在本地而不是全局赋值? - Why does my addEventListener function assign a value to a variable locally, but not globally? 如何在我的温度计算器中声明一个变量来解决这个问题? - How can I declare a variable to solve this in my temperature calculator? 添加了计数器变量和时间间隔后,为什么动画不起作用? - Why is my animation not working now that I've added counter variable and Intervals?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM