Brent Cromley Posted March 31, 2021 Share Posted March 31, 2021 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.