Posts Tagged actionscript

[Flash] Loader set init Width / Height 的問題

問題很簡單,就是當自己要 load image, swf 的時候,不能set loader 的 width 和 height. (set 了之後就不會 render image).

Chowky 在自己試過有過這種問題。

var loader:Loader = loader.load(new URLRequest("images/testimg.jpg");

loader.addEventListener(Event.COMPLETE,completeHandler());

private function completeHandler():void
{
//set 了之後就render 不到image.
loader.width = 100;
loader.height = 100;
}

之後chowky 就找了很久,都不太明白問題的原因。
坊間有太多不同的解決方法,不過看完就明白到問題原因。

簡單來說,因為 loader 未load完 (chowky 估計是Event.COMPLETE 的時間都未load 完成)

[更新一下,雖然loader 已經load 完,不過content 未完成 (這個logic 雖然不太通),就是image 未ready。所以我們應該listen _loader.contentLoaderInfo 而不是_loader 本身]

chowky 見過會利用scale 的方法去做。(算吧了)

var _loader:Loader=new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
_loader.load(new URLRequest("images/myphoto.jpg"));
this.addChild(_loader);

function loadCompleteHandler(evt:Event) {
	evt.target.content.height=100;
	evt.target.content.width=100;
        trace("Finish resize");
}

不過 chowky 的解決方法是自己寫一個class extends sprite ,再update sprite 的size. chowky 覺得這種做法比較適合,custom loader class (oh….又花了一整天)….

真辛苦呢。

Share

, , , , ,

2 Comments

[Flash] Fullscreen Mode

自從flash player 9.0.28 之後,flash 就支援fullscreen~,就已經不用以前的額外javascript。

只要在flash IDE 的publish 加上allowfullscreen,IDE 就會自動加上這個attribute

當然你亦可以在swfobject 自己加上

<param name="allowFullScreen" value="true" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
//Don't scale the movie when the stage size changes
Stage.scaleMode="noScale";
 
//Align the stage to the top left
Stage.align = "TL";
 
//Function to toggle between fullscreen and normal size
//the toggle fullscreen button calls this function when pressed
function toggleFullScreen(){
  //if normal size, go to fullscreen, else go to normal size
  if(Stage["displayState"]=="normal"){
    Stage["displayState"]="fullScreen";
  }else{
    Stage["displayState"]="normal";
  }
}
//Create a listener for each time the Stage is resized
var resizeListener:Object = new Object();
//Called each time the stage is resized
resizeListener.onResize = function () {
  //Move the button to the center of the screen
  toggleFullScreenButton._x=Stage.width/2;
  toggleFullScreenButton._y=Stage.height/2;
}
//Add the listener to Stage
Stage.addListener(resizeListener);

Source: http://www.bezzmedia.com/swfspot/tutorials/flash8/True_Fullscreen_Flash_Mode

Share

, ,

No Comments

[Flex] 利用 actionscript 去 load 其他的 swf

Flash 是允許 developer load 外部的 SWF files (當然也可以是 images,JPG, PNG, GIF) 這種做法其實是有好處的。

  1. 不同的swf file 可以跟次序地播。而又不用reload browser or pause the pages.
  2. 相比於一個很大的swf,能夠有效地分開一個個細小的swf file,一來可以load 得快,memory 的處理亦會比較好一點。
  3. 一個複雜的user interface 當然不可能單靠一個的swf file。(尤其download了不同componet的developer。)這個程況就可以靠load其他external 的swf 去解決,一邊可以容易一點manage,修改一個不會影響整個flash的控制。現在很多devleoper都支持component base 的design。而且可以加容許不同的developer一齊去做各自的ui部份。

如果要 load external 的 swf 有以下三種方法

* Using the ActionScript 2.0 loadMovie command
* Using the ActionScript 2.0 MovieClipLoader class
* Using the ActionScript 3.0 Loader class

chowky 對actionscript 2.0 無興趣,所以只看了3.o,有興趣可以自己看看原文

Loader 在 actionscript 3.0 是subclass DisplayObject

用法很簡單,只要利用load這個method就可以了。

var myLoader:Loader = new Loader();
addChild(myLoader);
var url:URLRequest = new URLRequest("myExternalMovie.swf");
myLoader.load(url);

url 可以是absolute path 或者是relative path。

但還有很多要注意的地方 Read the rest of this entry »

Share

, , , , ,

2 Comments

[Actionscript] Add Leading Zero

以前只是很簡單的pad 1個0在前面,沒有想過寫一個utility class。

所以,既然有人教,當然要記住la

1
2
3
4
5
6
7
8
function addZero(n:int, numZeros:int = 1):String {
var str:String = n + “”;
while (str.length<numZeros+1)
{
str = “0+ str;
}
return str;
}

Source: http://shang-liang.com/blog/algorithm-for-adding-leading-zeros/

Share

No Comments