Detect a specific number of instances of same obj

Get help using Construct 2

Post » Wed Sep 25, 2013 3:08 pm

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.



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.

B
4
Posts: 13
Reputation: 251

Post » Wed Sep 25, 2013 3:19 pm

This isnt 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 Darklinki2013-09-25 15:20:59
B
15
S
6
G
6
Posts: 512
Reputation: 5,555

Post » Wed Sep 25, 2013 5:34 pm

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.
zatyka2013-09-25 18:32:49
B
64
S
19
G
8
Posts: 341
Reputation: 12,185

Post » Wed Sep 25, 2013 6:38 pm

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. Its saver to pick the nearest objects than the one in a given distance(distance could get changed)
B
15
S
6
G
6
Posts: 512
Reputation: 5,555

Post » Wed Sep 25, 2013 6:58 pm

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).zatyka2013-09-25 19:06:06
B
64
S
19
G
8
Posts: 341
Reputation: 12,185

Post » Wed Sep 25, 2013 7:56 pm

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.



But it is not working. Can you guys help some more.
B
4
Posts: 13
Reputation: 251

Post » Wed Sep 25, 2013 8:58 pm

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.zatyka2013-09-25 21:02:01
B
64
S
19
G
8
Posts: 341
Reputation: 12,185

Post » Wed Sep 25, 2013 9:05 pm

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 Darklinki2013-09-25 21:06:20
B
15
S
6
G
6
Posts: 512
Reputation: 5,555

Post » Wed Sep 25, 2013 9:10 pm

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.

B
4
Posts: 13
Reputation: 251

Post » Wed Sep 25, 2013 10:01 pm

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
B
64
S
19
G
8
Posts: 341
Reputation: 12,185

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests