Create object at last instance in family?

Get help using Construct 2

Post » Tue Feb 14, 2012 4:59 pm

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

Anyone have a quick fix?

Thanks.
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Tue Feb 14, 2012 5:36 pm

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:
Last_FamilyInstance_UID

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.
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Tue Feb 14, 2012 5:47 pm

@cacotigon, Thanks for the help, It's kinda working, but not spawning on the last instance to start with, Please take a look: http://dl.dropbox.com/u/51270524/Train.capx
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Tue Feb 14, 2012 6:04 pm

EDIT: No problem, I'll upload a working sample capx, give me just a second.cacotigon2012-02-14 18:05:59
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Tue Feb 14, 2012 6:14 pm

Sorry to steal your thunder, cacotigon, but I couldn't resist. I got it working for you smitchell:

http://db.tt/Sz5JgAG3

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!SullyTheStrange2012-02-14 18:14:45
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 6:17 pm

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.
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Tue Feb 14, 2012 6:20 pm

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.
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 6:24 pm

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?
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Tue Feb 14, 2012 6:26 pm

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.
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 6:29 pm

So something like:

System.EveryTick
System.Compare2Values(Distance(?,?)>?)
     -> Create train.

But I dont get what im comparing.
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Next

Return to How do I....?

Who is online

Users browsing this forum: Callan S, Yahoo [Bot] and 13 guests