Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Array of Buttons


Hightower
 Share

Recommended Posts

Hi, I have a grid of buttons ie:

 

B | B | B

B | B | B
B | B | B

 

I would like to get these in an array so I can run checks against them and surrounding buttons, but whenever I do something like:

button[x][y] = game.add.button(...)

My game just has a black screen, where as

button = game.add.button(...)

works fine.

 

The reason I thought of putting them in an array is so say you click on 0,0 I can then check the status (and change the texture if needed) of 0,1

 

Can anybody help?

 

Thanks,

Link to comment
Share on other sites

This might be a dumb suggestion... but how are you constructing the 2d array before adding the buttons to it?

 

Try something like this:

var buttons = [];for (var rowIndex = 0; rowIndex < numRows; rowIndex++) {  var row = [];  for (var colIndex = 0; colIndex < numCols; colIndex++) {    row.push(game.add.button(...));  }  buttons.push(row);}
Link to comment
Share on other sites

 

This might be a dumb suggestion... but how are you constructing the 2d array before adding the buttons to it?

 

Try something like this:

var buttons = [];for (var rowIndex = 0; rowIndex < numRows; rowIndex++) {  var row = [];  for (var colIndex = 0; colIndex < numCols; colIndex++) {    row.push(game.add.button(...));  }  buttons.push(row);}

 

That seems to have done it thanks. Will take a look at how to make it all fit tomorrow now.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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