How do I Get the closest instance?

Just started using Construct 2? Post your questions here

Post » Tue Aug 18, 2015 5:43 am

how would I get the closest instance of a specified object

in gamemaker there is a function called instance_nearest that returns the closest instance of the object you specify
B
13
S
6
G
1
Posts: 149
Reputation: 1,960

Post » Tue Aug 18, 2015 6:35 am

Image Image
B
22
S
13
G
130
Posts: 869
Reputation: 67,816

Post » Tue Aug 18, 2015 7:02 pm

yes but it doesn't take the object to look for
and it checks the object using the code too

I would to pick nearest(target.x target.y)
but that would only check nearest object to the target and thats not what i want
B
13
S
6
G
1
Posts: 149
Reputation: 1,960

Post » Tue Aug 18, 2015 7:48 pm

You can put all the objects you want to test in a family
then
pick family the nearst (target.x,target.y)
Image Image
B
22
S
13
G
130
Posts: 869
Reputation: 67,816

Post » Tue Aug 18, 2015 8:51 pm

I have the free version right now
but I think i might of figured it out I used for each
B
13
S
6
G
1
Posts: 149
Reputation: 1,960

Post » Tue Aug 18, 2015 9:59 pm

ok so I am getting a problem
the p1 missile doesn't seem to work now but the p2 missile seems to work fine
Code: Select all
http://imgur.com/JL9qaof

is there something a did wrong?
B
13
S
6
G
1
Posts: 149
Reputation: 1,960

Post » Tue Aug 18, 2015 10:19 pm

event 9: "for each" is already every tick
event 10: missile pick the nearest self.x,self.y self=missile it would be more logical to use "... nearest player.x, player.y"
no interest in putting the event 11in sub event 9

Edit: in the event 9 , you take for each "player" the nearest missile but you do on each tick .
You should add a status (eg boolean instance variable "AsTarget" and TargetIID) to your missile once it has a target,then he follows it and is no longer in your loop
you could try: [...] missile when AsTarget is false,pick missile nearest (player.x,player.y)--> missile set AsTarget true / missile set targetIID to player.iid

Missile AsTarget is true -->> missile rotate 4° toward player(self.targetIID).x,player'self.targetIID).y
Image Image
B
22
S
13
G
130
Posts: 869
Reputation: 67,816

Post » Tue Aug 18, 2015 10:31 pm

hmm ok thanks
i removed the every tick from event 9
i changed event 10 to say pick nearest player.x player.y
I kept the event 11 in event 9 as I need it to not collide with the player on startup
however is still doesn't work
it seems now only some of the missiles home in on the player
B
13
S
6
G
1
Posts: 149
Reputation: 1,960

Post » Tue Aug 18, 2015 10:35 pm

can you share tour capx?
Image Image
B
22
S
13
G
130
Posts: 869
Reputation: 67,816

Post » Tue Aug 18, 2015 11:01 pm

Message: Shadowblitz16 can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
ya https://www.dropbox.com/s/bij2myaybb1rc ... .capx?dl=0

MadSpy you there
Last edited by Shadowblitz16 on Wed Aug 19, 2015 7:37 pm, edited 1 time in total.
B
13
S
6
G
1
Posts: 149
Reputation: 1,960

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest