Pay for Hesitation: note

Pages

顯示具有 note 標籤的文章。 顯示所有文章
顯示具有 note 標籤的文章。 顯示所有文章

2011年4月13日 星期三

[BulkLoader] Failed to get content with url as type: [class Bitmap]

今天試玩Google image search API, 用bulkloader來載圖,
在flash builder上run的好好的,但放在網路就無法執行。
後來trace bulkloader的ImageItem.as source code



才發現bulkloader在發生cross domain security error時,並不會把err丟出來,反而是把載好圖的loader丟給上層。
回去翻文件,才發現其實文件有寫... XD


SecurityError

BulkLoader can handle SecurityErrorEvents. By listening to BulkLoader.SECURITY_ERROR("securityError"), client code can gracefully handle security error events. Listeners can be added to each LoadingItem subtype or to the entire BulkLoader object. Please note that if you add an event listener for a LoadingItem but not for BulkLoader, in case the event fires, it will bubble up and result in a unhandled security error. In order to make sure the error never goes unhandled, add a securityError handler to the BulkLoader instance.
This mechanism will catch events thrown on
.load operations. Event thrown when trying to access content (such as getBitmapData) is still left to client code to handle.
When using type=image or type=movieclip (which are loaded using a Loader instance), if, when the asset is loaded, accessing it's loader.content throws an error, then BulkLoader will gracefully recover and use a simple Loader instance. This way, you can still add a Loader object to the stage, but won't be able to run code inside of it (for MovieClip instances) or load it as data (transforming to BitmapData instances).
In this case, you can get the Loader instance either using getContent or getDisplayObjecLoader .


所以原本寫的
addChild( imageLoader.getBitmap( key ) );

改寫成
addChild( imageLoader.get( key ).content );

就不會有cross domain security 討人厭的問題了.

Ref:
http://groups.google.com/group/bulkloader-users/browse_thread/thread/31189ad472fb1d90

2010年12月2日 星期四

Dispatch Custom Values via DeluxeSignal

http://groups.google.com/group/as3-signals/msg/474cc2ec4e122be1?pli=1

Yes ,that works. 
Thank you Robert for a fast response. 
On Sep 17, 1:50 am, Robert Penner  wrote: 
> Try new DeluxeSignal(this, IEvent, String,int); 
> Sorry this isn't well-documented. 
> Robert 
> On Sep 16, 4:17 pm, iki_xx  wrote: 
> > How can I dispatch custom values via DeluxeSignal? 
> > Meaning: 
> > new DeluxeSignal = new DeluxeSignal(this,String,int); 
> > deluxeInstance.dispatch(new GenericEvent(),'"foo",999); 
> > it doesnt work , I get runtime errors : Value object <[object 
> > GenericEvent]> is not an instance of <[class String]>. 
> > how can I dispatch/catch those parameters ? 
> > Do I have to extend the GenericEvent class, and add those parameters 
> > to the subclass of the GenericEvent class? 
> > Thanks 

2010年11月15日 星期一

用Movie Clip 實作Button時, mouse event造成不斷閃爍的問題?

往往為了要讓button的mouse event 有更多層次的特效時,
artist 會選擇用movieclip的製作方式來取代simple button.

MovieClip往往有多層元件.
當滑鼠移至母元件與子元件差集的部分, 
常常會因為母元件的mouse over event與子元件的mouse out event 發生循環loop,
使得movie clip 產生閃爍, 解決的方法就是僅對母元件註冊mouse event,
並將母元件的mouseChildren 設為false.


2010年8月17日 星期二

XML variable-access syntax

* : children()
.Name : child("Name")

@Name : attribute("Name")
@* : attributes()

..Name : descendants("Name");
..@ : retrieve a list of descendant attributes
..* : retrieve every single nodes from a given element
..@* : retrieve every single attributes defined both on an element and on all of its descendants.
..*.@* : retrieve every single attributes defined on an element's descendants, but not on the elements itself.


p.s. parsing from right to left.

Iterate All Bodies in the Box2d World

for-each-in & for-in

for-in : iterates over key set.
for-each-in : iterates over value set.



Output:
Toronto
Canada

city
country

2010年7月20日 星期二

Notes about Auto Binding

flash authoring tool中的symbol, 可以利用export成 linked class的方式。
來跟外部的Linked.as檔做連結。
並且在編譯後, 可以利用該Linked Class來create instance。
我稱做auto binding.

auto binding這是一個方便的設計, 並且在多本書中被推薦使用的方法。
經過一番測試,大致可推測flash 在做此類auto binding的時間點。

1. 在flash authoring tool下編譯時,如果symbol的Linked Class可以在class path中找到,那麼會在編譯時便做好auto binding;因此如果Linked Class有控制symbol的程式邏輯,可以立即在編譯後產生的swf中看到效果。

2. 在flash authoring tool下編譯時,如果symbol的Linked Class無法在class path中找到,那麼會自動產生一個新的Linked Class,並且亦可用此Linked Class create instance.

3. 在flex builder下編譯時,auto binding會發在擁有該symbol的swf被 Loader載入完成的那一刻,所以在此時間點之前Linked Class create的instance中都不會有symbol的resource。(Linked.as 必須存在於flex builder 的class path)

4. 因為第3點,在flash authoring tool中編譯場景檔時,即使symbol的linked class不存在於flash authoring tool 的 class path亦不影響auto binding的成功與否。因為只要在flex builder 的編譯時期,AVM能夠正確地找到對應的 fully qualified class name 即會auto binding。

5. 之前用bulkloader在Controller.swf中去讀Scene.swf時,同第三點亦能正確的auto binding。

6. 但當我在SceneManager.swf 無論用Loader 或 BulkLoader去載入Controller.swf,Controller.swf中的bulkloader在載入完Scene.swf後,卻沒有auto binding!!非得用Loader先去載入Scene.swf一次,才能成功 auto binding。


load swf 發生的問題

本來的架構是, 有兩個Scene,
每個Scene是一個project.
ControllerA.swf 去loaded ViewA.swf
ControllerB.swf 去loaded ViewB.swf

Scene之間的切換是利用navigateToURL透過server端去跳轉.
後來需求做了改變, 希望改由client端來控制scene之間的切換,

於是我寫了SceneManager Project來做這件事,
但又希望儘可能不破壞原有兩個Scene Project的程式架構.

所以變成SceneManager 去 create & launch Controller的instance,
原本的UI控制, 遊戲流程的程式邏輯都無變更.

僅在於要做場景切換時,
Controller 同樣先做destroy的動作,
但會透過connection channel 通知SceneManager做跳轉的動作,
而非原本的navigateToURL.

但首先SceneManager在create & launch Controller的instance時,
原先可以work的場景檔會出一些問題,
如:MovieClip的currentFrameLabel屬性變成undefined,
SimpleButton註冊的事件無反應,
有些MovieClip的效果不見了,
甚至有的MovieClip消失在場景上了.

試了好多地方, 如改變Load swf的方法 ( Loader, BulkLoader),
initialize controller instance的時間點.

最後才發現問題是沒在SceneManager Project 加上-target-player=10.1.0的設定.
(ControllerA 和ControllerB本身兩個Project都有加上面這個參數)



2010年6月18日 星期五

Clean the flash cookie (local shared object)

http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager07.html

or go to the path below and delete it manually.

C:\Documents and Settings\[username]\Application Data\Macromedia\Flash Player\#SharedObjects\[random_number]\

2010年4月29日 星期四

Hand Cursor on TextField

如果你的Sprite (MovieClip亦同) 物件中有一個TextField,
那麼即使你將該Sprite的buttonMode 設為true,當滑鼠移到TextField上時,
滑鼠游標不會顯示Hand Cursor。
那是因為TextField會搶先成為mouseEvent的target。

解決方法有兩種:
1. 是將Sprite的mouseChildren設為false,這樣Sprite會阻止其Children成為mouseEvent的target。
2. 是將TextField的selectable屬性設為false,mouseEnable屬性設為false。

reference:


2010年4月19日 星期一

OO Design Principles


Identify the aspects of your application that vary and separate them from what stays the same.
Favor composition over inheritance.
Strive for loosely coupled designs between objects that interact.
Open-Closed Principle: Class should be open for extension, but closed for modification.
Dependency Inversion Principle: Depend upon abstractions. Do not depend upon concrete classes.
No variables should hold a reference to a concrete class.
No class should derive from a concrete class.
No method should override an implemented method of any of its base class.
Principle of Least Knowledge - talk only to your immediate friends. (It means when you are designing a system, for any object, be care of the number of classes it interacts with and also how it comes to interact with those classes.)

The Hollywood Principle: Don't call us, we'll call you.

A class should have only one reason to change.

Null Object: A null object is useful when you don't have a meaningful object to return, and yet you want to remove the responsibility for handling null from the client. (p.214, NoComand and p.372 Null Iterator)

2010年4月12日 星期一

AS3 interface notes

1. All methods declared in an interface must not include an access-control modifier.

2. Variable definitions are not allowed. (However, interfaces can include get and set methods, which can be used to simulate variables.

3. A class implements two interfaces that define a method by the same name and with the exact the same name, no error occur(dangerous). The real question is whether the class can provide the services provided by both interfaces within a single method definition. In most cases, the answer is no.

4. Some developers also preface interface names with an "I" as in IMovable, IKillable.

5. An Interface can use the extends keyword to inherit from anther interface.

6. ActionScript interfaces also support multiple interface inheritance.

7. Marker interface ( empty interface, no behavior)

8. In a Java application, any class that is expected to be subclassed should be an implement of an interface. As such, it can be subclassed directly, or it can be used via composition by a class that inherits from another class.

2010年3月6日 星期六

No function overloading in AS3

It sucks !!!!!!!!!!!!!!!!!!!!!!!!!

2010年3月5日 星期五

Scope summary


package{

 //Global scope
 
 public class A {

  //Class scope

  public static function b():void
  {
    //Static method scope
  }

  public function c():void
  {
    //Class method scope

    function d():void{
      // Nested method scope
    }
  }

 }

}

// Global scope


注意:global scope有兩處


add at 2010/4/8
Conditionals and loops do not have their own scope in ActionScript3.
(p.335 in Chapter 17: Namespaces)

That's why the complier always issues a warning message "Duplicate variable definition" when I put following codes in a function.

for(var i:int = 1; i < 10; i++)
 doSomething();
}

for(var i:int = 1; i < 10; i++)
 doSomething();
}

this thread also talked about same issue.

Codes in Class Initializer


public class A{

 //codes here.

 public function A(){ }

}

When ActionScript defines a class at runtime, it automatically creates a method named the class initializer and executes that method. In this class initializer, ActionScript places all the class's static variable initializers and all class-level code that is not a variable definition or a method definition.

Code in the class initializer runs in interpreted mode, and is not compiled by the JIT compiler. Because JIT-compiler code generally executes much more quickly than interpreted code, you should consider moving processor-intensive code out of the class initializer when performance is a priority.

2010年3月4日 星期四

Notes for the Focus Events

1. focus event 即keyboard focus event.

2. in AS3, 同一時間只有一個object能收到focus event.

3. 語法: object.stage.focus = focusObject;

4. automatic tab order: from left to right, from bottom up
custom tab order: 透過設置 .tabIndex tab event觸發時, 會依tabIndex由小至大focus

5. .visible set to false 會自動排除被tab event觸發

6. .tabChildren set to false, 則container下的child 都會沒有tabIndex.

7. flash player預設, 被tab focus的物件外圍都會有黃色框, 可以將.focusRect set to false 來取消.
stage則需將.stageFocusRect set to false.

8. 利用下面三種方法, 可以讓物件在觸發mouse event後, 得到focus
(1) .buttonMode set to true.
(2) .tabEnabled set to true.
(3) .tabIndex set to a nonnegative integer

p.s. 若該物件是container, 則其下所有的子物件都會得到focus.


9. 反之, 將 .mouseEnabled set to false
.mouseChildren set to false 使容器下的物件都不會因為mouse event而得到focus

10. 同上, 將 .tabEnabled set to false
.tabChildren set to false
除TextField object例外, 是flash player 的bug

Notes for the Mouse Event

1. preventDefault()
有些元件有預設的behavior, 如simpleButton, 就有預設對應MouseEvent.OVER 的behavior, preventDefault可以關掉這種預設行為.

2. In AS3 , 沒 right click event.

3.當有多個interactiveObject overlap時, Flash player只會trigger 視覺上最上層那個物件的mouse event.

4.承上, 所以一個sprite 上若有textField, 那麼滑鼠點到textField時, sprite會收不到event. 解決的辦法是textField.mouseEnable = false;

5.取得mouse座標有兩種方法
.localX 與.localY, the position which relative to the event target's top left corner
.stageX 與.stageY, the position in the Stage instance.

6.Mouse.hide() //hide the system mouse pointer
Mouse.show() //show the system mouse pointer

7.若要設置flash player空白處的Mouse Event, 就必須在Stage instance註冊listener, 但是某些security的原因, 可能會被阻擋存取stage, 解決方法請參essential as3 的Ch12 的Handling Events Across Security Boundaries.

2010年3月3日 星期三

root and stage

var rec1:Shape = new Shape();
var rec2:Shape = new Shape();
var rec3:Shape = new Shape();

var spriteOnDisplayList:Sprite = new Sprite();
var spriteNotOnDisplayList:Sprite = new Sprite();
var spriteOnMainClass:Sprite = new Sprite();

spriteOnDisplayList.addChild(rec1);
spriteNotOnDisplayList.addChild(rec2);
spriteOnMainClass.addChild(rec3);
stage.addChild(spriteOnDisplayList);
//stage.addChild(spriteNotOnDisplayList);
this.addChild(spriteOnMainClass);

trace(rec1.root); //[object Stage]
trace(rec2.root); //null
trace(rec3.root); //[object MainTimeline]



一個display物件在它的constructor中, 其parent, root, stage等instance variable都是null值.
因為它尚未加入display list.

Notes of Flash Authoring Tool

下面的flash 為 flash authoring tool的簡稱.

1. library中的symbol只有被用到的, 才會compile進.swf

2. 僅管用一個symbol在 stage上拉出多個instance, compiler 只會load一個symbol content, 直到runtime 才會動態duplicate.

3. An instance variable created in a frame script is not initialized until that frame script executes.
(這就是為什麼document class一開始只能自動bind到1st frame中的instance, 其它frame中的instance都無法自動binding)

4. 同上. Code in a .swf file’s document class constructor method can access all manually placed child assets on the .swf file’s first frame but not on the second or subsequent frames (because assets on a subsequent frame are not added as display children until the playhead reaches that
frame).

4. document class -> main timeline class (flash 中可以針對每個keyframe 寫script code, 但是其實在compile時, 會被編成一個document class. 此外, document class有自己的timeline(main)及stage.
5. linked class -> movie clip symbols, 每個movie clip symbol都有自己的timeline及stage

2008年4月2日 星期三

多行註解開關

真是神奇, ActionScript支援和MATLAB類似的多行註解開關的方式, 可以快速打開或關閉指定區塊的程式碼! 原本多行註解這樣的:
如果我們把"/*"和"*/"都單獨成為一行, 所達到的效果是和上面一樣的, 不過要先將註解字元寫成這種形式, 才能使用註解開關. 接著就是開關的部份了, 以下你可以看到, 在"/*"之前我們再加一個"/"變成"//*", 該區塊就關閉註解可以執行了, 去掉那一個"/"則是原本的多行註解 (注意"*/"得改為"//*/").