Jump to content

Can I set the group width?


Recommended Posts

Yeah, a group's dimensions are derived from what's inside it - you only set its position normally. If you want objects to wrap, you'll have to put some custom logic in your code to explicitly handle this; ideally by extending Phaser.Group like in this example: http://jsfiddle.net/lewster32/skhsbkk4/

Link to comment
Share on other sites


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

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.

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.


  • Recently Browsing   0 members

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