Jump to content

Questions about Button Attributes/Sending Information


Chandleezy
 Share

Recommended Posts

So I downloaded Phaser yesterday and went through a couple of the examples but I can't find a suitable answer to a couple of questions I have.
 
I'm trying to create a pretty basic version of the game Battleship in Phaser and I decided that an array of buttons would be suitable to show where the user can guess the other players ships by clicking. I've created a grid of buttons that I can click but I want to know how I can get information about the button through a click. For instance if I have a 9x9 grid of 81 buttons and I click one I want to know if its button 54 or 37 or 23 and I want to send that information to a server.
 
I don't know enough about Buttons to figure out how to get this information from a click so I'm wondering if you guys can help me with this noob question.

 

var numRows = 9;
var numCols = 9;var game = new Phaser.Game(1536, 758, Phaser.AUTO, '', { preload: preload, create: create, update: update});function preload() {        game.load.image('0', 'images/gridsquare.png', 55, 60);     game.load.image('background', 'images/battleship2.png');}        var buttons = [];    var button;    var background;    function create() {    background = game.add.sprite(0, 0, 'background');   // button =  game.add.button(700, 100, '0', actionOnClick, this, 2, 1, 0);   // button.name = '0';    generateMainGrid();}function update() {}  function actionOnClick () {   // background.visible =! background.visible;    alert('this is grid number ' + this.name);}    function generateMainGrid(){    for (var row = 0; row < numRows; row++) {        for (var col = 0; col < numCols; col++) {                        buttons.push(game.add.button(700+55*row, 100+60*col, '0', actionOnClick, this, 2, 1, 0));        }    }}
 

As you can see I'm trying to call this.name in the function actionOnClick to alert me of the buttons name but it doesn't work. I'm not sure if '0' constitutes the name of a button or something else. Any help is appreciated!

Link to comment
Share on other sites

When a button is clicked the callback is set 2 things automatically: The pointer that clicked it (as it could be any number of pointers in a multi-touch system) and also the button that was clicked :)

 

So you can simply use that button reference to determine which one in the grid it is. Maybe parse the x/y coordinates, maybe give the button a name? Button.name = "8" could signify it's the 8th button along in the grid?

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...