简体   繁体   English

如何在Pixijs中与形状互动?

[英]How to interact with shapes in Pixijs?

I am trying to get the id of the shape my mouse is currently hovering over. 我正在尝试获取鼠标当前悬停的形状的ID。

my shapes are in a container 我的形状在容器中

// creating the layers
gridLayer = new PIXI.DisplayObjectContainer ();
gridLayer.setInteractive(true);
stage.addChild(gridLayer);

and i am creating each shape like this; 我正在创建每个这样的形状;

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);
}

However, clicking on any shape or hovering over it is not showing me anything in the console. 但是,单击任何形状或将其悬停在控制台上都不会显示任何内容。 what am i doing wrong? 我究竟做错了什么?

i have tried both 我都尝试过

shape.setInteractive(true)

and

shape.interactive = true

but neither seems to work for me. 但似乎都不适合我。

EDIT: i have added a jsfiddle. 编辑:我添加了一个jsfiddle。 it doesnt works (i dont know how to link things in jsfiddle) but you can see my entire code in there. 它不起作用(我不知道如何在jsfiddle中进行链接),但是您可以在其中看到我的整个代码。 http://jsfiddle.net/9aqHz/1/ http://jsfiddle.net/9aqHz/1/

For a PIXI.Graphics object to be interactive you need to set a hitArea shape (it can be a Rectangle, Circle or a Polygon): 为了使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 */)
]);

Another approach would be to generate a texture from the shape and use a Sprite, but the hit area would be the entire rectangular bounds of the hexagon: 另一种方法是从形状生成纹理并使用Sprite,但是命中区域将是六边形的整个矩形边界:

var texture = shape.generateTexture();
var sprite = new PIXI.Sprite(texture);
sprite.setInteractive(true);
sprite.anchor.set(0.5, 0.5);

Fiddle with this applied to your example 摆弄这个例子

@imcg I updated your code so it workes with Pixi 3.0.8 @imcg我更新了您的代码,使其可与Pixi 3.0.8一起使用

- sprite.setInteractive(true);
+ shape.interactive = true;
+ shape.buttonMode = true;

- sprite.setInteractive(true)
+ sprite.interactive = true;
+ sprite.buttonMode = true;

http://jsfiddle.net/LP2j8/56/ http://jsfiddle.net/LP2j8/56/

I will add a bit of info for anyone who is in the same boat i was in; 我将为我所在的同一条船上的任何人添加一些信息;

When you define a shape as a geom, you have to explicitly state a hitarea. 当您将形状定义为几何图形时,必须明确声明一个区域。

So adding the following code makes it work; 因此,添加以下代码即可使其工作;

shape.hitArea = new PIXI.Polygon(vertices);
shape.interactive = true;

shape.click = function(mouseData){
   console.log("MOUSE CLICK " + shape.hexId);
}

But, when you define a shape as a sprite/texture, you dont need to do this. 但是,当您将形状定义为子图形/纹理时,无需执行此操作。 in cases of sprites, just setting shape.interactive = true for the sprite is sufficient. 对于Sprite,只需将shape.interactive = true设置为Sprite就足够了。 You dont need to set the interactive property for the parent object or the stage. 您无需为父对象或舞台设置交互式属性。

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

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