# Count Number of Objects Nearby

Get help using Construct 2

### » Fri Jun 29, 2012 5:12 am

I'm making a game like Minesweeper, but I can't think of a way to have the game count the bombs around each space. I am using a 64px x 64px grid. Each space has a sprite called Rock (because it looks like a square rock). At the start of the layout, it will For Each through all of the rocks twice. Once to randomly place bombs (without putting more than one on the same spot by accident), and a second time to put Kreptons (money) of appropriate value in each of the remaining spaces. The Krepton sprite has different frames and an instance variable to determine how much each is worth. I want it to put specific amounts Kreptons in the space depending on how many bombs are around it (9 x 9 or 192px x 192px area with the current space in the center).

I'm afraid that if I put in another loop inside the second For Each loop, that it would make the game slow. Is there a way to count them without a loop? I tried this, but bcount is always zero.
B
25
S
8
G
8
Posts: 71
Reputation: 5,307

### » Fri Jun 29, 2012 6:15 am

I think you need a second loop to do this. You shouldn't have to worry about performance, since you only need to run these loops once when the layout starts.

The system command distance doesn't pick any instances, you cannot count the picked instances like you do above (correct me if I'm wrong).

I would do something like:

For each Rock
- Local number bcount = 0
- For each Bomb
- - If distance(Rock.X, Rock.Y, Bomb.X, Bomb.Y) <= 96 Add 1 to bcount
- Set Rock.bombcount to bcount.
B
20
S
5
G
4
Posts: 51
Reputation: 4,139

### » Fri Jun 29, 2012 8:41 am

Thank you. Problem solved.
B
25
S
8
G
8
Posts: 71
Reputation: 5,307

### » Mon Jul 02, 2012 3:01 pm

This counting objects technique is pretty awesome. It worked like a charm
B
89
S
32
G
23
Posts: 385
Reputation: 19,309