health bar for each enemy

Get help using Construct 2

Post » Mon Feb 06, 2012 9:10 pm

@R0j0hound or @Ashley probably could give a better explaining.
As far as I can word it, I guess the pairing code Ashley included in C2 makes it so that instances of different objects get associated when created in the same event.
So when you filter Sprite instances (health <=0) and then run through all the instances resulting as picked (for each sprite) in the subevent picking the first instance (index 0) of another object type returns the associated instance with the current sprite instance.

That's surely not clear, I'm sorry I don't know how to word it better for now.

As rojo said, it has to do with how events work and are processed.
One condition after the other from top to bottom and subevents pick up with the instances already filtered from the top level event.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

Post » Mon Feb 06, 2012 9:21 pm

The objects are not exactly paired to each other, it is enough to just delete one of the instances.

To help illustrate what is happening you could replace "Every tick" with the more verbose version here:

System | For "" from 0 to Sprite.Count-1
System | Pick Sprite instance loopindex
System | Pick shadow instance loopindex
System | Pick healthbar instance loopindex
B
79
S
24
G
54
Posts: 4,749
Reputation: 40,761

Post » Mon Feb 06, 2012 10:59 pm

Does C2 maintain one SOL per object type?

Line3 sets the Sprite SOL to the one we clicked on. So that array will usually only contain one Sprite.
Line4 checks that picked Sprite (array) against Health<=0. So far so good. Then we have the ForEach condition which according to the manual does not reset the SOL, thus by doing ForEach Sprite we would only iterate over that one sprite coming from line 3.
Then we get to line 5 which is System.Pick Healthbar instance 0. So far we have not filtered any healthbars so I would expect System.Pick to operate on ALL healthbars in the layout, and instance 0 of all healthbars could be any which one not necessarily the instance we are looking for
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Post » Mon Feb 06, 2012 11:34 pm

Yep, that is all correct. The purpose of the "for each" is for the situation multiple objects were clicked on at the same time.

It does not matter if the shadow of another sprite is destroyed. The shadow is paired in the "every tick" event.

For example if you have four sprite instances:
Sprite1, Sprite2, Sprite3, Sprite4

And four shadow instances:
shadow1, shadow2, shadow3, shadow4

Then they will be paired Sprite1:shadow1, Sprite2:shadow2, ... etc.

If Sprite3 is clicked on and destroyed and the first shadow instance is destroyed.
Then the instances will look like this:
Sprite1, Sprite2, Sprite4
shadow2, shadow3, shadow4

Which will still work fine even though different instances will be paired together. The effect is purely for a visual effect so it does not matter if the the same instances are paired together.

If you want the same instances to be paired together then you would need to use the UID method.
B
79
S
24
G
54
Posts: 4,749
Reputation: 40,761

Post » Tue Feb 07, 2012 12:10 am

Thank you R0J0hound for taking the time to explain this! Now it makes perfect sense.

I thought there was some special trick going on in the destruction part, but we just destroy arbitrary healthbars/shadows and then grab whichever one is available for display purposes during the next tick.
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Post » Tue Feb 07, 2012 12:13 am

[QUOTE=R0J0hound]It does not matter if the shadow of another sprite is destroyed. The shadow is paired in the "every tick" event.[/QUOTE]

Gaaah, that's what I was overseeing.
Thanks for the clarification @R0J0hound, always much appreciated.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

Post » Tue May 01, 2012 6:16 pm

Hello, mine is not working very well. THe HP Bars update wrong, somethings they are right other times not so much.

It destroys the correct Frame, HEalth, and enemy. However sometimes it works correctly (2 enemies max) but when there are 3+ enemies you need to hit them all atleast 1 to update correctly, otherwise it updates the wrong health bar.

Does this look right?

(Creation)

System Every 2.5 Seconds
-Create FrameEnemy on layer Main 0,0
-Create Dummy on layer Main Random(800,600)
-Create BarEnemy on layer Main 0,0
-Dummy set currentHealth to 5
-Dummy set maxHealth to 5

System Every Tick
-FrameEnemy Set Position to (Dummy.X - 64, Dummy.Y - 48)
-FrameEnemy Move to bottom of layer
-BarEnemy Set Position to FrameEnemy (image point 0) <--- This was the problem. Should have been also Dummy.X - 64, Dummy.Y - 48 and not image point 0.
-BarEnemy Set width to (Dummy.currentHealth * 125 (Width of image) / Dummy.maxHealth)
-BarEnemy Move to Top of Layer

Dummy CurrentHealth <
System For Each Dummy
-Dummy Destroy

->System Pick BarEnemy Instance 0
-BarEnemy Destroy

EDIT: SOLVED

->System Pick FrameEnemy Instance 0
-FrameEnemy Destroy

Edit: For example look below:

http://dl.dropbox.com/u/73299017/pixRPG/index.html

EDIT: SOLVEDEquinox802012-05-02 00:17:53
B
26
S
5
G
5
Posts: 98
Reputation: 4,734

Post » Wed May 02, 2012 12:04 am

It seems to work when I remove FrameEnemy (the neat border around the health bar)

If I get this working with the border, I will let you know.

Edit: Okay I found out that it was because it was setting BarEnemy to imagepoint 0 of FrameEnemy (GUess it didn't know which Frame! Lol!) So I fixed it by setting both to go to Dummy.X - 64, Dummy.Y - 48

Which btw is the height and width of the enemy sprite, which aligns the healthbar perfectly for me. (The imagepoint being not the center, but the most-left of the BarEnemy/FrameEnemy) (following the "Make a Fancy Healthar" tutorial, converting it for NPCs as well).

Next step... to learn stability! Lol game starting to get a bit laggy now :/ Must need to do some organizing considering it is not my Laptop.

ASUS G74-SX A-1Equinox802012-05-02 00:15:08
B
26
S
5
G
5
Posts: 98
Reputation: 4,734

Previous

Return to How do I....?

Who is online

Users browsing this forum: h1k3, LeFuji and 17 guests