【发布时间】:2013-07-02 15:55:08
【问题描述】:
我遇到了问题。我将不胜感激。
我正在尝试从玩家位置射击到鼠标点击位置。代码没有给我任何错误,根据我的逻辑,它应该可以工作,但它没有
它创建了子弹对象,仅此而已。
//Bullets
function bullet(id, color, size, speed, x, y, eX, eY) {
this.id = id;
this.color = color;
this.size = size;
this.x = x;
this.y = y;
this.eX = eX;
this.eY = eY;
this.velocityX;
this.velocityY;
this.speed = speed;
}
var bulletList = [];
function addBullet(color, bsize, bspeed, x, y, eX, eY) {
bulletList[bulletId] = new bullet(bulletId, color, bsize, bspeed, x, y, eX, eY);
bulletId += 1;
}
function updateBullet(bullet, player)
{
var dx = (bullet.eX - player.x);
var dy = (bullet.eY - player.y);
var mag = Math.sqrt(dx * dx + dy * dy);
bullet.velocityX = (dx / mag) * speed;
bullet.velocityY = (dy / mag) * speed;
bullet.x += bullet.velocityX;
bullet.y += bullet.velocityY;
}
// Add event listener for `click` events.
canvas.onmousedown = function(e) {
addBullet("black", 10, 2, playerList[0].x, playerList[0].y, e.x, e.y);
}
//draw bullets (taken from drawFrame function)
$.each(bulletList, function (index, bullet) {
updateBullet(bullet, playerList[0]);
ctx.fillStyle = bullet.color;
ctx.fillRect(bullet.x, bullet.y, bullet.size, bullet.size);
});
【问题讨论】:
-
您能否在 jsfiddle.com 上提供该问题的演示?
-
这里是完整的代码:jsfiddle.net/LyUmZ,但由于某种原因,它没有画任何东西。
-
为我工作。你用的是什么浏览器?
-
试过 ff 和 chrome,没有为我画任何东西(jsfidle),你看到那里画的子弹了吗?
标签: javascript jquery mouse-cursor