【发布时间】:2017-07-27 19:16:32
【问题描述】:
我正在尝试创建一个可以跟随鼠标转动的头像。
我已经成功创建了一个工作化身,现在我想用对象构造函数创建一个相同的化身。基本上一切都是一样的,除非我会输入var angleToBe; 我会写this.angleToBe;。一切都直接链接到构造函数。在我找到方法之前我没有任何问题,特别是turnToMouse。我的语法如下所示:
Avatar.prototype.turnToMouse = function() {
if (this.canTurn) {
this.spotX = this.body.position.x;
this.spotY = this.body.position.y;
this.spotY = this.spotY * -1 + height;
this.body.angleToBe = Math.atan2(cursorX - this.spotX, cursorY - this.spotY);
this.body.__dirtyRotation = true;
this.body.__dirtyPosition = true;
this.gun.__dirtyRotation = true;
this.gun.__dirtyPosition = true;
this.body.rotation.set(0, 0, avatarAngleToBe);
}
setTimeout(this.turnToMouse, time);
};
头像是构造函数。
本质上,这个函数只是不断地将头像(和它的枪)转向鼠标的方向。这个函数可以正常工作,因为它与我当前的工作函数完全相同(除了this's)。
我遇到的问题是,当我将此代码放入构造函数(即function Avatar() {})时:
this.turnToMouse();
应该调用我的函数,编辑认为没有函数'turnToMouse()'。
我错过了什么吗?
PS:如果您想查看我现在拥有的工作头像和对象头像的代码,click this link in google chrome。
【问题讨论】:
-
嗯,没有
Avatar.turnToMouse()函数,明明叫Avatar.prototype.turnToMouse()。唯一可以使用something.turnToMouse的情况是,如果您有一个使用new关键字创建的新实例
标签: javascript object three.js avatar