[SOLVED]Spawn instances of spr from same spr

0 favourites
  • 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 appreciated

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

    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.

  • 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? <img src="smileys/smiley1.gif" border="0" align="middle" />

  • 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.

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

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

    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.

  • 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.

    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.

  • > 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.

    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.

    When you create a new object it is automatically picked, so you can refer to it straight away in the loop.

  • When you create a new object it is automatically picked, so you can refer to it straight away in the loop.

    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).

  • 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 wretchedshark

  • 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).

  • Alcemon - i think i get it now, you want a sprite to "clone" itself and be the one spawning the other sprites instead of a system event?

    the difference i'm seeing here is, correct me if i misunderstood is, the system - create object > correctly picks and sets the instance variable

    whereas - sprite - spawn another object - sets the instance variable to 0?

    it also seems to multiply the pieces variable - which is really odd - making the spawn loop function incorrectly.

    i have made a new example on how to fix this, add an "on created" condition and move the set spriteID action to this condition, seems to work correctly.

    updated instance chain (r117)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • wretchedshark, thanks again for the reply. We are getting close to the issue :)

    Now, try to make each sprite to spawn exactly to the right of the previous sprite (spawning in the predefined image point 2).

    To top everything make sure to alternate the use of the animation "clone" in such way that:

    -the first spawned is using the Default animation

    -the second spawned is using the clone animation

    -the third spawned is again using the Default animation, and so on

    This should not be possible without setting up information of the sprites immediately after the spawn (which is apparently not possible in Construct2 if the spawning sprite is the same as the sprite being spawned).

  • This should not be possible without setting up information of the sprites immediately after the spawn (which is apparently not possible in Construct2 if the spawning sprite is the same as the sprite being spawned).

    It is possible, you could do it by varying this example a little bit:

    Snake demo

    Most of the example is not by me, only the few fixes. Check the final events for spawning and setting the sprite positions in events above. The point here is: divide your problem to few small, simple functions. :)

    Solving your problem here could be done by applying approach taken there.

  • vee41 Thanks for the tip. Basically this is using a global variables; (loopIndex and LastBodyUID) to help up picking and putting information into each of the instances.

    This also could work as an even better work-around to the one I linked but the truth is that it is still not possible to pick the created sprite when the spawning/creating command is done from another instance of the same sprite.

    Hmm...maybe both instances are picked (creator and created) and it's necessary to use 'Pick nth instance' before using it. I will try that right now and report the results.

    **UPDATE** Sorry, no luck, pick nth instance doesn't seem to help in this case.

  • When an object is created it becomes the only instance of that type that is picked. Once it is no longer picked it cannot be picked until the next top level event.

    Some more info:

    http://www.scirra.com/forum/picking-problem_topic41776_post257714.html#257714

    http://www.scirra.com/forum/r102-breaking-change-questions_topic56576_post352637.html#352637

    The way I'd do it is create the 5 sprites in one event then position them in the next.

    http://dl.dropbox.com/u/5426011/examples16/create5.capx

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)