# How to get the distance between two sprites?

Get help using Construct 2

### » Tue Oct 04, 2011 9:31 am

How to get the distance between two sprites and then compare?
B
12
S
5
G
5
Posts: 80
Reputation: 3,563

### » 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
8
G
2
Posts: 15
Reputation: 2,444

### » Tue Oct 04, 2011 1:06 pm

thanks man. I will try it.
B
12
S
5
G
5
Posts: 80
Reputation: 3,563

### » 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
21
S
6
G
10
Posts: 1,017
Reputation: 7,426

### » 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
8
G
2
Posts: 15
Reputation: 2,444

### » 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

### » 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 C@ functions/methods?
English is not my native language. Sorry for any mistakes and feel free to correct me if needed.
B
14
S
4
G
3
Posts: 98
Reputation: 2,951

### » Sat Mar 03, 2012 6:36 pm

B
75
S
21
G
48
Posts: 4,670
Reputation: 37,205

### » 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
60
S
22
G
14
Posts: 1,479
Reputation: 16,336

### » 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
English is not my native language. Sorry for any mistakes and feel free to correct me if needed.
B
14
S
4
G
3
Posts: 98
Reputation: 2,951

Next

Return to How do I....?

### Who is online

Users browsing this forum: Energiun, Mark2206, MrSpittle, Yahoo [Bot] and 26 guests