Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

javascript - Javascript:无法读取未定义的属性“移动”(Javascript: Cannot read property 'moving' of undefined)

Im getting this following error in my script: "Cannot read property 'moving' of undefined" on line 26 of player.js

(我在脚本中遇到以下错误:player.js第26行上的“无法读取未定义的属性'移动'”)

No idea why because I have "this.moving = false" defined in userMovement.js.

(不知道为什么,因为在userMovement.js中定义了“ this.movi??ng = false”。)

Does it maybe have to do with my controller in player.js?

(可能与player.js中的控制器有关吗?)

Could someone help me figure out why?

(有人可以帮我找出原因吗?)

Thanks in advance.

(提前致谢。)

player.js:

(player.js:)

var players = {};

class player {
  constructor(name, hasController) {
    this.pos = new position(this, 300, 300, 0, -16);
    this.img = new animator(this, this.pos, "tilesets/player", 4, 4, .2);
    if(hasController) this.controller = new userMovement(this, this.pos, this.img);

    this.properties = [
    this.pos,
    this.img
    ];
    if(hasController) this.properties.push(this.controller);
    this.name = name;
    players[name] = this;
    objects.push(this);
  }

  step() {
      if(this.name == client.socket.id) 
          client.send({
              posX: this.pos.x,
              posY: this.pos.y,
              id: client.socket.id,
              dir: this.img.realY,
              isAnimating: this.controller.moving
            }, "playerMovement");
  }
}

userMovement.js:

(userMovement.js:)

class userMovement {
  constructor(parent, pos, img) {
    this.parent = parent;
    this.pos = pos;
    this.img = img;

    this.pos.x = this.pos.x - (this.pos.x % 16/*32*/);
    this.pos.y = this.pos.y - (this.pos.y % 16/*32*/);

    this.speed = 3.5;
    this.target = {
        x: this.pos.x,
        y: this.pos.y
    }
    this.moving = false;
    this.keys = {
        W: 87,
        A: 65,
        S: 83,
        D: 68
    };
  }
  step() {
      if(keyIsDown(this.keys.W) && !this.moving) {
          this.img.setY(3);
          this.moving = true;
          this.target.y -= 32;
      }
      if(keyIsDown(this.keys.A) && !this.moving) {
          this.img.setY(1);
          this.moving = true;
          this.target.x -= 32;
      }
      if(keyIsDown(this.keys.S) && !this.moving) {
          this.img.setY(0);
          this.moving = true;
          this.target.y += 32;
      }
      if(keyIsDown(this.keys.D) && !this.moving) {
          this.img.setY(2);
          this.moving = true;
          this.target.x += 32;
      }
      if(this.moving) {
          var distX = this.target.x - this.pos.x;
          var distY = this.target.y - this.pos.y;
          var dx = Math.sign(distX) * this.speed;
          var dy = Math.sign(distY) * this.speed;
          if(Math.abs(distX) <= this.speed && Math.abs(distY) <= this.speed) {
              this.pos.x = this.target.x;
              this.pos.y = this.target.y;
              this.img.setX(0);
              this.moving = false;
          } else {
              this.pos.x += dx;
              this.pos.y += dy;
              this.img.animateX();
          }
      }
  }
}
  ask by Zaedian translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if(hasController) in "player.js" does not ensure this.controller is actually initiated.

(“ player.js”中的if(hasController)无法确保this.controller实际上已启动。)

Check the code where player class is instantiated.

(检查实例化player类的代码。)

Maybe you are passing a falsy value to player constructor?

(也许您正在将虚假的值传递给玩家构造函数?)

That would cause this.controller = new userMovement(this, this.pos, this.img);

(这将导致this.controller = new userMovement(this, this.pos, this.img);)

in player.js to not be executed.

(在player.js中不执行。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...