JS - Shooting - mouse

I run into a problem. I would appreciate any help. I am trying to shoot from a player position to mouse click position. Code gives me no error and according to my logic, it should work, but it doesn't It creates the bullet object, but thats all. //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); });
Cna you provide a demo of the issue on jsfiddle.com ?

以上就是JS - Shooting - mouse的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏