Ivasik

How to add anchor.set for cutom button?

Recommended Posts

Hi, i have a custom buttons and i want apply anchor.  How to do it?

Custom buttons:

this.button_start = this.btn("Play a game", this.play_game, 2.03, 15.1, 2, 8.5, 140, 30);

	btn: function(name, functions, anc1X, anc1Y, anc2X, anc2Y, w, h) {
		this.button = this.game.add.button(this.game.width, this.game.height, '', functions, this);
		this.button.name = name;
		this.button.width = w;
		this.button.height = h;
		this.button.anchor.set(anc1X, anc1Y);//Temporary
		
		this.text = this.add.bitmapText(this.game.width, this.game.height, 'Text_mainmenu', this.button.name, 46);
		this.text.anchor.set(anc2X, anc2Y);//Temporary
	}
/*
name - the name of the button
functions - button function

anc1X - first anchor.setX
anc1Y - first anchor.setY

anc2X - two anchor.setX
anc2Y - two anchor.setY
*/

 

Share this post


Link to post
Share on other sites

Hello, the correct code is:

anchor.setTo(dataX, dataY);

But you in the add function have the position at: (this.game.width, this.game.height) the total position not show button, is out of screen, its better use (this.game.world.centerX, this.game.world.centerY).

Share this post


Link to post
Share on other sites
2 hours ago, cardex107 said:

Hello, the correct code is:


anchor.setTo(dataX, dataY);

But you in the add function have the position at: (this.game.width, this.game.height) the total position not show button, is out of screen, its better use (this.game.world.centerX, this.game.world.centerY).

this.game.width, this.game.height  you need to resize the elements do not remain in place and has changed his position.

//example
this.play_game= this.add.button(this.game.width, this.game.height, 'test_button', this.play_game_functions, this);
this.play_game.anchor.set(1.65, 7.5);

resize: function(width, height) {
    this.play_game.x = this.game.width;
    this.play_game.y = this.game.height;
}

 

Share this post


Link to post
Share on other sites
2 minutes ago, Ivasik said:

this.game.width, this.game.height  you need to resize the elements do not remain in place and has changed his position.


//example
this.play_game= this.add.button(this.game.width, this.game.height, 'test_button', this.play_game_functions, this);
this.play_game.anchor.set(1.65, 7.5);

resize: function(width, height) {
    this.play_game.x = this.game.width;
    this.play_game.y = this.game.height;
}

 

ok, only need anchor.setTo

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.