Bakshish

Text taking click without adding input

Recommended Posts

Hi,

I have a button in my game which has two child elements(a text aligned top center in and another text aligned bottom center). The issue is the child texts are also taking click of the button which should not happen. Here is the code for this:

Fiddle : https://jsfiddle.net/3sf1b2zL/18/

var game = new Phaser.Game(480,320,Phaser.AUTO,'');
var gameStates = {};

gameStates.Main = function(game){};

gameStates.Main.prototype = {
	preload: function() {
           this.load.crossOrigin = "Anonymous";
           this.load.image('shop','https://s1.postimg.org/6fz3duzc0r/user_input_box.png'); 
	},

	create:function(){
	   var btnText = this.add.text(0,0,"Game Loaded",{fill:"#ffffff"});
           var infoText = this.add.text(0,0,"This is a description of the game. It is only an informative text and it should not take click of its parent button. Try clicking this text",{fill:"#ffffff", wordWrap:true,wordWrapWidth:450});
	   var shopBtn = this.add.button(0,0,'shop', function(){
	   btnText.text = "Shop Clicked";}, this);
           shopBtn.anchor.setTo(0.5, 0);    
           shopBtn.addChild(infoText);
           shopBtn.addChild(btnText);
           btnText.alignTo(shopBtn, Phaser.TOP_CENTER);
           infoText.alignTo(shopBtn, Phaser.BOTTOM_CENTER,0,20);
           shopBtn.x = this.game.width/2;
           shopBtn.y = this.game.height*0.2;
	}
}

game.state.add('main',gameStates.Main);
game.state.start('main');

Please help me so that only the button takes click and text should not take the click

 

Thanks

Share this post


Link to post
Share on other sites

The text objects accept input because they are added as the button's children: https://jsfiddle.net/2fet6pLf/

In your case, the hierarchy also seems a bit incorrect logically. The texts and the button should have a common parent (perhaps, call it a TextButton :)).

//TextButton pseudocode

TextButton : Sprite {
 
 titleText: Phaser.Text   //TOP_CENTER
 descText: Phaser.Text    //CENTER
 button: Phaser.Button    //BOTTOM_CENTER

 TextButton(){
    this.addChild(titleText);
    this.addChild(button);
    this.addChild(descText);
 }

}

 

 

Share this post


Link to post
Share on other sites

Ok, thanks for your suggestion. I made the text child to the button intentionally to keep the alignment uniform on different resolutions. Can you also suggest me how should I keep resolution independence with this hierarchy ?

Share this post


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.