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

I believe you will always make use of the scaling tool insider Flash or Blend. However, have you ever thought of achieving image scaling by coding?

That’s not an easy technique. You have to take care of the mouse position, image position, scale factor, cursor look, etc..

But don’t worry, here you could find a very simple demo for manipulating the size of the image. My sample is not intended to give you a complete/portable solution for Image Scaling. Instead, it provides you a basic idea how this is working.

Comparison

Flash implementation: 50 minutes

Silverlight implementation: 70 minutes (Implemented First)

Source codes

Flash

Silverlight

Silverlight: Create a custom property in XAML definition

If you open the Silverlight Source Code, you will find that the image is passed into the Image Manipulation Class directly in XAML. Here is part of the XAML code:

<custom:ImageManipulationScale VerticalAlignment=”CenterHorizontalAlignment=”Center“>
    <custom:ImageManipulationScale.Source>
        <Image Source=”shinedraw_logo.jpg” Width=”396” Height=”300“/>
   </custom:ImageManipulationScale.Source>
</custom:ImageManipulationScale>

It’s an important technique that everyone must know it. It definitely helps you to write a more professional control.

To start with, we have to define the property name and the property type. In the sample above, the property name is Source and the type is Image. Then, we are going to add some codings behind the Class ImageManipulationScale.

// C#
// create a custom property than can be exported to XAML
public static readonly DependencyProperty SourceProperty =
	DependencyProperty.Register(
	"Source", typeof(Image),
	typeof(ImageManipulationScale), null
	);

// define a public property with the name Source
public Image Source
{
	get { return (Image)GetValue(SourceProperty); }
	set
	{
		 SetValue(SourceProperty, value);
		// do whatever you want here
	}
}

That’s all! Pretty simple, right?

Jan 20

This is the second game example in Shine Draw. Snake Snacks is the most famous mini game in mobile world. I am sure everyone has played it before. The logic of this game is very simple and it’s not that hard to implement as well.

Samples source codes are provided. If you are interested to implement game, I am sure it’s a good place for you to start!

I will continue to implement more game in the coming day. If you have any simple game idea, please let me know and I can help you to create it.

Let’s Snake Snacks!!

Comparison

Flash implementation: 45 minutes
Silverlight implementation: 1 hour 45 minutes (Implemented First)
What’s the difference?

  • Check the pressed Key Code [AS3] vs [C#]

Source codes

Flash

Silverlight

Check the pressed Key Code [AS3] vs [C#]

If you want to check the capture the pressed key code, the best way in Flash is to attach KeyBoardEvent to the stage. (To make sure that it can always capture the key board event). For example:

// AS3
stage.addEventListener(KeyboardEvent.KEY_DOWN, on_key_down);

private function on_key_down(e : KeyboardEvent):void{
	if (e.keyCode == Keyboard.RIGHT) {
		// do something
	}

	switch(e.keyCode){
		case Keyboard.RIGHT:
		  // do something;
		  break;
	}
}

The sample for Silverlight, I will prefer to attach the handler to the RootVisual.

// C#
App.Current.RootVisual.KeyDown += new KeyEventHandler(RootVisual_KeyDown);

void RootVisual_KeyDown(object sender, KeyEventArgs e)
{
	if (e.Key == Key.Right){
		// do something

	}

	switch (e.Key){
		case Key.Right:
		  // do something;
	}
}
Jan 10

In Flash and Silverlight, there are many ways to call a method by a regular time interval. Today I will use a simple sample to illustrate their differences on the following methods:

  • Flash - flash.utils.Timer
  • Flash - Event.ENTER_FRAME
  • Silverlight - System.Windows.Threading.DispatcherTimer
  • Silverlight - CompositionTarget.Rendering

The samples are running with 10 FPS (Frame rate per second) and the timer interval is set to be 100 milliseconds.

Comparison

Flash implementation: 30 minutes
Silverlight implementation: 30 minutes (Implemented First)
What’s the difference?

  • Flash: Timer vs Enter Frame
  • Silverlight: Dispatcher Timer vs Composition Target

Source codes

Flash

Silverlight

Flash: Timer vs Enter Frame

(The result I described below may be varies in different browser or different OS)

Both of the method generated exactly the same count value. However, you will find that the number of handlers are called less than expected.

It is supposed for each second, the counter will be increased by 10. But the actual case is that the counter is less than the expected value as time goes by.

Silverlight: Dispatcher Timer vs Composition Target

I think Silverlight has more funny outcomes.

Firstly, the increment in Dispatcher Timer is far less than the value in that of Composition Target. (They are expected to be the same.)

Besides that, CompositionTarget.Tick will be stopped if the Application is not shown on the screen. Here is a simple trial, just scroll up your browser until the Application is completely moved out of the screen. Scroll back to the Application a while later. You will find that Dispatcher Timer will has a bigger value.

Jan 09

As promised before, I will make a Flash version after implementing the 3D Image Rotator in Silverlight.

The Silverlight version is implemented using Kit3D while the Flash version is made by Papervision3D. Personally, I love the Silverlight sample more since it looks smoother. However, I think Papervision3D is much more easy to understand and utilize.

By the way, the images used in the Flash version are taken from my graduated school - The Hong Kong University of Science and Technology. Hope you will like it~

Demo

Source codes

Flash

Silverlight