mrpetem Posted February 14, 2015 Share Posted February 14, 2015 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 More sharing options...
rich Posted February 15, 2015 Share Posted February 15, 2015 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 More sharing options...
mrpetem Posted February 16, 2015 Author Share Posted February 16, 2015 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.htmlthis.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 mouseWhen 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 More sharing options...
rich Posted February 16, 2015 Share Posted February 16, 2015 I wouldn't use physics / touching to handle clicking a block and breaking it if next to the player. I would just use a simple grid system (see the Phaser Coding Tips 5 for an example that would work, with a little tweaking) Link to comment Share on other sites More sharing options...
mrpetem Posted February 16, 2015 Author Share Posted February 16, 2015 Ok that is a certainly different approach. Will have to study form. Thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts