# Detect a specific number of instances of same obj

0 Favourites
• 13 posts
• I am really new to Construct 2 and so this might be a stupid question but here it is. If anyone can help, I'll be really grateful.

I have instances of same sprite in a hexagonal grid.(8 by 8) The instances have randomised animation frame on display (three frames - red, green and blue).

Now if six instances with the same color are positioned in a hexagon with one in the middle, they will get destroyed. But I can't figure out a way how to detect if that happens.

<img src="http://i42.tinypic.com/2igf63b.jpg" border="0" />

For example, in the image above, the six blue balls are positioned around another blue ball. Now I want to be able to destroy them automatically. So, how do I go about it. Any and all ideas are very welcome.

• This isn?t very hard but some code:

+on click(or however you pick the middle)

variable clickedX

variable clickedY

variable clickedFrame

• set clickedX = circle.X
• set clickedY = circle.Y
• set clickedFrame = circle.AnimationFrame
• for each order by distance(clickedX, clickedY, circle.X, circle.Y)

-- if circle.AnimationFrame != clickedFrame

--- stop loop

-- if loopindex("") = 6

--- again for each ordered by...

---- circle destroy

This works for sure but with some luck some one else got a better way

• You might run into trouble if you trigger the event by clicking on a ball object since that automatically picks the ball instance you clicked on, making it trickier to test the proximity of other instances. I would get around this by using a function. Something like:

Mouse: On Ball Clicked

Function: Call Function "Ball Clicked" (Ball.X, Ball.Y, Ball.AnimationFrame)

Function: On "Ball Clicked"

Set variable Range = Whatever the distances is between each ball

System: Pick Ball by evaluating (distance(function.Param(0),function.Param(1),Ball.X,Ball.Y) <= Range)

Ball: Ball frame = function.Param(2)

System: Ball.PickedCount = 7

Ball: Destroy Ball

If you have a hard time implementing this, please post your capx.

• zatyka the idea with the function is better than the mine with the instance variables but your action to pick the right objects is proud to failure. It�s saver to pick the nearest objects than the one in a given distance(distance could get changed)

• You are correct if the distance between instances will vary, but since he said the instances are in a grid, I'm assuming they are equidistant apart. If there are minor fluctuations in distance, he can always add a bit onto the range variable. Also, relying on the 6 nearest instance without checking their range isn't reliable (e.g. What if the player clicks on a ball along the edges... or if some of the balls are removed).

• Thank you so much for taking an interest in my problem and also for the quick replies.

Now in my concept the balls can be swapped. The player has to do the swapping and then create a situation like in the image I posted earlier. At that point the seven balls get destroyed. That's where I am stuck.

Using the idea from zatyka, I did something like the image below, calling the funtion "FindMatches" for each ball after each swap.

<img src="http://i39.tinypic.com/syru6d.jpg" border="0" />

But it is not working. Can you guys help some more.

• The "For Each" is problematic. It picks only one ball for each loop, so the PickedCount will never be greater than 1. I recommend removing it. I'm not saying that will fix everything, but it's definitely causing a problem.

If it still doesn't work, post your capx for assistance. It's hard to determine all the issues with only screenshots.

• 1. Remove the for each, without it will never hit 7 matches cause only 1 index is picked

2. should change to pick by distance() >= 107

107 = 75 the correct distance + the half distance, so you have some playroom

If this is not working check the parameter of the function and if this also not works, I never made it like this if you want you can try my version combined with his function

Edit: Damn someone was faster <img src="smileys/smiley7.gif" border="0" align="middle" />

• Is there any other way to call the Function for all the balls other than using the "For each"? May be use the Loop Function? Since the ball would have to be destroyed automatically each time the condition is fulfilled, all the balls have to be taken into account.

• The appropriate balls are taken into account. When an action is performed for an object, the action will run against all picked instances of that object. The conditions we use pick the 7 instances, and the destroy action is run against all of them. There is no need to use a loop to do that. It would be worthwhile to learn how the event system works to get a better understanding of why that happens:

How Events Work

• Thanks for the link. It's a great find for someone like me.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• This is driving me up the wall. Any and all help will be deeply appreciated.

This is what I have come up with.

• Never mind, guys. I think I solved it. Thank you all so much for your help and time. <img src="smileys/smiley4.gif" border="0" align="middle" />