Jump to content

Deep forEach to iterate over all sprites


OpherV
 Share

Recommended Posts

What would be the best way to iterate over all sprites?

 

I'm placing sprites randomly on screen and I don't want the newly placed elements to collide with any other elements.

My sprites are nested in their own groups sets, so a shallow forEach on game.world will not help in this case...

Link to comment
Share on other sites

I'm doing a game that puts sprites at random positions and I that can't collide too.

 

I solved this problem doing the following:

 

1 - I have a matrix of sprites

 

2 - A matrix of positions

 

3 - I created a matrix of n lines x m columns and I have some functions that generate random positions (and assure that repeated positions don't be used). These positions are put in the matrix of positions.

 

4 - Then I randomly assign the positions in the matrix for sprites in the sprites array.

Link to comment
Share on other sites

  • 8 months later...

I didn't understand your question quite well, so I'm answering what think you asked:

I know how many sprites I want to show in the screen then I create each sprite and add it to.the array.

To access an element I do something like tile.x, tile.width, and so on.

For the positions I do the following:

Given the screen dimensions, sprite dimensions and space between sprites (so they don't overlap), I determine how many sprites in a row and in a column I can have.

The matrix contains the number of rows x number of columns elements that are the same as the number of positions available for sprites.

Then I determine 2 functions (say getX and getY) that will return position in pixels given the position number.

These are used to set sprite's x and y position.

Notice that the screen dimensions used in the calculation above does not include space needed for HUD

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