2010年11月30日 星期二

[AS3] 亂數隨機載入外部圖檔(輪播)_每一輪不重覆

外部有6個圖檔,依亂數載入。同一輪已載入過的就不再載入。第1輪播完再播第2輪......



//991228從991130_randomPhoto_1.fla修改

import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
//載入Tweener、ColorShortcuts類別
//請至以下URL下載tweener類別程式庫與說明文件
//http://code.google.com/p/tweener/
import caurina.transitions.Tweener;
import caurina.transitions.properties.ColorShortcuts;
//初始化ColorShortcuts
ColorShortcuts.init();

//****** photo陣列 - push() 加入外部圖檔檔名
var photo:Array = new Array();
photo.push("991130_p1.jpg");
photo.push("991130_p2.jpg");
photo.push("991130_p3.jpg");
photo.push("991130_p4.jpg");
photo.push("991130_p5.jpg");
photo.push("991130_p6.jpg");

//
var pNum:uint = photo.length;

//****** intervalDuration - 換圖秒數(毫秒)
//目前設為 5000豪秒,即 5 秒
var intervalDuration:Number = 5000;

//TextFormat物件設定文字格式
var aTextFormat:TextFormat = new TextFormat();
aTextFormat.size = 18;
aTextFormat.color = 0xffffff;
aTextFormat.leftMargin = 5;
aTextFormat.rightMargin = 5;
//宣告TextField文字欄位類別實體infoTextField
var infoTextField:TextField = new TextField();
infoTextField.x = 15;
infoTextField.y = 15;
infoTextField.background = true;
infoTextField.backgroundColor = 0xff2200;
infoTextField.autoSize = TextFieldAutoSize.LEFT;
//將 infoTextField 加入顯示清單
addChild(infoTextField);
//建構Loader類別實體ldr
var ldr:Loader = new Loader();
//
ini();
//
function ini():void
{
 loadPhoto();
}
//
function loadPhoto()
{
 //如果photo陣列元素都被移除掉了
 //以for迴圈重新加入各元素,即外部檔名,同初始值
 if(photo.length == 0){
  for (var i:uint=0; i < pNum; i++) { // pNum
   photo[i] = "991130_p" + String(i+1) + ".jpg";
  }
  trace("------------------------------------------");
 }
 
 //Math.random() 亂數產生0(含)~1(不含)之浮點數, 
 //乘以圖檔張數(即photo陣列長度 photo.length),會得到 0(含)~6(不含) 之符點數
 //Math.floor - 去掉小數 - 會得到0~5之正整數
 var photoRandom:uint = Math.floor(Math.random() * photo.length);
 //photo[photoRandom] - 因應亂數,做為陣列索引,指向陣列元素
 var photoURL:String = photo[photoRandom];
 //splice(起始索引, 刪除數量) - 刪除被選定的陣列元素
 //如此在每一循環載入圖檔,才不會重覆
 photo.splice(photoRandom, 1);
 trace(photoURL);
 //將photoURL所存的圖檔檔名字串指定給 infoTextField 文字欄位顯示
 infoTextField.text = photoURL;
 //setTextFormat()方法,將aTextFormat格式化物件與infoTextField文字欄位關聯
 infoTextField.setTextFormat(aTextFormat);
 //
 //Loader類別實體ldr,以load()方法載入new URLRequest()的相對網址(即圖檔檔名)
 ldr.load(new URLRequest(photoURL));
 //將Event.COMPLETE事件的偵聽程式onLoaderComplete,附加到Loader類別實體ldr的contentLoaderInfo屬性 
 ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
}

//
function onLoaderComplete(e:Event):void
{
 addChild(ldr);
 //將ldr可視物件實體,以x,y座標控制在舞台中央
 ldr.x =  (stage.stageWidth - ldr.width) / 2;
 ldr.y =  (stage.stageHeight - ldr.height) / 2;
 //呼叫漸變屬性設定函式tweenPhoto1()
 tweenPhoto1();
 //setTimeout(closure:Function, delay:Number, ... arguments):uint
 //在指定的延遲時間 (以毫秒為單位) 之後執行指定的函數
 var intervalId:uint = setTimeout(ini, intervalDuration);
}
//
function tweenPhoto1():void{
 Tweener.addTween(ldr, {alpha:0, scaleX:0.9, scaleY:0.9, time:0, onComplete:tweenPhoto2});
} 
function tweenPhoto2():void{
 Tweener.addTween(ldr, {alpha:1, scaleX:1, scaleY:1, time:1.5});
} 

◎另需參閱
指定類別檔路徑 - 以下載及設置Tweener類別庫為例 http://flash.twgogo.org/2010/12/blog-post.html

2010年11月21日 星期日

[AS3]StyleSheet與TextField - 將 StyleSheet 物件套用至含有 htmlText 屬性的文字欄位

var style:StyleSheet = new StyleSheet();建構一個樣式表物件
StyleSheet.setStyle(styleName:String, styleObject:Object):void
以指定的名稱,將新的樣式新增至樣式表物件。
TextField.styleSheet = StyleSheet Object;
將樣式表附加至文字欄位

  1. TextField文字欄位中不是純文字就是HTML文字。純文字的字串指定給TextField類別實體的text屬性;HTML文字的字串則指定給TextField類別實體的htmlText屬性。
  2. 以flash.text.StyleSheet類別來處理CSS宣告。new StyleSheet()建構StyleSheet物件。之後透過一組物件來定義樣式屬性及其值。呼叫StyleSheet.setStyle(對應的選擇器名稱字串, 樣式物件)方法。
  3. 將樣式表StyleSheet物件指定給文字欄位TextField類別實體的styleSheet屬性,將文字欄位以CSS樣式格式化。
  4. 含有樣式表的文字欄位無法再編輯。 若將文字欄位的 type 屬性設定為 TextFieldType.INPUT, StyleSheet會 套用到文字欄位的預設文字,但就無法再對內容進行編輯。 因此,若有輸入文字需求,則改用 TextFormat 類別,將樣式指定給輸入文字欄位。
  5. 程式碼重要順序→樣式屬性內容必須先設定,接著將樣式表套用至 TextField 物件,然後才能設定 htmlText 屬性,如此 CSS 樣式才能實際生效,表現在TextField的Html文字上。
  6. 下列 ActionScript API 無法在含有指定樣式表的文字欄位中使用:• TextField.replaceText() 方法• TextField.replaceSelectedText() 方法• TextField.defaultTextFormat 屬性• TextField.setTextFormat() 方法

SWF



//
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.StyleSheet;
//
var style:StyleSheet = new StyleSheet();
//
var styleObj_1:Object = new Object();
styleObj_1.color = "#ff0000";
styleObj_1.fontSize = 88;
style.setStyle(".red", styleObj_1);
//
var styleObj_2:Object = new Object();
styleObj_2.color = "#00ff00";
styleObj_2.fontSize = 66;
style.setStyle(".green", styleObj_2);
//
var styleObj_3:Object = new Object();
styleObj_3.color = "#0000ff";
styleObj_3.fontSize = 44;
style.setStyle(".blue", styleObj_3);
//
var tf:TextField = new TextField();
//
tf.styleSheet = style;
//
tf.htmlText = "< span class = 'red'>●RED< /span>< span class = 'green'>●GREEN< /span>
< span class = 'blue'>●BLUE< /span> text";
//
tf.x = 50;
tf.y = 8;
tf.width = 460;
//
tf.multiline = true;
tf.wordWrap = true;
tf.autoSize = TextFieldAutoSize.LEFT;
//

addChild(tf);
//style.clear();


Flash Player 支援原始 CSS1 規格 (www.w3.org/TR/REC-CSS1) 中屬性的子集。 請點擊下圖連結至Flash Help瀏覽所支援的「階層式樣式表」(CSS) 屬性和值,以及它們的對應 ActionScript 屬性名稱。


適用於 Adobe  Flash  Professional CS5 的 ActionScript  3.0 參考__flash.text.StyleSheet 類別
適用於 Adobe  Flash  Professional CS5 的 ActionScript  3.0 參考__flash.text.StyleSheet.setStyle()方法
適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__flash.text.TextField.styleSheet屬性

2010年11月18日 星期四

[AS3]TextField.getTextFormat(beginIndex, indIndex)方法

getTextFormat(beginIndex:int = -1, endIndex:int = -1):flash.text:TextFormat
會傳回 TextFormat 物件,包含 beginIndex 和 endIndex 參數所指定之文字範圍的格式資訊

UI組件Button的實體開啟了Toggle屬性,切換指定文字欄位tf3的TextFormat屬性



import fl.controls.Button;
import flash.events.Event;
import flash.text.TextFormat;
//
var tf1:TextField = new TextField();
tf1.autoSize = TextFieldAutoSize.LEFT;
tf1.text = "1.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf1.x = 30;
tf1.y = 30;
tf1.background = true;
addChild(tf1);
//
var tf2:TextField = new TextField();
tf2.autoSize = TextFieldAutoSize.LEFT;
tf2.text = "2.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf2.x = 50;
tf2.y = 60;
tf2.background = true;
addChild(tf2);
//
var tf3:TextField = new TextField();
tf3.autoSize = TextFieldAutoSize.LEFT;
tf3.text = "3.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf3.x = 70;
tf3.y = 115;
tf3.background = true;
addChild(tf3);
//
var format1:TextFormat = new TextFormat();
format1.color = 0x0000FF;
format1.font = "04b_08";
format1.size = 24;
//
var format2:TextFormat = new TextFormat();
format2.color = 0xFF2255;
format2.font = "Times New Roman";
format2.size = 36;
//
var format3:TextFormat = new TextFormat();
format3.color = 0x662255;
format3.size = 42;
format3.font = "Orator Std";
//trace(format3);
//
var startRange:int = 7;//啟始字元 (0 base)
var endRange:int = 12;//結束字元+1 (0 base)
tf1.setTextFormat(format1);
tf2.setTextFormat(format2, startRange);
tf3.setTextFormat(format3, startRange, endRange);

//new運算子建構Button組件實體myButton
var myButton:Button = new Button();
addChild(myButton);
myButton.width = 250;
myButton.label = "取得 tf1 的 format1,據以改變 tf3 的文字格式";//取得或設定組件的文字標籤
myButton.toggle = true;//開啟-是否可以切換按鈕
/*selected屬性
http://help.adobe.com/zh_TW/Flash/CS5/AS3LR/fl/controls/LabelButton.html#selected
myButton.selected = true;
將Button組件實體myButton移動到座標(50,230)
*/
myButton.move(200, 210);
//;
var format:TextFormat = new TextFormat();
format = tf1.getTextFormat();
//
myButton.addEventListener(Event.CHANGE, changeFormat);
function changeFormat(e:Event):void
{
 if (e.currentTarget.selected)
 {
  tf3.setTextFormat(format, startRange, endRange);
  e.currentTarget.label = "回復 tf3 原本格式";
 }
 else
 {
  tf3.setTextFormat(format3, startRange, endRange);
  e.currentTarget.label = "取得 tf1 的 format1,據以改變 tf3 的文字格式";
 }

}


適用於 Adobe  Flash  Professional CS5 的 ActionScript  3.0 參考__flash.text.TextField.getTextFormat()方法

適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__fl.controls.LabelButton.selected屬性

2010年11月17日 星期三

[AS3] TextField.setTextFormat(format, beginIndex, endIndex):void

setTextFormat()方法
public function setTextFormat(format:flash.text:TextFormat, beginIndex:int = -1, endIndex:int = -1):void
參數
format:flash.text:TextFormat — TextFormat 物件,包含字元和段落格式資訊。
beginIndex:int (default = -1) — 選擇性;整數,指定從零開始的索引位置,以指定想要的文字範圍的第一個字元。
endIndex:int (default = -1) — 選擇性;整數,指定想要的文字範圍後的第一個字元。依原設計,如果指定 beginIndex 和 endIndex 值,就會更新從 beginIndex 到 endIndex-1 的文字。


  
  1. my_textField.setTextFormat(textFormat:TextFormat)
    將 textFormat 的屬性套用到文字欄位中的所有文字
  2. my_textField.setTextFormat(textFormat:TextFormat, beginIndex:int)
    會將 textFormat 的屬性套用到從 beginIndex 位置開始的字元。
  3. my_textField.setTextFormat(textFormat:TextFormat, beginIndex:int, endIndex:int)
    將 textFormat 參數的屬性套用到從 beginIndex 位置到 endIndex-1 位置的文字範圍。

SWF



//
var tf1:TextField = new TextField();
tf1.autoSize = TextFieldAutoSize.LEFT;
tf1.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf1.x = 50;
tf1.y = 30;
tf1.background = true;
addChild(tf1);
//
var tf2:TextField = new TextField();
tf2.autoSize = TextFieldAutoSize.LEFT;
tf2.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf2.x = 50;
tf2.y = 60;
tf2.background = true;
addChild(tf2);
//
var tf3:TextField = new TextField();
tf3.autoSize = TextFieldAutoSize.LEFT;
tf3.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf3.x = 50;
tf3.y = 115;
tf3.background = true;
addChild(tf3);
//
var format1:TextFormat = new TextFormat();
format1.color = 0x0000FF;
format1.font = "04b_08";
format1.size = 24;
//
var format2:TextFormat = new TextFormat();
format2.color = 0xFF2255;
format2.font = "Times New Roman";
format2.size = 36;
//
var format3:TextFormat = new TextFormat();
format3.color = 0x662255;
format3.font = "Orator Std";
format3.size = 42;
//
var startRange:int = 6;
var endRange:int = 11;
tf1.setTextFormat(format1);
tf2.setTextFormat(format2, startRange);
tf3.setTextFormat(format3, startRange, endRange);


適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__flash.text.TextField.setTextFormat()方法

2010年11月16日 星期二

[AS3]嵌入字型 - TextField.embedFonts ( 三 )_嵌入外部引入的字型資訊

public static function registerFont(font:Class):void
在全域字體清單中註冊字體類別
例:Font.registerFont(ShareFont);

※ 從主檔載入外部字型分享swf檔,嵌入外部引入的字型資訊

//registerFont()方法
//public static function registerFont(font:Class):void
//在全域字體清單中註冊字體類別,才能提供給其它檔案嵌入字型。
//參數↓
//font:Class — 您要新增至全域字體清單中的類別。


‧TextField 內的字元使用嵌入之字型 (點擊文字欄位中的文字,跳窗連結至所顯示的url)




(一)首先必須做一個元件庫中含有嵌入字型的swf檔。開一新檔名為 991115_shareFont.fla
如同  [AS3]嵌入字型 - TextField.embedFonts ( 一 ) 的說明,在元件庫新增字體


(二)按 [+] 新增字體後,到右邊選擇字型、字元範圍,填入名稱。






















(三)切換的[ActionScript]頁嵌,外框模式選[傳統],勾選[匯出給ActionScript使用、[匯出在第一影格],填入類別命名,此處為[ ShareFont ],這類別名稱在ActionScript中將被引用。*** 注意,由於這字型要分享給其它Flash檔案使用,必須在下方[共享區塊勾選[匯出給執行階段共享],並將URL欄位填入此FLA 將匯出的SWF檔名,此處為[ 991115_shareFont.swf ]。


(四}到主場景第一影格寫入以下AS,並將991115_shareFont.fla匯出為991115_shareFont.swf

//
var a = Font.enumerateFonts();
trace("991115_shareFont.swf裡的嵌入字型為 " + a[0].fontName);
//
//registerFont()方法  
//public static function registerFont(font:Class):void
//在全域字體清單中註冊字體類別
//參數↓
//font:Class — 您要新增至全域字體清單中的類別。
Font.registerFont(ShareFont);


(五)建立此例主檔 991115_TextField.embedFonts_3.fla
主場景第一影格AS3如下~

//
import flash.display.Loader;
import flash.net.URLRequest;
import flash.text.TextFormat;
import flash.text.TextField;
//
var ldr:Loader = new Loader();
//載入外部擁有字型分享資訊的swf檔
ldr.load(new URLRequest("991115_shareFont.swf"));
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, lcHandler);
//
function lcHandler(e:Event):void
{
//*******將字体清單的裝置字體設為false,所以字型清單陣列儘剩下嵌入字型
//所引入的嵌入字型只有一個
 var embededFonts:Array = Font.enumerateFonts(false);
 trace("991115_TextField.embedFonts_3.swf裡的嵌入字型為 " + embededFonts[0].fontName);
 //
 var aTextFormat:TextFormat = new TextFormat();
 //******所引入的嵌入字型只有一個,其名稱就是embededFonts[0].fontName
 aTextFormat.font = embededFonts[0].fontName;
 trace("aTextFormat.font = " + aTextFormat.font);
 aTextFormat.color = 0xffffff;
 aTextFormat.size = 24;
 aTextFormat.letterSpacing = -1;
 aTextFormat.leftMargin = 15;
 aTextFormat.rightMargin = 15
 aTextFormat.target = "_blank";
 aTextFormat.url = "http://help.adobe.com/zh_TW/Flash/CS5/AS3LR/flash/text/TextFormat.html#url";
 //
 var aTextField:TextField = new TextField();
 //*******在文字欄位實體設定embedFonts(嵌入字型)屬性為真
 aTextField.embedFonts = true;
 aTextField.multiline = true;
 aTextField.wordWrap = true;
 aTextField.background = true;
 aTextField.backgroundColor = 0xff2288;
 aTextField.x = 100;
 aTextField.y = 50;
 aTextField.width = 360;
 //aTextField.height = 200;
 aTextField.autoSize = TextFieldAutoSize.LEFT;
 aTextField.defaultTextFormat = aTextFormat;
 aTextField.text = "\n"+"http://help.adobe.com/zh_TW/Flash/CS5/AS3LR/flash/text/TextFormat.html#url"+"\n\n";
 //aTextField.setTextFormat(aTextFormat);
 addChild(aTextField);
}


適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__flash.text.Font.enumerateFonts()方法


適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__flash.text.Font.fontName屬性

適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__flash.text.Font.registerFont()方法