最近在學習Flash,準備每天都花點時間去用Silverlight和Flash去實現些相同的功能。
我之前有學過半年的Silverlight,因爲完全可以用C#來寫,最多學習了半年的Silverlight,比較扯得是項目裏竟然用flash,我發現還是得懂點flash。。
在園子裏看到夏天的樹每天學點flash系列不錯,我就試着學習他每天學點。有些功能都是和他的一樣,但是代碼我完全都是我寫的。
1.簡單加密功能實現:
通過獲取當前的天來判斷是否等於某個數,不同的情況我們讓程序顯示不同的內容。
a.使用Flash實現:
i. 創建一個Flash文件:
使用ActionScript3.0。
ii.在默認的那一幀中添加一個文本如下圖:
iii.插入一個新的關鍵幀,並把它的文字設置爲"對不起,系統已經過期"如下圖:
並保存文件爲"Encryption.fla"。
iv.新建一個as文件,並保存爲Encryption.as:
需要注意的是:Encryption.as和Encryption.fla必須保存在同一目錄下。
as文件的代碼:
{
import flash.events.MouseEvent;
import flash.system.ApplicationDomain;
import flash.events.Event;
import flash.display.MovieClip;
public class Document extends MovieClip{
public function Document(){
this.addEventListener(Event.ADDED_TO_STAGE,on_added_to_stage);
}
private function on_added_to_stage(e:Event):void{
// the MovieClip stoped at the first frame
stop();
// get the datetime
var myday:Date = new Date();
var mydate:Number = myday.getDate();
//trace(mydate); ----- 12
if(mydate==12)
{
this.gotoAndStop(2);
}
}
}
}
最後一步是要把這個as文件綁定到fla文件。
b.使用SIlverlight實現:
i.創建一個Silverlight應用程序,如下圖:
ii.xaml文件內容:
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 Width="550" Height="400">
7
8 <Grid x:Name="LayoutRoot" Background="#FF9865">
9 <TextBlock Height="23" HorizontalAlignment="Left" Margin="190,176,0,0" Name="tblk1" Text="歡迎你使用這個系統" VerticalAlignment="Top" Width="177" FontSize="13" FontFamily="Comic Sans MS" Foreground="#FF0000" />
10 </Grid>
11 </UserControl>
iii.cs文件的內容:
比較:Silverlight程序耗時:5分鐘; Flash程序耗時:10分鐘。(只是我個人的使用時間,我是個actionscript新手)
--------------------------------------------------------------------------------------------------------------------------------------
2.讀取XML文件:
功能描述:讀取一個XML文件的內容,並讓它顯示在一個界面。
XML文件的內容:
<GALLERY>
<IMAGE TITLE="Nicholas">image1.jpg</IMAGE>
</GALLERY>
a.使用Flash實現:
i.創建一個Actionscript文件也就是as文件起名爲"ReadXML.as":
具體的創建方法上面例子裏有說明。
as文件中需要使用ActionScript實現的功能:加載xml文件,並讓它的節點內容顯示在第一幀。代碼:
{
import flash.system.System;
import flash.system.ApplicationDomain;
import flash.events.Event;
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
public class ReadXML extends MovieClip{
public function ReadXML(){
this.addEventListener(Event.ADDED_TO_STAGE,on_added_to_stage);
}
private function on_added_to_stage(e:Event):void{
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("Nicholas.xml"));
myLoader.addEventListener(Event.COMPLETE,processXML);
}
private function processXML(e:Event):void{
var myXML:XML;
var myText:TextField = new TextField();
var myArray:Array = new Array();
myXML = new XML(e.target.data);
// push each Title to the array
for (var i:int = 0; i<myXML.*.length(); i++){
trace("My image number is " + (i+1) + ", it's title is " + myXML.IMAGE[i].@TITLE + " and it's URL is " + myXML.IMAGE[i]);
myArray.push(myXML.IMAGE[i].@TITLE);
}
// trace(myXML.IMAGE.@TITLE);
myText.text = myXML.IMAGE.@TITLE;
addChild(myText);
myText.x=220;
myText.y=200;
myText.textColor=0xFF00000;
myText.thickness=5;
}
}
}
ii.創建一個fla文件,最好和ReadXML.cs文件在同一目錄下,並這個flash文件添加後臺代碼:
iii.運行程序。
b.使用Silverlight實現:
i.創建一個SIlverlight應用程序;
方法在上面那個例子裏有說明。
ii.把上面那個XML文件添加到項目中:
iii.在xaml中需要添加一個控件Canvas:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="550" Height="400">
<Grid x:Name="LayoutRoot" Background="#FF9865">
<Canvas x:Name="canvas" Width="550" Height="400"></Canvas>
</Grid>
</UserControl>
iv.使用linqtoxml讀取xml節點,並把內容通過一個textblock顯示在前臺。
{
InitializeComponent();
Loaded+=new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, EventArgs e)
{
var images = from img in XElement.Load("Nicholas.xml").Elements("IMAGE")
select img;
TextBlock block = new TextBlock();
block.SetValue(Canvas.TopProperty, 200.0);
block.SetValue(Canvas.LeftProperty, 220.0);
block.Text = "";
foreach (var image in images)
{
block.Text+= image.FirstAttribute.Value;
}
canvas.Children.Add(block);
}
}
對比:Silverlight耗時5分鐘,Flash耗時15分鐘(包括查找資料和類的使用學習)