简体   繁体   English

画布-鼠标指向的项目符号

[英]Canvas - bullets to mouse point

So I'm trying to implement shooting, and what I want to do is take the players x and y and use that as the base point where the projectile derives from and then it basically goes to the point pressed in the canvas. 因此,我正在尝试实施射击,而我想要做的是将玩家x和y用作射弹所源自的基点,然后基本上到达画布上按下的点。 The projectile is deriving from the player, but instead of going to the cursor point it strangely ALWAYS goes right, but will go right+up or right+down depending on which direction was pressed; 射弹是从玩家那里衍生出来的,但是它并不会一直指向光标,而是总是向右走,而是根据被按下的方向向右上移或右下移。 I've looked on-line and can't seem to find the answer. 我已经上网查看了,似乎找不到答案。

Inside my javascript here is the shoot function: 在我的JavaScript中,这里是Shoot函数:

function shoot(event){
    bullets[bulletCount] = new Array(4);
    bullets[bulletCount][0] = x;
    bullets[bulletCount][1] = y;
    bullets[bulletCount][2] = window.event.clientX;
    bullets[bulletCount][3] = window.event.clientY;
    bulletCount++;
}

In my javascript here is the bullet part in the update method: 在我的JavaScript中,这是update方法的项目符号部分:

for(var b = 0; b < bullets.length; b++){
        if(bullets[b][0] < bullets[b][2]) bullets[b][0] += 5;
        if(bullets[b][0] > bullets[b][2]) bullets[b][0] -= 5;
        if(bullets[b][1] < bullets[b][3]) bullets[b][1] += 5;
        if(bullets[b][1] > bullets[b][3]) bullets[b][1] -= 5;
        ctx.fillRect(bullets[b][0],bullets[b][1], 8, 8);
    }

and in my index.html page: 在我的index.html页面中:

<canvas id="gameBoard" onClick="shoot(event)" width="500" height="500" tabindex="1"></canvas>

EDIT: fixed the problem, for anyone else who suffers this problem inside the shoot function change from what was originally up there to this: 编辑:修复了该问题,对于其他任何在射击功能更改中受苦的人都从原来的位置更改为此:

function shoot(event){
    var rect = canvas.getBoundingClientRect();
    bullets[bulletCount] = new Array(4);
    bullets[bulletCount][0] = x;
    bullets[bulletCount][1] = y;
    bullets[bulletCount][2] = event.clientX - rect.left;
    bullets[bulletCount][3] = event.clientY - rect.top;
    bulletCount++;
} 

The problem lies within these two lines: 问题在于以下两行:

bullets[bulletCount][2] = window.event.clientX;
bullets[bulletCount][3] = window.event.clientY;

It is because that the location you save for the click is not the real location. 这是因为您为点击保存的位置不是真实位置。

You see, event.clientX/Y give you the mouse coordinates relative to the top left corner of the window , but you only want the coordinates relative to the top left corner of your canvas element . 您会看到, event.clientX/Y为您提供相对于窗口左上角的鼠标坐标,但是您只需要相对于canvas元素左上角的坐标。

To fix this, first get to know another way to get the mouse coordinates, which is event.pageX/Y . 要解决此问题,请首先了解另一种获取鼠标坐标的方法,即event.pageX/Y

This will not give you the coordinates relative to the canvas, however you need this instead of event.clientX/Y because it will give you the mouse coordinates relative to the top left corner of the page, not the window , which means it doesn't matter if the user scrolls somewhere, the coordinates are always true to the page. 不会为您提供相对于画布的坐标,但是您需要它而不是event.clientX/Y因为它将为您提供相对于页面左上角(而不是窗口)的鼠标坐标,这意味着它不会无论用户是否滚动到某个位置,坐标始终都与页面一致。

So, now that you're using event.pageX/Y , how to make it relative to the canvas? 那么,既然您正在使用event.pageX/Y ,如何使其相对于画布? Well, we can just take these coordinates and subtract the top left corner coordinates of the canvas : 好吧,我们可以获取这些坐标并减去画布的左上角坐标

bullets[bulletCount][2] = window.event.pageX - canvas.offsetLeft;
bullets[bulletCount][3] = window.event.pageY - canvas.offsetTop;
  • Assuming that your canvas element is saved in a variable named canvas 假设您的canvas元素保存在名为 canvas 的变量中

Hope that solves it :D 希望能解决它:D

    var bullets = []; 
    var bullet_speed = 10;   

call this in your init() fucntion: which could have your setInterval. 在您的init()函数中调用此函数:可以使用setInterval。

    window.addEventListener('click', shoot, false);

call this in your draw function: 在draw函数中调用此函数:

for (var i = 0; i < bullets.length; i++){
    bullets[i].x += bullets[i].xChange;
    bullets[i].y += bullets[i].yChange;
    context.fillStyle ='black';
    context.fillRect(bullets[i].x,bullets[i].y,4,4)
  }

function shoot(event){
    x = event.offsetX
    y = event.offsetY
    d = Math.sqrt(Math.pow(Math.abs(player.x-x),2)+Math.pow(Math.abs(player.y-y),2))
    bullet = {
        x : player.x,
        y : player.y,
        xChange : (x-player.x)/(d/bullet_speed),
        yChange : (y-player.y)/(d/bullet_speed),
    }; 
    bullets.push(bullet);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM