Dr. Dobb's Portal

Section 3: Silverlight Game

Table of Contents


3.1.4Simulating brick explosion

The simplest way to do brick explosion animation is to define a StoryBoard associated with each brick. Whenever an explosion is to take place we can start the animation through the following code:

	control.content.findName("brickAnimation").begin();
								

The obvious drawback of this approach is that the animation has to be re-implemented for each brick. Silverlight has the concept of resources, which defines elements that can be reused across the scene. Unfortunately in our case, this approach has a major limitation: a single animation cannot be associated with multiple objects at the same time, and therefore we cannot implement the scenario where two bricks explode at the same time. The approach we used in our game is to create a Storyboard at runtime and associate it with the brick and then begin the animation. The following pseudo code shows how we implement that:

	var explodeXaml = '<Storyboard> …';
	var storyBoard = control.content.createFromXaml(explodeXaml);
	storyBoard["Storyboard.TargetName"] = brick.Name;
	var root = control.content.findName("Root");
	root.Resources.Add(storyBoard);
	storyBoard.begin();
								




NAVIGATION

Flipbook Advertisement