What have you found for these years?

2009-06-29

avatarblock imp 範例

日前加上了 example.as, 不過這個僅用了內建的 BlockImpRandom
這邊實作了一個 callback chain, 在 callback 裡 callback 自己。
我在 flash 裡滿常這樣做的,沒記錯的話 avatarblock 本身也常用。
可惜 AS3 看起來是不支援在 closure 裡面呼叫 super method,
也沒辦法把 super method bind 在某個 variable 裡:
var start = super.work 這樣是不行的,還是回到自己...
   1  
2 package com.roodo.agogo{
3 import org.godfat.avatarblock.imp.BlockImpRandom;
4 import org.godfat.avatarblock.Master;
5 import org.godfat.avatarblock.Avatar;
6 import org.godfat.avatarblock.Block;
7 import org.godfat.avatarblock.Util;
8 import flash.utils.setTimeout;
9 import flash.utils.setInterval;
10 import flash.utils.clearInterval;
11
12 public class BlockImpAgogo extends BlockImpRandom{
13 static public function make_imp(block: Block): BlockImpRandom{
14 return new BlockImpAgogo(block);
15 }
16 function BlockImpAgogo(block: Block){ super(block); }
17
18 public override function work(){
19 if(start_){ super.work(); return; }
20
21 var x: int = 0, y: int = 0;
22
23 var callback: Function = function(){
24 ++y;
25 if( y == block.avatars.height ){
26 y = 0; ++x;
27 }
28 if( x < block.avatars.width ){
29 fast_fade_in(block.avatars.get(x, y), callback);
30 }
31 else{
32 start_ = true; // cannot access super.work in closure...
33 work(); // so here's the hack, call super.work in this.work
34 }
35 }
36
37 fast_fade_in(block.avatars.get(0, 0), callback);
38 }
39
40 public function fast_fade_in(avatar: Avatar, callback){
41 var step: int = 0;
42 const pid: int = setInterval(function(){
43 avatar.body.alpha = step++ * 1 / 10;
44 if(avatar.body.alpha >= 1){
45 clearInterval(pid);
46 if(callback != null) callback();
47 }
48 }, 1000/30);
49 }
50
51 private var start_: Boolean = false;
52 }
53
54 }

0 retries:

Post a Comment

All texts are licensed under CC Attribution 3.0