[SOLVED]Spawn instances of spr from same spr

Get help using Construct 2

Post » Wed Jan 30, 2013 10:11 pm

Hello all,

As the title says I want to spawn several instances of the same sprite (say Sprite1 from the example projects).

While just spawning instances of the same sprite in succession has been covered in another post in the forums, I am interested in spawning a chain of exactly 5 sprite instances.
To do this we need a an instance variable ("piece" in the example projects) that gets incremented in each spawn.

The problem is, I cannot pick the newly spawned instance! When I try to increment the "piece" instance variable of the spawned instance, I actually increment it on the spawner.

I have even tried to create a sprite family with only Sprite1 as a member (Family1 in the example 2) to use as an alias but this did not work.

Example projects
Instance chain
Family instance chain

Any help would be appreciatedAlcemon2013-02-06 08:05:59
B
19
S
4
G
4
Posts: 70
Reputation: 3,896

Post » Fri Feb 01, 2013 2:04 pm

Here is a link to a related topic but it's not quite the same.

http://www.scirra.com/forum/instance-spawn-another-instance-of-itself_topic48162.html

The problem I am having is related to picking the created instance when spawning from that same sprite.
B
19
S
4
G
4
Posts: 70
Reputation: 3,896

Post » Fri Feb 01, 2013 3:57 pm

@Alcemon - have you tried using UID's and a variable somehow?

so something like:
- spawn sprite1
- set spriteUID to sprite1.UID
- manipulate that sprite1 via sprite1(spriteUID).X (for example)
- repeat the cycle

is that what you mean or did i misinterpret?
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Fri Feb 01, 2013 5:27 pm

Unless I misunderstood the question you just need to run a loop.

+For "" from 1 to 5
-Create object at <x> + loopindex("") * <distance apart>

then maybe set IDs to loop index and go from there.Tokinsom2013-02-01 17:33:01
Image
B
225
S
27
G
13
Posts: 1,774
Reputation: 18,024

Post » Mon Feb 04, 2013 6:55 pm

Thanks for the replies guys, unfortunately is not as simple.

[QUOTE=wretchedshark] @Alcemon - have you tried using UID's and a variable somehow?[/QUOTE]

This solution has two problems.
1) I cannot pick the created instance (this is the main problem), so if try to get the UID by doing something like

temp_var = Sprite1.UID

I would end up storing the parent UID not the child

2) The second problem is that last time I tried something similar I realized that the UID is not set until the cycle after something is created. So trying to store the UID right after something is spawned/created would not give you the correct value.
B
19
S
4
G
4
Posts: 70
Reputation: 3,896

Post » Mon Feb 04, 2013 6:57 pm

[QUOTE=Tokinsom] Unless I misunderstood the question you just need to run a loop.

+For "" from 1 to 5
-Create object at <x> + loopindex("") * <distance apart>

then maybe set IDs to loop index and go from there.[/QUOTE]

This would work handsomely (as per the link to the topic in my second post) if the X,Y of the instances was the only thing that I wanted to change (as position can be passed as a parameter to spawn and create).

But if you want to change any of instance variables or things like mirrored/flipped, once again you run into the problem of not being able to pick the newly created instance.
B
19
S
4
G
4
Posts: 70
Reputation: 3,896

Post » Mon Feb 04, 2013 7:02 pm

[QUOTE=Alcemon] [QUOTE=Tokinsom] Unless I misunderstood the question you just need to run a loop.

+For "" from 1 to 5
-Create object at <x> + loopindex("") * <distance apart>

then maybe set IDs to loop index and go from there.[/QUOTE]

This would work handsomely (as per the link to the topic in my second post) if the X,Y of the instances was the only thing that I wanted to change (as position can be passed as a parameter to spawn and create).

But if you want to change any of instance variables or things like mirrored/flipped, once again you run into the problem of not being able to pick the newly created instance.[/QUOTE]

When you create a new object it is automatically picked, so you can refer to it straight away in the loop.
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Mon Feb 04, 2013 7:23 pm

[QUOTE=vee41] When you create a new object it is automatically picked, so you can refer to it straight away in the loop.[/QUOTE]
This doesn't seems to be the case when the sprite creator is the same as the created sprite (check out the cap on the first post for an example).Alcemon2013-02-04 19:24:31
B
19
S
4
G
4
Posts: 70
Reputation: 3,896

Post » Mon Feb 04, 2013 7:38 pm

here is an example for you demonstrating chain spawning and UID/instance variable picking
the capx

you can manipulate them as you spawn them, i used something similar to make a spread shot example (though i didn't use a loop there)

edit: if you need more help or explanation just tag me with @wretchedsharkwretchedshark2013-02-04 19:39:30
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Mon Feb 04, 2013 9:48 pm

Thanks for the detailed reply Wretchedshark. As I told you in my first reply, this is exactly how it is solved in the forum post I linked (and how I actually do it in my project as a work-around).

The problem I am running against is basically related to picking and sprite spawn recursion (my deepest apologies as it seems that I am utterly failing at explaining myself).

Imagine that you have the following two lines running on a Sprite instance of "Sprite":
Sprite | Spawn Sprite on layer 0 (image point 0)
Sprite | Set spriteID to Sprite.UID

Instead of having spriteID correctly loaded with the created sprite's UID, you would get the creator's UID loaded into spriteID (as Construct2 confuses the parent and child instance because they are called the same).
B
19
S
4
G
4
Posts: 70
Reputation: 3,896

Next

Return to How do I....?

Who is online

Users browsing this forum: benajad, Braus, R0J0hound, shinkan and 24 guests