This is a new request from my friend Pierre. He wants to implement a POC to demonstrate how to control UI objects on the stage. That’s a very good sample for your reference if you want to implement some layout application.
If you want to add a resize functionality to the object, you may refer to my previous post Image Manipulation - Scale.
Comparison
Flash implementation: 75 minutes(Implemented First)
Silverlight implementation: 55 minutes
What’s the difference:
- Creating random color
- Limit the range of Text Input
Source codes
Controlling UI Object [Flash 9, AS3] (13.6 KiB, 450 hits)
Controlling UI Object [Silverlight 2, C#] (12 KiB, 850 hits)
Flash
Silverlight
Creating random color
Creating random color is very easy in AS3 if you know the way. Just one line!
// AS3 var color:uint = uint(0xFFFFFF * Math.random());
For C#, here is one of the approaches. Does anyone have a better solution?
// C# Random r = new Random(); byte red = (byte)(255 * r.NextDouble()); byte green = (byte)(255 * r.NextDouble()); byte blue = (byte)(255 * r.NextDouble()); Color color = Color.FromArgb(255, red, green, blue);
Limiting the range of Text Input
Sometime, you may want to limit the range of numeric values entered by the user. Here is my solution.
// AS3
// limit the input to numeric value
_textField.restrict = "01234567890";
_textField.addEventListener(Event.CHANGE, on_change);
private function on_x_change(e : Event):void{
var value:Number = parseInt(_textField.text);
value = Math.max(MIN_VALUE, Math.min(MAX_VALUE, value));
// update the text field value again
_xText.text = value.toString();
}
For Silverlight, it’s more or less the same.
// C#
TextField.TextChanged += new TextChangedEventHandler(TextChanged);
void TextChanged(object sender, TextChangedEventArgs e)
{
try
{
double value = Convert.ToInt16(TextField.Text);
value = Math.Max(MIN_VALUE, Math.Min(MAX_VALUE, value));
TextField.Text = value.ToString();
}
// If user attempt to add non-numeric value to the text box
catch (Exception ex)
{
TextField.Text = "";
}
}

February 24th, 2009 at 4:43 am
You could create a random color in less code like this:
Random r = new Random();
Color c = Color.FromArgb(255, (byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255));
But I think that’s it!
February 24th, 2009 at 5:17 am
Or this
Color c = Color.FromArgb(new Random().Next() & 0xffffff);
February 24th, 2009 at 5:24 am
In addition, your TextChanged event method body could look something like this:
int value = MIN_VALUE;
if (int.TryParse(TextField.Text, out value))
value = Math.Max(MIN_VALUE, Math.Min(MAX_VALUE, value));
TextField.Text = value.ToString();
February 24th, 2009 at 9:32 am
Glad to see that sl version is really much better than the flash one.
February 24th, 2009 at 11:10 am
thanks for all of the suggestion, they are very useful!
February 24th, 2009 at 8:31 pm
Hi,
It seems that for some of the objects, “Bring Forward” does not work.
What I did is to add about 10-15 new objects and then to select one of them and click the button.
February 25th, 2009 at 12:47 am
Ivan,
the bring forward is a very simple implementation and it will increment the z-index one by one. That’s means it’s working but you have to press the button for few more times.
February 25th, 2009 at 5:02 am
Inside TextChanged for SL:
{
var t = (sender as TextBox);
t.Text = Regex.Replace(t.Text, “\\D”, “”, RegexOptions.None);
}
Although you need to add the namespace (System.Text.RegularExpressions).
What framerate do you specify for your Flash stuff?
Nice work by the way.
February 25th, 2009 at 5:49 am
Oh, yeah sorry I forgot to mention that with \D you need to specify the regular expression negation of your numeric range. Just trying to demonstrate that SL is better! :)
February 25th, 2009 at 1:09 pm
Bill, Thanks for the suggestion.
By the way, you raised a good question, the frame rate of Flash is 24 while the default frame rate of Silverlight is 60. That’s why Silverlight App look smoother.
February 28th, 2009 at 6:57 pm
[...] Zu sehen im Eintrag “Flash vs Silverligh: Controlling UI Object“. [...]
April 29th, 2009 at 5:38 am
Hey, I like your comparisons. But you have to stop comparing apples to oranges. If you specify a low framerate for Flash and SL is running at a high one, of course they are going to look different. Give both pieces the same framerate. This is misleading to visitors that may not understand what is going on.
January 30th, 2010 at 12:32 am
Saltmarch Media is organizing India Game Developer Summit Event in Bangalore. This Summit will be a boost for the Game Developing Industries. It covers the topics like Online Gaming, Gaming Business, Gaming Career, Audio in Gaming, Flash, UI, Flex, Mobile Gaming and Adobe Flash Platform and has 1 day workshop at the end as well. Any one attending this event?
Register at gamedevelopersummit dot com
July 10th, 2010 at 3:47 am
great post dude..
thanks..