Create Text Dynamically and set to follow sprite

Get help using Construct 2

Post » Tue Oct 09, 2012 6:20 am

I'd like to create multiple instances of a sprite, then attach a text object to each of them (representing the health) but each one will be unique. Each time you click one of the sprites, that specific objects health text value goes down by 1.

Is there a way to parent a text object to a sprite, and to be able to refer to the child object (text) as a variable?

eg: when i click the sprite, find the text field child and subtract 1 from its health variable and update the text?

thanks
B
50
S
21
G
8
Posts: 844
Reputation: 10,299

Post » Tue Oct 09, 2012 7:57 am

On the creation of each sprite.. in the same event, create the text object and also set this variable:
Sprite.TextID to Text.UID

and then to keep each text updated:

for each Sprite
pick Text by UID = Sprite.TextID

that'll single out the corresponding text object for the actions such as:
-> set text to Sprite.Health
-> set text position to sprite.x, sprite.y
etckeepee2012-10-09 07:59:56
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Wed Oct 10, 2012 5:55 am

I understand the last part, but im still not clear about the first step "Sprite.TextID to Text.UID"

Sprite.textID can't be a variable because it contains a . in it. And textID isnt a valid property of Sprite. So how to do set it?

Can you wip me up a quick screenshot of the setup? I'm sure its something tiny im missing.

The sprites are already on the layout,
Here's what sorta works already.
The it checks each of the sprites in existance, creates a text field for each of them, aligns them to the center, then changes their text to the area (width*height) and divides by 100 to make the numbers smaller, then updates the text.

How i want to subtract 10 lets say from each sprite/text combo each time a sprite is clicked. I tried making it a whole other event (system/on clicked object subtract 10), but it kept subtracting that value from all of the boxes, not just the one i clicked.

see pic below

http://imgur.com/wtdFH

thx for your continued help.
B
50
S
21
G
8
Posts: 844
Reputation: 10,299

Post » Wed Oct 10, 2012 7:31 am

Store the textfields UID in an instance variable of the sprite, so you have it handy when needed.

Also use the "PIN" behaviour to pin the textfield to the sprite, so it follows the sprite.
B
42
S
19
G
12
Posts: 723
Reputation: 13,911


Return to How do I....?

Who is online

Users browsing this forum: alextro, MarkStockley and 0 guests