Jump to content

Object that has events but no texture


Recommended Posts

Suppose I have an area of the game users can click and something happens. The area is not defined by a visible object. It doesn't have a texture like a sprite of image object would. 


That said, what sort of object should I use to create my area? It needs to have height/width, x/y and receive input events but it doesn't need a texture. Even though Phaser.Rectangle would be ideal, geometry isn't an option because geometry doesn't have events. Unless there is a way to attach input events to geom? Sprite and image isn't an option because it must have a texture. I could use a fake texture but I am looking to use an object that is as lightweight as possible as there will be a lot of these areas receiving input events. 

Link to comment
Share on other sites

You are slightly mistaken about Phaser.Image and Phaser.Sprite. They do not require a texture.

I use invisible images all the time.


Simply do not pass it anyting after the position, and you can adjust the width and height as much as you like after that.



This is taken from my current project, where I wanted to prevent all input passing below a certain level in the draw order:

this.eatMe = new Phaser.Image(this.game, 0, 0);this.eatMe.inputEnabled = true;this.eatMe.width = this.game.width;this.eatMe.height = this.game.height;this.eatMe.events.onInputDown.add(function(){}, this);this.add(this.eatMe);

This Phaser.Image eats all inputs, hence the name, and does nothing with them...

Link to comment
Share on other sites

You could use a rectangle:

this.game.input.onDown.add(function(pointer) {  if(Phaser.Rectangle.containsPoint(hitAreaRectangle, pointer.position)) {    // do something  }});

The event is on game.input but you could use your rectangle geometry to hit test the pointer and then execute whatever behaviour is meant to happen.

Link to comment
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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...