<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SHAZAML! &#187; RemoveElementAction</title>
	<atom:link href="http://www.shazaml.com/archives/tag/removeelementaction/feed" rel="self" type="application/rss+xml" />
	<link>http://www.shazaml.com</link>
	<description>The Blog for Design &#38; Development Superheroes</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:02:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Video: Creating a Silverlight 3 Casual Game using Blend 3 and Triggers, Actions, and Behaviors</title>
		<link>http://www.shazaml.com/archives/video-silverlight-casual-game</link>
		<comments>http://www.shazaml.com/archives/video-silverlight-casual-game#comments</comments>
		<pubDate>Mon, 09 Nov 2009 15:19:11 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[ChangePropertyAction]]></category>
		<category><![CDATA[ContinuousPlayMediaBehavior]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[GlobalCounterMinReachedTrigger]]></category>
		<category><![CDATA[GoToStateAction]]></category>
		<category><![CDATA[IncrementGlobalCounterAction]]></category>
		<category><![CDATA[InvokeCommandAction]]></category>
		<category><![CDATA[MagnifierOverBehavior]]></category>
		<category><![CDATA[MouseCursorBehavior]]></category>
		<category><![CDATA[ParticlesBehavior]]></category>
		<category><![CDATA[PlaySoundAction]]></category>
		<category><![CDATA[RemoveElementAction]]></category>
		<category><![CDATA[SetGlobalCounterAction]]></category>
		<category><![CDATA[SetInteractionPropertyAction]]></category>
		<category><![CDATA[ShowGlobalCounterBehavior]]></category>
		<category><![CDATA[Trigger]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[Visual State Manager]]></category>
		<category><![CDATA[VSM]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=333</guid>
		<description><![CDATA[Video presentation from Desert Code Camp 2009 - Silverlight 3 Casual Game]]></description>
			<content:encoded><![CDATA[<p>On Saturday, November 7, 2009 at 9:00am I presented at the Desert Code Camp in Phoenix, AZ. The topic of my presentation was Silverlight casual game development. In less than an hour I demonstrated how to use triggers, actions, and behaviors in Blend 3 to create a hidden object game. Because all code was contained in the TABs (Triggers, Actions, and Behaviors) there was no code behind for the main UserControl.</p>
<p>Here is a link to the <a title="Video - Creating a Silverlight 3 Casual Game using Blend 3 and Triggers, Actions, and Behaviors" href="http://www.shazaml.com/downloads/DesertCodeCamp2009-Silverlight3CasualGame.wmv">presentation video</a> in WMV format.</p>
<p>If you like you can see <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">all the episodes</a> and follow along with the tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/video-silverlight-casual-game/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://www.shazaml.com/downloads/DesertCodeCamp2009-Silverlight3CasualGame.wmv" length="63294285" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>Hidden Object: Episode 4 – Adding Particles with a Behavior</title>
		<link>http://www.shazaml.com/archives/hidden-object-episode-4</link>
		<comments>http://www.shazaml.com/archives/hidden-object-episode-4#comments</comments>
		<pubDate>Fri, 18 Sep 2009 03:25:53 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[DebugEventTrigger]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[InvokeCommandAction]]></category>
		<category><![CDATA[ParticlesBehavior]]></category>
		<category><![CDATA[RemoveElementAction]]></category>
		<category><![CDATA[Trigger]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=80</guid>
		<description><![CDATA[Create ParticlesBehavior and make use of DebugEventTrigger, InvokeCommandAction, and RemoveElementAction]]></description>
			<content:encoded><![CDATA[<p>In the previous episodes of <a title="Creating a Hidden Object Game in Silverlight 3" href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">Creating a Hidden Object Game in Silverlight 3</a> we spent all of our time in Blend. Now we are going to put on our developer hat and create our own custom behavior that will shoot particles when an item is clicked. This will give a nice visual effect that compliments marking the item off the list and the sound effect.</p>
<p>Click <a href="http://www.shazaml.com/hidden-object-episode-4-demo">here</a> to see a demo of what the game will look like after we finish this episode.</p>
<p>I want to make sure that I give tons of credit to Robby Ingebretsen from <a href="http://blog.nerdplusart.com/">nerdplusart</a> for his <a href="http://blog.nerdplusart.com/archives/silverlight-particle-generator">Silverlight Particle Generator</a>. If you haven&#8217;t had a chance to play with it before (and I do mean play) then take a few minutes to do so.</p>
<p><a href="http://labs.nerdplusart.com/particles/fullscreen.php"><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec1.png" border="0" alt="" /></a></p>
<p>The plan is to take the ParticleControl from that solution and wrap it in a ParticlesBehavior that will be triggered when the item&#8217;s path (ex: staplerPath) is clicked.</p>
<p>First we need to take a side-step into the differences between an action and a behavior and their relationship to triggers. We will start to see some patterns emerge. In the previous episodes we looked at two actions (ChangeProperyAction &amp; PlaySoundAction) and noticed that an action specifies what will happen and to which object. When the action will occur is specified by a trigger. There are two main types of actions (TriggerAction &amp; TargetedTriggerAction). The first allows us to attach to an object and invoke some code when a trigger is fired whereas the second does that <em>and</em> defines a target for the action that is different from the object that the action is associated with. Both of these actions can only be called by a single trigger. A behavior in some ways appears simpler than an action because there is no direct connection to a trigger and therefore no <em>Invoke</em> method to be called. All we get are overrides for OnAttached and OnDetaching. But behaviors are powerful, can hold state, and can do various things based on <em>many</em> triggers.</p>
<p>For a class diagram of the Action and Behavior base classes as well as some great sample behaviors, check out the <a href="http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx">Behaviors and Triggers in Silverlight 3</a> article on SilverlightShow.</p>
<p>Hint: We will use the MagnifierOverBehavior in the next episode.</p>
<p>Enough talking lets open Blend and get coding. In the Projects panel, create a folder called Interactivity and under it a folder called ParticlesBehavior. Interactivity seems to be the term Microsoft uses to talk about both actions and behaviors and by the time we are done with this tutorial we will have created both types. I am doing this in Blend, because it has a template to create a basic behavior.</p>
<p>Right-click the ParticlesBehavior folder, pick Add New Item, select Behavior, and enter the name: ParticlesBehavior.</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec2.png" alt="" /></p>
<p>Click OK to create the behavior.</p>
<p>After saving all the files, open the solution concurrently in Visual Studio 2008 so that you can switch back and forth between Visual Studio and Blend as needed.</p>
<p>Go to the <a href="http://blog.nerdplusart.com/archives/silverlight-particle-generator">Silverlight Particle Generator</a> page and download the source code. Copy the following files into the Interactivity\ParticlesBehavior folder and include them in the project:</p>
<ul style="margin-left: 38pt">
<li>ParticleControl.xaml</li>
<li>ParticleControl.xaml.cs</li>
<li>LICENSE.TXT</li>
</ul>
<p>The quick idea behind the ParticlesBehavior is that it will be attached to the LayoutRoot canvas. The behavior will handle the canvas&#8217;s MouseMove event and store the current mouse position. When the ParticlesBehavior is notified that it should show particles, it creates an instance of the ParticleControl and positions it by setting the OffsetX and OffsetY properties.</p>
<p>We start by changing the class declaration so that ParticlesBehavior inherits from Behavior&lt;Canvas&gt;. Now throughout code, AssociatedObject will be of type Canvas. This also means that this behavior can only be attached to objects of type Canvas. In the OnAttached method an event handler is created for the canvas&#8217; MouseMove event whereas in the OnDetaching method the event handler is removed. When the mouse is moved, the current position is recorded.</p>
<pre class="brush: csharp; gutter: false;">
public class ParticlesBehavior : Behavior&lt;Canvas&gt;
{
private Point currentMousePosition;

protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
}

protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
}

void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
{
currentMousePosition = e.GetPosition(null);
}
}
</pre>
<p>Now we need a way to trigger when the control will be shown or hidden. This is achieved by adding a command to ParticlesBehavior called ShowParticles. In the constructor, instantiate a new ActionCommand and pass it an Action delegate. The method that you use can have no parameters or one parameter (of type object). Later when triggers are associated with this behavior you can set the value of the CommandParameter. If you have a method that passes an object parameter, you can get access to the parameter.</p>
<p>This command then creates and sets properties on the ParticleControl and adds it to the Children collection of LayoutRoot. Update the ParticlesBehavior class to include this code:</p>
<pre class="brush: csharp; gutter: false; highlight: [5,6,7,8,27,28,29,30,31,33,34,35,36,37,38,39,40,41];">
public class ParticlesBehavior : Behavior&lt;Canvas&gt;
{
    private Point currentMousePosition;

    public ParticlesBehavior()
    {
        this.ShowParticles = new ActionCommand(this.OnShowParticles);
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
    }

    void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
    {
        currentMousePosition = e.GetPosition(null);
    }

    public ICommand ShowParticles
    {
        get;
        private set;
    }

    private void OnShowParticles()
    {
        ParticleControl p = new ParticleControl();

        p.OffsetX = currentMousePosition.X;
        p.OffsetY = currentMousePosition.Y;

        AssociatedObject.Children.Add(p);
    }

}
</pre>
<p>If you compile the project, you will now see the ParticlesBehavior in the Assets panel. Drag and drop it on LayoutRoot. The properties panel will have a ShowParticles group created for the ShowParticles Command and an empty Triggers list:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec3.png" alt="" /></p>
<p>Click the plus sign to the right of Triggers and pick EventTrigger and set the SourceName (staplerPath) and EventName (MouseLeftButtonDown):</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec4.png" alt="" /></p>
<p>What this is saying is that when the staplerPath object is clicked then it will call the ShowParticles command on this behavior. You could also specify a CommandParameter which you then could use if OnShowParticles had an object parameter defined. When you click the plus sign next to Triggers again, you can specify another EventTrigger to be called when idolPath is clicked. Repeat this for all the paths.</p>
<p>Run the game now and click on an item. You should see lots of particles flowing from that location. Find another object and click it and there will be a second stream of particles. The ParticleControl currently doesn&#8217;t have a way to stop the particles from flowing. We will take care of that in a minute. But let&#8217;s first try to figure out what is happening between the triggers and the behavior. The command defined on the behavior is an ICommand, it knows nothing about triggers. And triggers just know when something is going to happen. How does it know to call a command on the behavior? The answer is the InvokeCommandAction that is created behind the scenes. This can be seen in a diagram from the Expression Blend SDK User Guide:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec5.png" alt="" /></p>
<p>It might be a good learning opportunity to intercept the trigger before it calls InvokeCommandAction and see it doing the work of connecting the trigger to the behavior. Let&#8217;s create a trigger that we can use and debug. In a new folder under Interactivity called DebugEventTrigger, create a class called DebugEventTrigger.cs:</p>
<pre class="brush: csharp; gutter: false;">
public class DebugEventTrigger : System.Windows.Interactivity.EventTrigger
{
    public DebugEventTrigger() : base() {}

    public DebugEventTrigger(string eventName) : base(eventName) {}

    protected override void OnEvent(EventArgs eventArgs)
    {
        base.OnEvent(eventArgs);
    }
}
</pre>
<p>We derive the class from EventTrigger so we can use it just list the triggers we have already used. Compile the project and in Blend, add this trigger to the triggers collection on the ParticlesBehavior and select the SourceName and EventName. Switch back to Visual Studio and set a breakpoint in the OnEvent method. Run the app in debug mode from Visual Studio. When the breakpoint is hit, look at the Locals window and you will see that the trigger has a single action, InvokeCommandAction, the CommandName is &#8220;ShowParticles&#8221; and the AssociatedObject is ParticlesBehavior.</p>
<p><a href="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec6.png"><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec6.png" alt="" width="503" height="279" /></a></p>
<p>Back to the issue of the never-ending particles. To fix this, we need just one set of changes to ParticleControl. The UpdateParticles method is the heart of the control and is called each time the Silverlight rendering process renders a frame. It updates existing particles, creates new particles, and removes expired particles. The particles are created 10 at a time until the particles collection reaches the MaxParticleCount. But because particles are also being removed from this collection the result is a never-ending flow of particles. Change <strong>this.particles.Count</strong> to <strong>totalParticlesCreated</strong> and define it as a private field at the top of the class definition as type int. At the end of the SpawnParticle method, increment the totalParticlesCreated value by one. Now the control will create particles until MaxParticleCount is reach and stop.</p>
<p>We have a fully-functioning ParticlesBehavior.</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec7.png" alt="" /></p>
<p>The last thing I did was add the dependency properties from ParticleControl (StartColor, EndColor, Fuzziness, MaxParticleCount, OriginVariance, Speed, ParticleSize, ParticleSizeVariance, Life, and LifeVariance) to ParticlesBehavior and set the values for each created instance of the ParticleControl. This gives a nice design-time experience to configure the particles. I changed a few values to better time the particles with the length of the sound played when an item is clicked:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec8.png" alt="" /></p>
<p>There is one final thing we need to do when an item is clicked. Right now you can click on an item multiple times and it will play a sound and generate particles each time. To solve this, we can use the RemoveElementAction. This action removes the targeted element from its parent control. Drag the RemoveElementAction onto the staplerPath object. Since the trigger defaults to EventTrigger and MouseLeftButtonDown and the TargetName defaults to the AssociatedObject then we don&#8217;t have to change anything:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0325_HiddenObjec9.png" alt="" /></p>
<p>When we click on the staplerPath then it will:</p>
<ul>
<li>Change the opacity of its corresponding TextBlock (ChangePropertyAction)</li>
<li>Play a sound (PlaySoundAction)</li>
<li>Spray particles (ParticlesBehavior)</li>
<li>Be removed from its parent control (RemoveElementAction)</li>
</ul>
<p>Download the <a href="http://www.shazaml.com/downloads/ClutteredCubeSource4.zip">source</a> for this episode.</p>
<p>In our <a href="http://www.shazaml.com/archives/hidden-object-episode-5">next episode</a>, we will use a magnifer pixel shader effect wrapped in a behavior to allow the user to zoom in on portions of the image.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/hidden-object-episode-4/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
