Request: When sprite is x distance from another

Discussion and feedback on Construct 2

Post » Sun Oct 09, 2011 6:43 am

I love Tower Defense games. They are what keeps me sane.

What I would really love is if the lads would code some sort of function that is essentially:

When Sprite1 is <= x distance from Sprite2 ... fire event

That way when coding a tower defense you could easily have a tower fire at the enemy mob.

My aim is to say something like: When an instance of the players units are within an instance of the enemy's units - fire a bullet.

I have read the forums and I'm aware of the distance() function, but it doesn't really cope when there are multiple instances of both the player and the enemy's units.

If this could be coded into the engine, I would buy the boy's a beer or two. If I have missed something fundamental in coding events please learn me good.

The beer is still offered*.

* You will need to come to Adelaide to claim said beer.

Cheers guys!


B
21
S
5
G
2
Posts: 26
Reputation: 2,512

Post » Sun Oct 09, 2011 8:19 am

There is actually a distance expression: Distance(x1,y1,x2,y2)

I made an example: http://dl.dropbox.com/u/6660860/distance.capx

Have fun!

Wait doh, i didn't realize you already mentioned the Distance() expression in your post! My bad....

Edit again, maybe this will help? http://www.scirra.com/forum/nearest-instance_topic44692.htmlCitnarf2011-10-09 08:28:12
B
7
S
2
G
4
Posts: 164
Reputation: 2,418

Post » Sun Oct 09, 2011 8:33 am

Hello!

I think that you are talking about Construct 2 but do you really mean C2 or probably CC?
B
17
S
4
G
4
Posts: 94
Reputation: 3,058

Post » Sun Oct 09, 2011 9:05 am

@spades I am talking about Construct 2 - the HTML most recent version that exports to HTML5.

@citnarf I read that forum post but the trick is having any instance of sprite 1 within any instance of sprite 2. I think that is the magic right there.

(I meessed around with that today but couldn't get it to work with any instance of 1 vs any instance f 2)



zurtri2011-10-09 09:06:29
B
21
S
5
G
2
Posts: 26
Reputation: 2,512

Post » Sun Oct 09, 2011 1:25 pm

By default the compare values condition just picks the first instance of an object, so it doesn't work with multiple instances. That's why distance() doesn't appear to work with multiple instances.

However, you can fix it with for-each:

+ For each A
+ For each B
+ distance(A.x, A.y, B.x, B.y) < 100
-> A: look at B

This repeats the distance() comparison for every combination of A and B, so every A looks at a B inside its range. (Not the nearest one - that's a fair bit more complicated - but it works OK this way.)Ashley2011-10-09 13:26:21
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sun Oct 09, 2011 10:20 pm

Ahh thanks mate. I'll give it a go and let you know how I go.

Kudos.
B
21
S
5
G
2
Posts: 26
Reputation: 2,512

Post » Sun Oct 09, 2011 11:03 pm

It worked perfectly!

Here is the final code.



Obviously I wouldn't want too many of these running for performance reasons but it does solve the problem of having things like Tureets autofire at mobs.

thanks Ashley!
B
21
S
5
G
2
Posts: 26
Reputation: 2,512

Post » Sun Oct 09, 2011 11:09 pm

You should be able to have all four conditions in one event and have it still work the same! No need for three subevents.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Mon Oct 10, 2011 4:17 am

Can I just confirm the difference from when I indent the events?

Also, if all these events are at the same 'level' of indentation is that correct?

Is there a performance difference?

B
21
S
5
G
2
Posts: 26
Reputation: 2,512

Post » Mon Oct 10, 2011 4:59 am

i think ash is telling you that you can create 'one' event that includes all the conditions you have listed, because they all create one desired effect.
B
52
S
17
G
15
Posts: 1,029
Reputation: 26,339

Next

Return to Construct 2 General

Who is online

Users browsing this forum: doomredmage and 16 guests