Find Nearest

Get help using Construct 2

Post » Sun Feb 26, 2017 2:31 am

I've seen this pop up quite a few times and read up on quite a few examples.

Image

The problem is I can't see what I'm doing wrong with this. The Player sprite is part of the Players family... but it's not working the way I imagine. I'm only using 1 sprite for all the players.

Instead of setting the "ClosestEnemy" variable to the number assigned to the closest enemy, it appears to be setting it to the enemy with the lowest number.

I've seen people do similar with more events, and I myself have done so, but I'd rather not go through the hassle of complicated code when I know it can be done with only a few lines.

I imagine I'm close, but I can't quite get it right.

Can anyone help?
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
80
S
12
G
7
Posts: 965
Reputation: 10,750

Post » Sun Feb 26, 2017 3:07 am

It's kind of hard to tell what you want to do.
Family pick closest to player.x, player.y perhaps?
I'd get rid of the inverted, and for each.

Or maybe just player pick closest to family.x, family.y
Image ImageImage
B
169
S
50
G
173
Posts: 8,316
Reputation: 110,276

Post » Sun Feb 26, 2017 3:18 am

I wouldn't say it was close, I'd say it was correct. Not sure why it's not working, pick nearest but not self? Works fine for me.
B
46
S
16
G
77
Posts: 2,154
Reputation: 45,821

Post » Sun Feb 26, 2017 8:39 pm

@newt Yeah, that's pretty much what I'm trying to do. I have 4 of the same objects, all a part of the same family. I want each one to find the nearest other version and assign it as a target.

@plinkie As I said, I've done it a few times in the past, but they were somewhat convoluted. I saw this example on this forum, and thought I'd give it a go, but can't see what I'm missing.

I can post the capx I'm working on, but one needs the Chipmunk behaviour to get it to work.

https://dl.dropboxusercontent.com/u/542%20...%20nk2.3a.zip Will post the capx shortly.

Nevermind, solved it. I had a line above that which was only looking for a certain variable, and it was throwing everything off.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
80
S
12
G
7
Posts: 965
Reputation: 10,750

Post » Sun Feb 26, 2017 10:31 pm

Speaking of lines...
Was just about to suggest the line of sight behavior.
Image ImageImage
B
169
S
50
G
173
Posts: 8,316
Reputation: 110,276

Post » Sun Feb 26, 2017 10:54 pm

Try this:

var tempUID = sprite.UID
var tempX = sprite.X
var tempY = sprite.Y

C:Pick by comparison: Object: sprite, Expression: sprite.UID, Comparison: ≠, value: tempUID
C: Pick nearest sprite to tempX, tempY

It creates a SOL of all sprite objects that do not share the UID you wish to avoid picking, then it picks the closest one of those to the player sprite's position.
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888


Return to How do I....?

Who is online

Users browsing this forum: mcscooter and 22 guests