Jump to content

PixiJS - Sprite hitArea Question/Bug


Recommended Posts

I am playing with hit areas and trying to constrain the amount of surface area that registers a mouse event. I have it working well on Graphics, but it does not seem to work similarly in Sprites. Here is my code showing it working on a graphic, but failing on a sprite.

    const app = new PIXI.Application({ width: 800, height: 600, backgroundColor: 0x1099bb });
    document.body.appendChild(app.view);

    // Setup a test graphic as a rectangle
    const testRect = new PIXI.Graphics();
    testRect.beginFill(0x00FF00);
    testRect.drawRect(0, 0, 100, 100);
    testRect.endFill();
    testRect.position.set(50, 50);
    app.stage.addChild(testRect);

    // Now make it interactive and set the hit area slightly smaller thant the boundary of our graphic
    testRect.interactive = true;
    testRect.hitArea = new PIXI.Rectangle(20, 20, 60, 60);

    // And set the mousedown handler
    testRect.on('mousedown', mousedown);

    // Now create our pixi sprite object
    const texture = PIXI.Texture.from("assets/cartoon-tiger.png");
    const newSprite = new PIXI.Sprite(texture);
    newSprite.x = 300;
    newSprite.y = 300;
    newSprite.width = 200;
    newSprite.height = 200;
    app.stage.addChild(newSprite);

    // Make it interactive and define a small hit area compared to our bigger sprite
    newSprite.interactive = true;
    newSprite.hitarea = new PIXI.Rectangle(25, 25, 150, 150);

    // Finally mousedown handler
    newSprite.on('mousedown', mousedown);

    function mousedown(event) {
        this.data = event.data;

        console.log("Mousedown Local coordinates");
        console.log(this.data.getLocalPosition(event.currentTarget));
    }

If you run this, you will see that when you click on the graphic, the hitarea is working and mouse events on the outer edge of the graphic object do not register. But the same code with hit area does not work with a sprite. IE, any where you click on the sprite registers a mouse event.

Any suggestions on where I am going astray?

Thanks,

Brent

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...