Building the HealthBar Behaviors
As you've noticed, in both the Security Actor and in the Skeleton actor, we've been subtracting the value of the HealthBar property. Now we need to visually show the value of the health decreasing when the Health property changes.
Building a Custom Code Behavior for Updating the HealthBar's HealthValue
Click on the HealthBar actor, switch to its list of behaviors, and click the custom code event to add a new custom code behavior for the HealthBar actor. For the custom code event, select the Property Change event and set it so that whenever the Health property changes, the custom code event will fire (see Figure 2-65).

Figure 2-65. Setting the event to fire whenever the Health property changes
Next, we'll add the code in Example 2-6 for the custom code event. This code gets a reference to the top-level root of the Silverlight document, finds an XAML object named HealthValue, and then sets the Width of the HealthValue rectangle to the value of the game's Health property.
Example 2-6. Updating the width of the HealthValue Silverlight rectangle
this.GetVisualRoot().FindName("HealthValue").Width =
Game.GetValue("Health").toString();This is what happens in the game:
When the game starts, the Health property is set to 100 (see Figure 2-29).
When the Main actor collides with the Fire Bullet actor, we reduce the value of the Health property by 20.
Once this happens, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access