Count Number of Objects Nearby

0 favourites
  • 4 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • 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.

    <img src="https://dl.dropbox.com/u/52788071/count_bombs.png" border="0" />

  • 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.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you. Problem solved.

  • This counting objects technique is pretty awesome. It worked like a charm

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)