【发布时间】:2014-07-31 10:23:59
【问题描述】:
我的精灵图像将在画布上随机移动。但是在我按下画布后,一个对象会出现,并且精灵图像将以之字形方式向它移动。但我希望精灵直接向对象移动。
我的精灵图像如何移动到我在画布上按下后创建的图像:
Fish.prototype.chaseFood = function(index) {
if (this.xPos > foodArray[index].x + foodWidth) {
this.speedX = -1 * Math.abs(this.speedX);
} else if (this.xPos < foodArray[index].x) {
this.speedX = Math.abs(this.speedX);
}
if (this.yPos > foodArray[index].y + foodHeight) {
this.speedY = -1 * Math.abs(this.speedY);
} else if (this.yPos < foodArray[index].y) {
this.speedY = Math.abs(this.speedY);
}
};
我知道有一种方法是通过计算精灵点和对象之间的线的斜率来让我的精灵图像移动。但是试了之后还是拿不到。有人知道如何实现吗?
【问题讨论】:
标签: javascript html canvas sprite