How to get the distance between two sprites?

Get help using Construct 2

Post » Tue Oct 04, 2011 9:31 am

How to get the distance between two sprites and then compare?
B
14
S
5
G
6
Posts: 80
Reputation: 4,149

Post » Tue Oct 04, 2011 12:48 pm

Let's say you're trying to find the distance between Player and Object, you could create a variable called Distance.

Distance = int(abs(Player.X-Object.X)+abs(Player.Y-Object.Y))

Then you'd create an event: "System" -> "Compare 2 Values" and then create your comparison however you want (ie. Distance < 5)ScottP2011-10-04 12:50:06
B
20
S
9
G
2
Posts: 15
Reputation: 2,599

Post » Tue Oct 04, 2011 1:06 pm

thanks man. I will try it.
B
14
S
5
G
6
Posts: 80
Reputation: 4,149

Post » Tue Oct 04, 2011 1:18 pm

Construct 2 has an expression for this:

distance(x1, y1, x2, y2)

It's the same as it was in Classic btw.
B
27
S
6
G
11
Posts: 1,051
Reputation: 8,173

Post » Tue Oct 04, 2011 1:31 pm

Yeah, you COULD use the distance expression, but where's the fun in that? Don't you know that the more nested parentheses you have, the more impressive your code looks? :p

BTW: I just replaced the parenthetical mess in my project with the distance expression FTW! Thanks!ScottP2011-10-04 13:32:38
B
20
S
9
G
2
Posts: 15
Reputation: 2,599

Post » Thu Oct 06, 2011 9:14 am

Ah, very useful, thanks for that one guys! Can't believe I didn't even think of that or notice the distance() function. I was wondering how to get an object nearest another object and this seems to be the way. :)
B
126
S
7
G
4
Posts: 185
Reputation: 7,233

Post » Sat Mar 03, 2012 12:05 pm

Guys, i could only find this Distance() function here, on forums. I used it yesterday when i needed. My question is: where the find built-in [email protected] functions/methods?
B
18
S
7
G
4
Posts: 154
Reputation: 4,113

Post » Sat Mar 03, 2012 6:36 pm

B
100
S
38
G
134
Posts: 5,556
Reputation: 85,325

Post » Sat Mar 03, 2012 6:38 pm

w00t ninja'd
@Cassianno
They are all listed in construct when you have to type an expression you should have a window (F4 if it's hidden) with all your layout object, and one of them is "system", if you double click on it you'll see all the system expression available.
Else, you can also read this page of the fabulouse manual
http://www.scirra.com/manual/126/system-expressionsYann2012-03-03 18:38:55
B
71
S
22
G
14
Posts: 1,494
Reputation: 16,660

Post » Sat Mar 03, 2012 7:06 pm

Thanks R0J0hound and Yann!
This morning i've found the page you guys said, forgot to mention here...

Althought, didn't knew the shortcut while on a expression field and objects window (system object)!Cassianno2012-03-03 19:07:39
B
18
S
7
G
4
Posts: 154
Reputation: 4,113

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests