我正在尝试转换一个旧的模拟从Flash到createjs与动画抄送。你唯一的代码是旋转一块,但我不能让它工作。此代码不起作用:
function spinit()
{
var ang = myangle * Math.PI / 180;
this.pin1.x = disk1.x + R * Math.cos(ang);
this.pin1.y = disk1.y + R * Math.sin(ang);
this.yoke1.x = pin1.x;
this.disk1.rotate = myangle;
this.myangle = myangle + 1;
if (myangle > 360)
{
myangle = 0;
}
}
var myangle = 0;
var R = 90;
setInterval(spinit, 5);发布于 2018-02-14 01:11:40
很有可能这是一个范围问题。您的spinit方法将被匿名调用,因此它将无法访问使用this引用的任何框架内容。你可以通过限定你的方法的作用域并绑定你的setInterval调用来解决这个问题。
this.spinit = function() // 1. scope the function
{
var ang = myangle * Math.PI / 180;
this.pin1.x = disk1.x + R * Math.cos(ang);
// etc
setInterval(spinit.bind(this), 5); // 2. bind this so it calls in the right scope.
}一定要给this.spinit()打电话。
希望这能有所帮助!
https://stackoverflow.com/questions/48764995
复制相似问题