Jump to content

Can I set the group width?


sinanqd10
 Share

Recommended Posts

@lewster32,

for my purpose is that,

I have created sprite in order

Eg: sprite 1 sprite 2 sprite 3 sprite 4 then sprite 5 i want to break the line into second row. Therefore, can i get number of sprites as total number and specific frame of sprite in order to count the different frame in the group?

Could you show example related to my question? Thanks in advance bro.

Link to comment
Share on other sites

This is basically a grid layout, which is usually done with nested for loops for rows and columns, and a value for the grid/cell size:

var sprite, gridSize = 32;for (var rows = 0; rows < 4; rows++) {  for (var cols = 0; cols < 6; cols++) {    sprite = game.add.sprite(rows * gridSize, cols * gridSize, 'sprite');  }}
Link to comment
Share on other sites

Your best bet here is to create a separation between your data and what you render. Maybe start off by defining your grid as a 2-dimensional array:

var grid = [  [1, 2, 2],  [2, 1, 3],  [1, 2, 3],  [3, 2, 1]];

Then create a function which renders the grid:

var gridGroup = game.add.group();function generateGrid(grid, size) {  size = size || 32; // default size of 32 pixels for the grid  var col, row;  for (row = 0; row < grid.length; row++) {    for (col = 0; col < grid[row].length; col++) {      // draw the sprite with the frame number specified in the appropriate grid cell      gridGroup.create(row * size, col * size, 'sprite', grid[row][col]);    }  }}generateGrid(grid, 32);

If you need to update the grid afterwards, you'll probably want to create another 2-dimensional array to store the created sprites in the same way, and then just change their .frame or .frameName as appropriate, rather than destroy and recreate the entire group.

Link to comment
Share on other sites

Forgot to answer your other question - to work out how many of a particular number is in a row or column, try something like this:

function countInRow(grid, row, number) {  return grid[row].filter(function(n) { return n === number; }).length;}function countInColumn(grid, column, number) {  var found = 0;  for (var row = 0; row < grid.length; row++) {    if (grid[row][column] === number) {      found++;    }  }  return found;}

Remember arrays are zero indexed, so the first row or column is 0, not 1!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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