Jump to content

Buttons with different sizes based on size of text


alarian
 Share

Recommended Posts

Best way is extending Phaser.Sprite/Phaser.Image/Phaser.Button object, passing a JSON file with all your params.

Or you can use a function similar to this one:

function create() {game.stage.backgroundColor = '#104111';createPropButton(10,10,"Tiny");createPropButton(10,55,"- Medium -");createPropButton(10,100, "BIG FAAAAAT BUTTON");//just for "doSomething" function:message=game.add.text(5, 160, "--", {font: 'bold 15pt Courier', fill: '#2345AF', strokeThickness: 2});}function createPropButton(x, y, text) {    var width=30+text.length*16;    var propButton=game.add.graphics(0, 0);    propButton.lineStyle(2, 0x222222, 0.8);    propButton.beginFill(0xDDDDFF, 0.8);    propButton.drawRoundedRect(x, y, width, 30, 3);        propButton.text = game.add.text(x+propButton.width/2, 1+y+propButton.height/2, text);    propButton.text.anchor.setTo(0.5, 0.5);    propButton.inputEnabled = true;    propButton.events.onInputDown.add(doSomething, this);    propButton.events.onInputOver.add(buttonOver, this);    propButton.events.onInputOut.add(buttonOut, this);        //just for "doSomething" function:    propButton.name=text;}function doSomething(button) {    message.text = button.name;}function buttonOver(button) {button.lineStyle(2, 0xEDDE33, 0.9);button.beginFill(0xFF3333, 0.8);button.drawRoundedRect(button.graphicsData[0].shape.x, button.graphicsData[0].shape.y, button.graphicsData[0].shape.width, button.graphicsData[0].shape.height, 3);}function buttonOut(button) {button.lineStyle(2, 0x222222, 0.8);button.beginFill(0xDDDDFF, 0.8);button.drawRoundedRect(button.graphicsData[0].shape.x, button.graphicsData[0].shape.y, button.graphicsData[0].shape.width, button.graphicsData[0].shape.height, 3);}
Link to comment
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...
 Share

  • Recently Browsing   0 members

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