Create object at last instance in family?

0 favourites
  • 15 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hi guys, I'm actually struggling to figure out something I presume is simple.

    I have a family of objects, every x seconds I would like to spawn a object from the family at the last instances x.

    Imagine a endless train, I have the family with the train, and I need to spawn another cartage at the last carriages instance, I have a image point on each object in the family called "Back" so I tried simply Family.CreateObject, at image point "Back" But that did not work. I tried system.create object at Family.imagepointX("Back"). But no luck <img src="smileys/smiley6.gif" border="0" align="middle" />

    Anyone have a quick fix?


  • I'm going to presume that you have an initial object belonging to Family created already. (and that there is only one of them on the screen at the time)

    Create a Global var called:


    System On Start Layout

    --> Last_FamilyInstance_UID = Family.UID;

    Every X seconds:

    --> Family.Pick Last_FamilyInstance_UID

       --> Family.Spawn (Family, Family.ImagePointX("Back"))

       --> Last_FamilyInstance_UID = Family.UID // this will pick the one just spawned

    Note: I believe that calling Spawn on a Family will spawn a random sprite belonging to that family, though I'm not sure.

  • cacotigon, Thanks for the help, It's kinda working, but not spawning on the last instance to start with, Please take a look:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • EDIT: No problem, I'll upload a working sample capx, give me just a second.

  • Sorry to steal your thunder, cacotigon, but I couldn't resist. <img src="smileys/smiley17.gif" border="0" align="middle" /> I got it working for you smitchell:

    The easiest setup for this scenario, rather than keeping track of the last created instance, is just picking the one furthest from 0,0. In this case, since all the carriages are in a straight line, that'll always be the last one.

    I always changed the origins of each of the train cars since they weren't lining up properly. It was possible to make it work the way it was before, but this way is much simpler.

    Lemme know if you need me to explain anything else I did!

  • Heh, no problem Sully ;) I was going to make a family instance variable called NextSection_UID, such that the code would be more modular and you could have multiple trains going at once, but even in r79.2, I GOT A BIG FAT "No Instance value found for instance variable" check failure.

  • If smitchell wants to have more than one train at once, yeah, he'll need a setup more like that one... As for the check failure, can't help you there. <img src="smileys/smiley5.gif" border="0" align="middle" />

  • Thanks guys, SullyTheStrange; I understand what I was doing wrong, I do have one question concerning something a little different.

    The trains speed is kept within a variable, So over time this would be increased to add more difficulty. But how would you suppose I handle the spawning timer so it will still spawn correctly with the speed increased?

    Maybe for each increment in the speed variable, half that to get the spawning timer?

  • Hmm. Rather than use a timer, which could potentially mess up, you could check to see how close the last train car is to the edge of the screen. If it's less than 100 pixels, let's say, spawn the next car. I guess that depends on how fast the train is gonna be going. If it's going at lightning speed, you'd have to spawn the new car a lot earlier than that. <img src="smileys/smiley17.gif" border="0" align="middle" />

  • So something like:



         -> Create train.

    But I dont get what im comparing. <img src="smileys/smiley6.gif" border="0" align="middle" />

  • No worries, I got yer back. <img src="smileys/smiley1.gif" border="0" align="middle" />

    In the "Train - Pick furthest from (0,0)" event, I added, "Train - X is less than (scrollx + WindowWidth/2) + 100)". That intimidating expression, "scrollx + WindowWidth/2", just means the edge of the screen, since scrollx is the center and WindowWidth/2 is, well, half of the window's width!

    So if the last train is within 100 pixels of the edge of the screen (but still OFF screen), create the new train car. Seems to work right now, but like I said, it might not work once it gets faster and faster. If it doesn't, just change that "+100" to something bigger, like 500.

  • Thanks!!!!! I had a play with changing the speed, add a event to add 1 to speed, like on click or something, It holds up pretty well <img src="smileys/smiley4.gif" border="0" align="middle" /> hurts your eyes though.

    But thanks!

  • Haha no problem man. <img src="smileys/smiley36.gif" border="0" align="middle" />

  • just keep a gloabal variable called 'lastCar' and everytime you spawn a new 'train' set this variable to train.UID


    +train.UID = lastCar

    +train.X+train.Width/2 < windowidth+50

       -> spawn a new train

       -> lastCar = train.UID

  • Yann thanks for this, but it really confused me. Can I see it in a example please? Thanks.

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