How do I stick sprites together like they're one object?

Get help using Construct 2

Post » Wed Jul 15, 2015 10:01 pm

I'm trying to randomly generate people that spawn every now and then and walk around. They're made of a torso which contains all the important instance variables and a bunch of other sprites hanging off (limbs, facial features) in their own family called 'BodyParts' or something.

The characters will be moving around, changing size (handled with a 'SizeScale' instance variable rather than the built-in 'Scale' parameter which wasn't really doing what I'd hoped), changing the z order and jumping between layers.

I think containers might be what I need, but I don't quite understand how they work so instead I've given everything an 'index' instance variable to show which body parts belong to which torso. I've been using this with the conditions 'if BodyParts.Index=Torso.Index and Torso.Index=BodyParts.Index', to make sure the right instances get picked. Then I'll move the sprites to where they need to be or whatever.

Anyway, it's not working. The way it assigns the index works fine, the z ordering fake-3d stuff works fine with just the torsos, but the other body parts are all over the place. Even when the conditions specify I want to pick the BodyParts with the same Index as a Torso and visa-versa, it just isn't happening. Some body parts behave and move with their parent torso, but most of them just float about ignoring the programming.

The BodyParts definitely have the same 'index' as their corresponding torso. So how do I get the conditions to pick all the right ones? Alternatively, is their some other method of getting different objects to move around like one object, including their Z order and everything?

Sorry if I'm not explaining this properly or if I'm missing something obvious.
Last edited by Sam Mortimer on Thu Jul 16, 2015 11:10 am, edited 1 time in total.
B
8
S
1
Posts: 54
Reputation: 824

Post » Wed Jul 15, 2015 10:34 pm

You should make more of an effort to understand containers. They'll save you a lot of headaches here. I'd suggest reading the manual entry for them.
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,106

Post » Thu Jul 16, 2015 12:49 am

Yes Containers in Construct is the thing you need.


Lets say you create 3 people. And every "person" have lets say. A head, body and legs. 3 things.
If you start making a sprite representing the head, and call that one "Person".
Then create another sprite representing a body, and call it "Body".
And after that you create yet another sprite representing legs, and call it "Legs"

If you go to, or select your Person-sprite ( the head ) and go to containers in the Properties ( to the left ) ... And click Add object. Just add your Body and Legs there.
Now the Head, body and Legs are in a container.

If you later need to select one Person. You use the "Pick by comparison" - event
There you choose the Person-sprite ( the Head ) - and after that you have an expression... Lets say ( Person.index = person01 ). This way Construct picks the Person.sprite that has person01 in it's instance variable: Person.index

Then what you later decide to do with that object applies to ALL the sprites in that container. The Head, Body and Legs. So if you want to destroy the "Person" - choosing Destroy Person also destroys the body and legs. It's the same if you want to create a Person. The body and Legs will also be created.
If you position the person though... the body and legs wont automatically also position them selves correctly. For that you have to preferably use the Pin to behaviour. If you have pinned the Body and Legs to the head and later position the head somewhere, the Body and Legs will follow. The same works if you rotate the head. I do not know about scaling. But at least if you choose the Person object in an event and in the actions you for example use Person - Set scale and below that Body - Set scale and then Legs - Set scale. ...The correct Head, body and legs will be scaled.

So think like this.
Have an unique identification for each people. Like Person01, Person02 ... and so on. In an instance variable. Do it when the sprite gets created.
Use one sprite that controls all the other body parts.. lets say the Head of the person. And put all the other body parts in the heads container.
When you pick the Head.sprite - ALL the other body sprites also gets picked.

Try experiment with this in a totally fresh project and I'm sure you will quickly learn how it works. A tip is to position the head and the body parts as you want them to fit together when you're in constructs layout. Because when you create a new object, construct will create the head, and then position the other objects in the container the way you have positioned them in the Constructs layout. If you directly after you created your Head ALSO pin all the body parts to it - then when you position your head all the body parts will also get positioned.

I hope you get it to work. :)
Soulmachine @Soundcloud
B
14
S
2
G
1
Posts: 63
Reputation: 928

Post » Thu Jul 16, 2015 11:40 am

Thanks for the detailed answer!
I experimented with containers, and they're not what I was expecting but they're really useful.
However I don't think I can use them because the way they work, from what I can tell, is that one of every object in the container is spawned at once, and each one is linked solely to the objects it spawned with. This means I can't have more than one of the same object, e.g. more than one eye, and still have them linked together in the event sheet.

I could bypass this problem if there were some way to spawn a container full of objects, then spawn another instance of one of the objects without anything else in the container getting duplicated too, then add this extra object into the existing container. But I have no clue how that would work.

Any ideas, anybody?
B
8
S
1
Posts: 54
Reputation: 824

Post » Thu Jul 16, 2015 11:53 am

Jesus christ, okay that was tricky but I finally found a solution. Gave up on those bullshit containers. I went back to my instance-variable-indexes and tried a bunch of different ways to pick the correct instances, and I GOT ONE THAT WORKS.

The important parent object with all the crucial instance variables and shit is called "Person", everything else is in a family called "BodyParts". Every time I want the BodyParts to do something in relation to their parent Person, I use the conditions:
- For each Person
- Pick BodyParts by comparison, BodyParts.index = Person.index

And it bloody works.
Thanks everyone.
B
8
S
1
Posts: 54
Reputation: 824


Return to How do I....?

Who is online

Users browsing this forum: Exabot [Bot] and 6 guests