"Pick another instance" or something like that

Get help using Construct 2

Post » Wed Dec 11, 2013 9:45 am

Hi. I'm at a loss with this.

Suppose I want the player to click a Sprite1. When they do, a line is drawn from this clicked Spirte1 to another instance of Sprite1 (provided there is one), and if there are any more instances then the line is further drawn to them.

The drawing of the line itself is not the problem, the problem is determining where to draw it.

What i need is something like "pick another instance" or "pick random instance excluding the currently picked one", so the logic would go like this:

Sprite1 clicked --> save coordinates of Sprite1 into a variable --> pick another instance of Sprite1 --> draw a line between the variable coordinates and the Sprite1 --> repeat for as long as tere any more instances.

The order doesn't matter, as long as the first instance is the clicked one.
So I would like to end up with something like in the picture:




Any ideas appreciated.
B
16
S
7
G
1
Posts: 167
Reputation: 3,143

Post » Wed Dec 11, 2013 12:38 pm

instance variable drawn=0

on click: save x and y in 2 variables, set drawn=1, call function "draw"

function "draw":
pick object drawn=0; pick random instance: draw line from saved x and y to sprite.x and y; save x and y coordinates in the variables again, call function "draw"

you can also do that in a while loop ofc, stop condition could be related to sprite.pickedcount = 0 after picking drawn=0 or you pick drawn=0 and repeat it pickedcount times.mindfaQ2013-12-11 12:39:48
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Wed Dec 11, 2013 2:56 pm

ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Wed Dec 11, 2013 8:40 pm

Great! Both answers work like a charm. I wasn't aware you could combine different instance picking options. Also, I didn't think to use recursion.

Thanks a lot!
B
16
S
7
G
1
Posts: 167
Reputation: 3,143

Post » Fri Dec 13, 2013 8:54 pm

I don't really know WHAT recursion IS. LOL I'm about to find out... *CLICK*
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 793
Reputation: 15,157

Post » Fri Dec 13, 2013 10:39 pm

@blackhornet - I think I'm following the logic in your capx...
1. Bee object instance is clicked
2. For EACH Bee in the Family, set the NextUID family instance variable to -1
3. Call the Connect function with a parameter that plugs in the UID of the Bee that was clicked.

4. On call of the Connect function...
5. Pick the object that has a UID that equals 0 and set the NextUID value to UID of the object instance that was picked.
6. Pick all instances of the Family whose NextUID value is equal to -1.
7. From those selections, pick a random instance of the Family and set the UID of the Family to the Next instance variable of the Bee that was picked.
8. Draw the line between the two objects.
9. Call on the Function again and run it for all other instances of the Family whose UIDs are 0.

How'd I do? I'm still not sure I fully understand the whole thing.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
142
S
24
G
8
Posts: 793
Reputation: 15,157

Post » Fri Dec 13, 2013 11:15 pm

Almost:
5) Pick the object that has a UID passed into the function at index 0 and set the NextUID value to UID of the object instance that was picked. (Consider this the draw-from object).
7) ... (Consider this the draw-to object).
9) Call on the Function again, passing in the draw-from UID, and run it for all other instances of the Family whose NextUIDs are -1 (not used yet).
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 31 guests