Jump to content

How to add anchor.set for cutom button?


Ivasik
 Share

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
*/

 

Link to comment
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;
}

 

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

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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