我想将两个Canvas'元素叠加在一起.它们用于游戏,其中较低的Canvas将显示hexgrid,而上部Canvas旨在显示问题/命令(基本上是箭头或十六进制突出显示 - 我不想重绘大量的六角形数十亿次) .
画布由zIndex 1/2分层.
但问题是:顶部Canvas上显示的顺序应该是为了响应"click"和"mousemove"事件而绘制到底部Canvas.
显然,通过在"hexgrid"画布上层叠"命令"Canvas,任何mousemove和click事件都不会被"hexgrid"画布触发,因为它不再接收事件.
有没有办法"转发",即将事件从一个元素传播到另一个元素,或者什么是解决我的问题的另一种智能方法?
我在循环中创建输入元素.单击时,这些按钮应根据按钮元素的初始创建调用具有特定参数的特定函数.实际上发生的事情是所有按钮总是使用LAST按钮中的参数调用该函数.
for (var i = 0; i < planets.length; i++){
var id = planets[i].id;
var input = document.createElement("input");
input.type = "button";
input.className = "worldButton";
input.value = "Choose this world";
Input.onclick = function(){
game.pickWorld(planets, id);
}
}
Run Code Online (Sandbox Code Playgroud)
每个按钮似乎总是传递行星的id [planets.length]而不是planets[i].
console.log(id)是正确的.它没有问题,如果使用onclick,addEventListener或者jquery.click.
然而,整个行星数组成功传递.
如何让Input正确传递特定迭代的id?
我有一个数组变量,由对象填充.我需要通过array [i] .target对这个数组进行排序.然后由数组[i] .weaponPriority二次我可以用一个值对数组进行排序,但遗憾的是我无法理解如何进一步优化它.
请指教.
var ships = []
function Ship(name, target, priority){
this.name = name;
this.target = target;
this.id = ships.length;
this.weaponPriority = priority;
}
var ship = new Ship("Alpha", 10, 3);
ships.push(ship);
var ship = new Ship("Beta", 10, 1);
ships.push(ship);
var ship = new Ship("Gamma", 10, 3);
ships.push(ship);
var ship = new Ship("Delta", 15, 2);
ships.push(ship);
function log(){
for (var i = 0; i < ships.length; i++){
var shippy = ships[i];
console.log(shippy.name + " ---, targetID: " + …Run Code Online (Sandbox Code Playgroud)