我刚刚开始拼凑一个简单的游戏,你的一个点需要食物,否则你就会死。很简单,这只是一件有趣的事情。
我正在尝试一种不同于我通常所做的方法,即角色旋转然后移动。
当角色旋转时,它移动一定的量,然后在另一个方向上旋转一定的量。然后重复。我如何让它只旋转一个方向,而不是交替旋转(而且整个过程都在一个大致的方向上)。
旋转脚本:
self.rotationy = 0;
self.rotationx = 0;
self.turnLeft = function() {
self.rotationy -= 0.1;
};
self.turnRight = function() {
self.rotationy += 0.1;
};
self.move = function() {
var posX = Math.sin(self.rotationy);
var posZ = Math.cos(self.rotationy);
self.locationx += posX + 3;
self.locationy += posZ + 3;
};
如果它很重要,下面是更新脚本:
self.update = function() {
self.hunger -= 0.003;
self.updateColour();
if (self.hunger <= 0) {
self.die();
} else if (self.hunger >= 1) {
self.die();
}
if (self.locationx <= 20) {
self.locationx = canvas.width - 21;
} else if (self.locationx >= canvas.width - 20) {
self.locationx = 21;
} else if (self.locationy <= 20) {
self.locationy = canvas.height - 21;
} else if (self.locationy >= canvas.height) {
self.locationy = 21;
}
drawCircle(ctx, self.locationx, self.locationy, 20, self.colour);
};
self.die = function() {
};
setInterval(self.update, 50);
};
这是一个JSFiddle: http://jsfiddle.net/hzhubf8o/
还请注意,我没有创建一个函数来清除所使用的路径。
提前感谢!
感谢Siguza,我有了答案(把它贴在这里以防有人有类似的问题)
我添加了+3,这使得字符沿着正数移动。它应该让他走得更快,而不是应该是* 3(也是他建议的)。
self.locationx += posX + 3;
self.locationy += posZ + 3;
更改:
self.locationx += posX * 3;
self.locationy += posZ * 3;