Develop Silverlight on Eclipse : Eclipse4SL Flash vs Silverlight: Custom Loading Splash Screen
Feb 24

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

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 = "";
	}
}

Shares and Enjoy~

Did you like this post?

Subscribe here:  

14 Responses to “Flash vs Silverlight: Controlling UI Object”

  1. Bill Sithiro Says:

    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!

  2. Dan Neely Says:

    Or this

    Color c = Color.FromArgb(new Random().Next() & 0xffffff);

  3. Dan Neely Says:

    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();

  4. forever Says:

    Glad to see that sl version is really much better than the flash one.

  5. admin Says:

    thanks for all of the suggestion, they are very useful!

  6. Ivan Dragoev Says:

    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.

  7. admin Says:

    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.

  8. Bill Sithiro Says:

    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.

  9. Bill Sithiro Says:

    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! :)

  10. admin Says:

    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.

  11. Programming with Silverlight, WPF & .NET » Silverlight vs Flash: Controlling UI Object Says:

    [...] Zu sehen im Eintrag “Flash vs Silverligh: Controlling UI Object“. [...]

  12. Jake Says:

    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.

  13. Roop Sharma Says:

    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

  14. syabac Says:

    great post dude..
    thanks..

Leave a Reply