Jump to content

Problem Detecting Object Clicked


mrpetem
 Share

Recommended Posts

Very new to phaser and HTML5 game development so please bare with me.

 

I am having difficulty detecting when the mouse pointer is clicked on a sprite object. I am using MightyEditor so not sure if it could be an issue with this. I have also searched and searched to see why.

 

I am using the following:

create: function(){   this.blocks = mt.create("blocks");   this.blocks.inputEnabled = true;},update: function(){   this.blocks.events.onInputUp.add(this.blockClicked, this);},blockClicked : function(){   // DO STUFF HERE}

I get the following error message: 

 

Uncaught TypeError: Cannot read property 'onInputUp' of undefined

 

Why would it appear that events is undefined?

 

Anyones help would be greatly appreciated. What I am actually trying to achieve is:

 

The user clicks on a block, but the block can only be destroyed if the player is next to it. I have collision detection on the player and the 'blocks' but I only want the player to affect one of the blocks that is clicked on with the mouse. Hopefully I am going about this the right way.

 

Thanks

Link to comment
Share on other sites

onInputUp is fine - the problem is you're doing this every single frame, every second as your game runs. Event handlers only need to be setup once - so do it after you've inputEnabled the Sprite in your create method.

 

Also it's not obvious what 'blocks' is, but if it's a Group then you cannot inputEnable a whole Group. Just Sprites.

Link to comment
Share on other sites

Ah ok gotcha.

 

I believe the blocks are actually a group not a sprite. So I guess that is my issue here.

 

I have actually used the following now, however not sure if this is ideal? Should I be using a sprite instead for the blocks, considering I would like them to start cracking? (I am adapting the following tutorial: http://mightyfingers.com/blog/tutorial-creating-mini-game-with-editor/)

 

My game so far: http://mightyeditor.mightyfingers.com/data/projects/p27nw/phaser/index.html

this.game.physics.arcade.collide(this.character, this.blocks, 		function(character, block) 		{			block.inputEnabled = true;						if(this.input.activePointer.isDown)			{				if(block.body.touching.right && block.input.pointerOver())				{					this.destroyBlock(block);				}				else if(block.body.touching.left && block.input.pointerOver())				{					this.destroyBlock(block);				}				else if(block.body.touching.up && block.input.pointerOver())				{					this.destroyBlock(block);				}				else if(block.body.touching.down && block.input.pointerOver())				{					this.destroyBlock(block);				}			}		}, null, this);

Appreciate your help Rich, and very excited about this game development I am getting into. However could you possibly advise me on the correct way to try and do what I am achieving, which is:

 

  • Character moves with mouse
  • When next to a block and also the mouse pointer clicks on that block, the block takes damage (4 attempts to break it).

 

Thanks

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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