Check for nearby instances?

Get help using Construct 2

Post » Tue Apr 03, 2012 9:05 pm

Im creating a certain number of objects in randomized locations now I want to be able to loop thru all those instances and check for nearby instances in specific locations (above, below, upper left, etc) to perform specific actions

How exactly would I go about checking in these directions around a given object (its picked via loopindex in a for each)?
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Tue Apr 03, 2012 9:29 pm

You want to check if each object has another one close by? I did this recently by adding each object to an array, like this:

Start of layout
+ For each object
-> Set value at int(object.x/32)*32, int(object.y/32)*32 to 1

Then you loop through each array element and check if the position above, below, etc equals 1, that means there's an object immediately near it

It was quite difficult to work with it, you will probably need to save the UID of the object to a similar array to use it later... unfortunately I don't have the project anymore so I'm not entirely sure how I did it7Soul2012-04-03 21:30:57
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134

Post » Wed Apr 04, 2012 4:25 am

I figured it out for my needs. What I did was make 1d array and when I created the blocks I put their x,y in as the value like Block.X&"|"&Block.Y then I For Each'd thru the blocks and checked if the array contained a value for the directions I needed to check. Works great
B
13
S
5
G
2
Posts: 306
Reputation: 3,262


Return to How do I....?

Who is online

Users browsing this forum: DemantDigitalArt, Lancifer and 27 guests