23 October 2007
Silverlight: Keep it simple stupid.
A while back I posted about how I used our AJAX Control Toolkit to animate with Silverlight. I stated that the end reason for this approach was simply due to lack of animation capabilities within Silverlight 1.0.
Michael Scherotter being the brilliant Silverlight coder, decided to prove that theory wrong (heh - not so much, probably more to acid test my b.s factor). He came back with an approach that was simple and yet humiliating (for me) at the same time (I'm so embarrassed).
1: void Handle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
2: {
3: switch (m_state)
4: {
5: case ShadeState.down:
6: m_state = ShadeState.goingUp;
7: UpHandleStart.Value = m_upHandleStart;
8: UpShadeStart.Value = m_upShadeStart;
9: Up.SpeedRatio = 1.0;
10: Up.Seek(new TimeSpan(0));
11: Up.Begin();
12: break;
13:
14: case ShadeState.goingDown:
15: m_state = ShadeState.goingUp;
16: Down.Pause();
17: UpHandleStart.Value = HandleTranslation.Y;
18: UpShadeStart.Value = Shade.Height;
19: Up.SpeedRatio = 2.0;
20: Up.Begin();
21: break;
22:
23: case ShadeState.goingUp:
24: m_state = ShadeState.goingDown;
25: Up.Pause();
26: DownHandleStart.Value = HandleTranslation.Y;
27: DownShadeStart.Value = Shade.Height;
28: Down.SpeedRatio = 2.0;
29: Down.Begin();
30: break;
31:
32: case ShadeState.up:
33: m_state = ShadeState.goingDown;
34: DownHandleStart.Value = m_downHandleStart;
35: DownShadeStart.Value = m_downShadeStart;
36: Down.Seek(new TimeSpan(0));
37: Down.SpeedRatio = 1.0;
38: Down.Begin();
39: break;
40: }
Essentially what he's done is wire up the animation(s) to mouse events (i.e. when the user clicks, make sure you fire this methods). He's then put together 4 states so to speak and based on where things in time are at, he adjusts and forces the animation to react accordingly.
Simple!
This problem reminds me of a game I once wrote in Flash. It was a XMAS game where Santa would kick presents into chimneys based on where the power bar was at (i.e. user needs to guess the right amount of power). I wrote the game in such a way that it used gravity, physics etc and took forever. It took a Flash Designer on my team to show me that all that was need was a few motion paths and when the power bar got within certain ranges, it would fire off the right animation path (ie 50-60% meant gotoAndPlay(3)).
Amazing how one can write 115 lines of code for "Hello World" when all you need is 2 lines.
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.