由於Flash發佈的SWF文件能包含使用者要是使用得同一類型不同屬性的文字或圖片格式,所以儅我要使用Flex加載大量圖片或文字的時候我會選擇使用加載SWF文件中的組件,而不去保存一個圖片文件夾來進行加載的使用,譬如我現在要進行的是棋牌類遊戲的開發,一幅牌的只數最少也有54只,如果此時的你使用的是.png格式的圖片進行加載,那麽你的52張牌的圖片容量將接近有1M的效果,但如果你加載的是SWF文件,那麽它的規格將只有100~300K不到,而且能保持畫質的清晰。雖説都是可以使用循環可以使之添加,但程序容量的大小也是我們開發者要注意的問題之一。
要使用SWF文件進行擺放和加載的要注意(舉例撲克):54張撲克牌(大小王),每一只牌就需要在其庫中點選:連接,勾選為ActionScript導出,多只的牌要統一開始的類名稱,例如: poker101~poker113那指定的就是葵扇花式牌的1 - 13 的對應;還有要注意的是,要使用Flash9以上版本才能使用(低版本未試過,呵呵)
代碼:(如何生成一個指定牌列)Poker Class
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
package
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
import flash.display.MovieClip; // 因爲在SWF中的保存類型是屬於MovieClip所以要導入MovieClip類
4
import flash.events.Event;
5
import flash.utils.getDefinitionByName; // 返回 name 参数指定的类的类对象引用
6
import mx.core.UIComponent;
7
public class poker extends UIComponent
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
9
public var clickState:Boolean
10
public var Pid:int; // 牌的ID
11
public var Pdot:int ; //牌類型
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public function poker(i:int,p:int)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
super()
16
clickState = false;
17
Pid = i
18
Pdot=p
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public function loadImage():void
{
21
var s:String = new String()
22
s = "poker"+Pdot
23
var cls:Class = getDefinitionByName(s) as Class
24
var mc:MovieClip = new cls();
25
mc.doubleClickEnabled = false
26
addChild(mc)
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
//加載代碼:
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
import mx.core.UIComponent;
32
//private var Ppoker:poker = new poker()
33
private var uc:UIComponent = new UIComponent();
34
private var loaderPoker:Loader = new Loader();
35
public var i:int=0;
36
public var p:int=105
37
public var array:Array =new Array(101,104,203,303,205,208,309,311,211,201,301,102,109,111,312,113,209); //測試用牌型
38
private var PokerArray:Array = new Array(); //存放牌對象的數組
39
public var Px:int = 250; //牌的X坐標
40
public var Py:int = 400; //牌的Y坐標
41
private var pokerLoader:Loader = new Loader();
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private function initApp():void
{
43
pokerLoader.contentLoaderInfo.addEventListener(Event.INIT,loadPoker);
44
laodswf();
45
this.addChild(uc)
46
}
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private function loadPoker(id:int,Pdot:int):void
{ //加載牌
49
PokerArray[id] = new poker(id,Pdot);
50
PokerArray[id].loadImage();
51
PokerArray[id].x = Px+id*40;
52
PokerArray[id].y = Py;
53
PokerArray[id].clickState = false;
54
//PokerArray[id].scaleX =
55
//PokerArray[id].scaleY =
56
uc.addChild(PokerArray[id])
57
PokerArray[id].addEventListener("click",PokerArray[id].setState) //監聽牌的點擊狀態
58
//PokerArray[id].setState(event);
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private function laodswf():void
{ //加載SWF
61
loaderPoker.contentLoaderInfo.addEventListener(Event.INIT,gotoGame);
62
loaderPoker.load(new URLRequest('poker.swf'),new LoaderContext(false,ApplicationDomain.currentDomain));
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private function gotoGame(evt:Event):void
{ // 循环添加
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i:int; i<array.length;i++)
{
67
var j:int;
68
j = array[i]
69
loadPoker(i,j)
70
}
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
74![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
有不足之処請指出。。。。。謝謝