Ris*_*ran 2 javascript pixi.js
我正在尝试获取鼠标当前悬停的形状的ID。
我的形状在容器中
// creating the layers
gridLayer = new PIXI.DisplayObjectContainer ();
gridLayer.setInteractive(true);
stage.addChild(gridLayer);
Run Code Online (Sandbox Code Playgroud)
我正在创建每个这样的形状;
function drawHexagon(x,y, size, gap,scale, color, iterI, iterJ, type) {
var shape = new PIXI.Graphics();
// set a fill and line style
shape.beginFill(color);
shape.lineStyle(1, 0xa0a0a0, 1);
size = size-gap;
for (i = 0; i < 7; i++) {
angle = 2 * Math.PI / 6 * (i + 0.5);
var x_i = x + size * Math.cos(angle);
var y_i = y + size * Math.sin(angle);
if (i === 0) {
shape.moveTo(x_i, scale *y_i)
}
else {
shape.lineTo(x_i, scale * y_i)
}
};
shape.endFill();
// calculate and save the axial coordinates
var cX = iterJ - (iterI - (iterI&1)) / 2;
var cZ = iterI;
var cY = -1*(cX+cZ);
shape.hexId = cX + "x" + cY + "y" + cZ + "z";
shape.hexPosX = x;
shape.hexPosY = y;
shape.setInteractive(true);
shape.mouseover = function(mouseData){
console.log("MOUSE OVER " + shape.hexId);
}
shape.click = function(mouseData){
console.log("MOUSE CLICK " + shape.hexId);
}
gridLayer.addChild(shape);
}
Run Code Online (Sandbox Code Playgroud)
但是,单击任何形状或将其悬停在控制台上都不会显示任何内容。我究竟做错了什么?
我都尝试过
shape.setInteractive(true)
Run Code Online (Sandbox Code Playgroud)
和
shape.interactive = true
Run Code Online (Sandbox Code Playgroud)
但似乎都不适合我。
编辑:我添加了一个jsfiddle。它不起作用(我不知道如何在jsfiddle中进行链接),但是您可以在其中看到我的整个代码。 http://jsfiddle.net/9aqHz/1/
为了使PIXI.Graphics对象具有交互性,您需要设置一个hitArea形状(可以是Rectangle,Circle或Polygon):
shape.hitArea = new PIXI.Polygon([
new PIXI.Point(/* first point */),
new PIXI.Point(/* second point */),
new PIXI.Point(/* third point */),
new PIXI.Point(/* fourth point */),
new PIXI.Point(/* fifth point */)
]);
Run Code Online (Sandbox Code Playgroud)
另一种方法是从形状生成纹理并使用Sprite,但是命中区域将是六边形的整个矩形边界:
var texture = shape.generateTexture();
var sprite = new PIXI.Sprite(texture);
sprite.setInteractive(true);
sprite.anchor.set(0.5, 0.5);
Run Code Online (Sandbox Code Playgroud)