Attaching Objects to Other Objects

For questions about using Classic.

Post » Thu Sep 08, 2011 9:44 pm

This is a tricky problem I've been thinking about for a feature that I'm working on. Basically, I'd like to have one type of object following another type of object, so that it always stays at the same position as the object it's following.

What makes this more difficult is that I have more than one object of the same type. Let's say I have a type of objects called "Monster" and another type called "Health Bar". I want every monster to have a health bar, and it should be displayed above the monster's head at all times. But how can I make a unique health bar for each monster, and then make sure they're all at the right position?

Ideally, this should be done in an efficient manner so that the position of health bars will only be set when it must be set. I don't want a loop that runs every tick and sets the position of every health bar on the level. I would like to only move health bars for the monsters that have moved that tick; stationary monsters don't need to move their health bar.

Any help with this would be appreciated.Nillo2011-09-08 21:47:11
B
4
S
1
G
6
Posts: 28
Reputation: 2,056

Post » Thu Sep 08, 2011 9:56 pm

I always adde the parent unique.id to a variable in the child object then pick by comparing (child variable and parent unique id).

Urled2011-09-08 21:57:28
B
18
S
4
G
1
Posts: 413
Reputation: 2,512

Post » Thu Sep 08, 2011 10:12 pm

Put "Monster" and "Health Bar" in a container. Then the positioning can be done with one event:

+ System: Always (every tick)
-> healthBar: Set position to Monster.X, Monster.Top-16
-> healthBar: Set width to Monster ('health')
B
79
S
24
G
53
Posts: 4,728
Reputation: 40,219

Post » Thu Sep 08, 2011 10:12 pm

Construct has a hidden feature that automatically does a lot of this.

To use it:

Put everything that acquires a health bar into a family.

Set the healthbar to destroy at startup.

Then, at the start of layout, for each member of that family, create a health bar.

Then in another event, set the position of the health bar to family.x, family.top-howevermuch.

Construct will automatically place a healthbar at each family member. You can also use this technique to set the width of the healthbar to family variables, such as family('hp'), and construct will automatically know which instance to use.

This is actually better than the container method, as it is simpler to set up, all automatic and doesn't require a different health bar object for each sprite.Arima2011-09-08 22:14:06
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Thu Sep 08, 2011 10:25 pm

I believe I know what you are trying to do :]

http://www.yourfilelink.com/get.php?fid=711379

Edit: Sry Arima, were Irl buddies. ;]Nifl2011-09-08 22:41:02
B
6
S
1
G
2
Posts: 18
Reputation: 711

Post » Thu Sep 08, 2011 10:30 pm

Very simple method.Urled2011-09-08 22:38:57
B
18
S
4
G
1
Posts: 413
Reputation: 2,512

Post » Thu Sep 08, 2011 10:32 pm

Please stick to english on the forums, or at least provide a translation. Thanks!
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Fri Sep 09, 2011 5:02 am

Thanks for the help everyone! I'm going to try this soon and I'll let you know if I run into any more problems with it.
B
4
S
1
G
6
Posts: 28
Reputation: 2,056


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests