【发布时间】:2016-04-22 09:45:54
【问题描述】:
我有一个名为 snitch 的精灵,我试图让它在一个圆圈中移动,中心在 canvas/2(画布被命名为空间)和半径 30。我正在使用以下代码,但它没有似乎什么也没做,“告密者”只是站在那里,在坐标 (0,0) 上什么也不做。我尝试从 MySprite2 函数中删除这些 (0,0) 坐标,但是当我这样做时,飞贼甚至都没有出现。
var ctx = space.getContext("2d");
var FPS = 40;
var snitch= new MySprite2("http://i.imgur.com/IgNKTbW.png");
function MySprite2 (img_url) {
var x = this.x = 0;
var y = this.y = 0;
this.visible= true;
var img = this.MyImg2 = new Image();
this.MyImg2.src = img_url;
}
MySprite2.prototype.Do_Frame_Things = function() {
if (this.visible) ctx.drawImage(this.MyImg2, this.x, this.y);
};
function Do_a_Frame () {
ctx.clearRect(0, 0, space.width, space.height);
snitch.Do_Frame_Things();
}
function theMoves(snitch){
var theta = 0;
for (theta = 0; theta < 2 * Math.PI; theta+=0.1) {
snitch.x = (space.width/2) + Math.sin(theta)*30;
snitch.y = (space.height/2) + Math.cos(theta)*30;
}
}
setInterval(theMoves, 1000/FPS);
setInterval(Do_a_Frame, 1000/FPS);
知道有什么问题吗? 好的,它在移动,但现在它只是在一条直线上运行,而不是一个圆圈。下面是新代码。
function MySprite (img_url) {
var x = this.x = (space_x/2);
var y = this.y = (space_y/2);
var angle = this.angle = 0;
this.visible= true;
var img = this.MyImg = new Image();
this.MyImg.src = img_url;
}
MySprite.prototype.Do_Frame_Things = function() {
if (this.visible) ctx.drawImage(this.MyImg, this.x, this.y);
};
function theMoves(){
snitch.x += Math.cos(snitch.angle)*2;
snitch.y += Math.sin(snitch.angle)*2;
}
setInterval(theMoves, 40);
【问题讨论】:
标签: javascript animation canvas path geometry