health bar as an instance variable

Get help using Construct 2

Post » Sat Feb 23, 2013 8:24 pm

When you shoot the enemy in the game I'm making, it's health bar goes down. But when another instance of that enemy is created, the health bar is the same. Any suggestions on how to change that?
B
6
S
2
G
3
Posts: 18
Reputation: 2,306

Post » Sat Feb 23, 2013 8:35 pm

you could make a event to restart de heatlh bar when another enemy appears
B
6
Posts: 25
Reputation: 539

Post » Sat Feb 23, 2013 10:19 pm

Your solution is in the title of this thread: use instance variables. When each instance spawns, it'll spawn using the default value of the instance variable, and those variables will be independent of those belonging to other instances.

If this isn't working properly for you, you might not be correctly picking the instances of the enemy that are meant to lose health: you might be affecting all enemies at the same time.
B
56
S
15
G
10
Posts: 910
Reputation: 11,942

Post » Fri Mar 01, 2013 10:21 pm

But how? How do I link a picture and an instance variable?
B
6
S
2
G
3
Posts: 18
Reputation: 2,306

Post » Fri Mar 01, 2013 10:35 pm

@Mage213456 - Are you assigning instance variables directly TO the object? I don't mean creating a variable globally in the event sheet, but creating it through the object. Select the sprite/object you want to have the health value instance variable for, then, on the far left of the editor, in the properties bar, look for the instance variable option...that should do the trick for ya.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
112
S
20
G
8
Posts: 741
Reputation: 13,636

Post » Fri Mar 01, 2013 10:55 pm

Assuming you are actually using an instance variable, you need to choose the proper object instance to interact with.
You can use something like the Pick Instance event, or if you use something like "Is Overlapping Object" or "On Collision with Object" then it will choose the proper instance automatically.
https://www.scirra.com/manual/71/instancesgrimwolf2013-03-01 22:57:59
B
6
S
1
Posts: 67
Reputation: 986

Post » Fri Mar 01, 2013 11:57 pm

Containers are your friends.

Put the health bar in a container with the enemy. When an enemy is spawned it will have it's own health bar spawned with it, and any time you pick that enemy it will automatically pick it's health bar.

Just do a for each and loop through your enemies. Each time it picks an enemy, it will also pick that enemy's health bar so that you're only updating just that one.

Here is a capx (R120.2) that demonstrates what I am talking about.
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Sun Mar 03, 2013 5:15 pm

Thanks. I'm still kind of confused on containers though.
B
6
S
2
G
3
Posts: 18
Reputation: 2,306

Post » Sun Mar 03, 2013 5:36 pm

I tried the container method, but the monster would come in with a health bar and when you shoot it, the health bar goes down which is good. When another instance of that same monster comes in and the first monster isn't destroyed, the second instance wouldn't have a heath bar untill the first one is destroyed. Also, when one instance of the monster is hit once or twice, the next monster will have that same amount of health as the first. Any suggestions on how to fix them?
B
6
S
2
G
3
Posts: 18
Reputation: 2,306


Return to How do I....?

Who is online

Users browsing this forum: R0J0hound, randomly and 25 guests