用AsWing开发了一个饭否桌面应用-Focuswing

September 9th, 2007

下载swf文件:focuswing_1.0.rar
下载exe文件:focuswing_1.0.exe

采用as3,mvc模式开发。刚开始不习惯这种模式,用多了突然会觉得很顺手。
饭否:应该就不用介绍了吧
AsWing:aswing是一个开源的flash GUI框架,布局和架构思路类似javaswing。之前的A2版本由于只支持mtasc编译方式,所以在配置和使用上都略显复杂。不过目前的A3版本既可以支持Flex开发,也支持Flash编译方式开发,配置和使用几乎没有门槛。
更值得说一下的是,这个框架是由国人自己开发的,在国内这样的互联网环境下能有这样始终保持生命力的开源项目诞生和存在,还是满有意义的。
一直认为flex的组件以及mxml的编写方式局限性很大。虽然AsWing与flex的组件不具有可比性。不过AsWing显然提供了一种更为纯净的方式来使用as3。

关于as3事件的笔记

August 17th, 2007

as2时代直接在dispatchEvent的时候跟上想跟的参数就可以了,as3增加了一个Event的继承类来承担其中的信息传递,不过看书看到关键时刻居然就没有了。于是笔记一下类实例怎么向外发送事件和传递参数,来点实用的。
发送事件的Guest,在Guest内发送”do_b”的Event:

  1. var e:GuestEvent = new GuestEvent(GuestEvent.DO_B);
  2. e.args = {author:"hirokimo",time:"21:10"};
  3. this.dispatchEvent(e);

这时候为了传递参数,使用一个继承Event的自定义事件类GuestEvent:

  1. package
  2. {
  3.     import flash.events.Event;   
  4.     public class GuestEvent extends Event
  5.     {
  6.         public static const DO_A:String = "do_a";
  7.         public static const DO_B:String = "do_b";
  8.         public static const DO_C:String = "do_c";      
  9.         public var args:*;   
  10.         public function GuestEvent(_type:String)
  11.         {
  12.             super(_type);
  13.         }
  14.     }
  15. }

最后,在类外面添加侦听和接收的地方:

  1. package
  2. {
  3.     public class EventTest
  4.     {
  5.         public function EventTest()
  6.         {
  7.             var guest:Guest = new Guest();
  8.             guest.addEventListener(GuestEvent.DO_B,handleB);
  9.         }
  10.         private function handleB(e:GuestEvent):void
  11.         {
  12.             trace("author:"+e.args.author
  13.             +" time:"+e.args.time);
  14.             //author:hirokimo time:21:10
  15.         }
  16.     }
  17. }

大功告成,之后就可以顺利的发送事件参数了。
不过有一点值得注意:Guest类里有时需要在初始化后直接dispatch一个Event出来,这时候因为外部addEventListener动作还没有完成,listener是收不到事件的。
这个问题很容易被忽略,如果一定要这么做,可以:
1.Guest初始化后timer几百毫秒再dispatch 或者
2.外面addEventListener完之后,主动调guest.xxx来发送。

as3 读书笔记

August 5th, 2007

仅仅是笔记 没有讨论问题 大家请略过

1.用正则来replace字符串

  1. var str:String = "above thE cloud,around The shadow.";
  2. var pattern:RegExp = /the/ig;
  3. trace(str.replace(pattern, "my"));
  4. //above my cloud,around my shadow.

2.数组深度copy

  1. private function clone(source:Object):*
  2. { 
  3.     var myBA:ByteArray = new ByteArray();
  4.     myBA.writeObject(source);
  5.     myBA.position = 0;
  6.     return(myBA.readObject());
  7. }

3.遍历Object,这个和as3没什么关系,不过经常用到。

  1. private function traceObject(obj:Object, indent:uint = 0):void
  2. {
  3.     var indentString:String = "";
  4.     var i:uint;
  5.     var prop:String;
  6.     var val:*;
  7.     for (i = 0; i < indent; i++)
  8.     {
  9.         indentString += "\t";
  10.     }
  11.     for (prop in obj)
  12.     {
  13.         val = obj[prop];
  14.         if (typeof(val) == "object") 
  15.         {
  16.             trace(indentString + " " + prop + ": [Object]");
  17.             traceObject(val, indent + 1);
  18.         }
  19.         else
  20.         {
  21.             trace(indentString + " " + prop + ": " + val);
  22.         }
  23.     }
  24. }

开始用flex builder来as3

July 29th, 2007

一直在找借口,要等到fdt3出来后才开始学as3,没想到fdt3的alpha就已经出来了-_-,下载了后总的来说,感觉已经失去了as2时代的优势了,抛开没有好的一个trace机制来说,flex builder的功能也完全可以取代之了。
不过还是会一直用它来写as2代码,卡卡。
关于fb3beta,试用感觉还不错,把快捷debug设置设成和flash里一样,然后在调试台里看信息。其实和写java的感觉差不多,怪不得java程序员那么容易上手as3-_-。
关于怎么设置的和flash里一样手感,可以参照eidiot同学的文章。
here:使用Flex Builder开发AS3程序
然后,总算可以开始了,虽然总比别人慢一拍。
至于flex组件和mxml编码方式,暂时不打算涉及,这种编码方式实在是满奇怪的。

饭否 FanFlash beta 1.3 发布

July 1st, 2007

下载地址

或者有需要swf文件直接用的朋友:fanflash-13-pro.rar(35k)
[由于flash安全问题在本地无法工作,需要传到网上.]

主要改进:
1.之前一直有朋友提议记住密码,可能我误会了大家的意思,现在不用每次输密码了-_-,当然重新开以后还是要输入一次得。
2.之前输入id的流程不太合理,感觉很麻烦,现在可以在客户端记住id,发信息使用当前id。当然,增加了更改id的选项,满足马甲狂人的需要。

3.重大改进,发信息时可以选择@好友。因为平时给好友@信息比较麻烦,尤其是昵称有中文和奇怪字符的好友。

4.修正了些bug…最后还是要谢谢大家留的建议.

flash做的饭否桌面工具 FanFlash beta 1.2

June 17th, 2007

下载地址:fanflash-beta-1.2.exe

周末忙里偷闲做了这个东西出来,主要就是觉得饭否在同类站点里很有亲和力,可以明显看到团队的力量。而且比twitter稳定,希望他们越发展越好。主要功能:
1.发信息
2.看我的文章
3.看我和好友的文章(可刷新)
4.看好友列表
5.随便看看(可刷新)
其实就是api开发出来的几条功能,呵呵 -_-

b1.gif

b2.gif

mine:hirokimo
相当热爱互联网的同事:guke

解决 setInterval 和 clearInterval 脱僵状态的类

May 26th, 2007

刚开始使用 setInterval 和 clearInterval 的时候,总是会被脱僵状态困扰。就是说,明明有时候将 intervalID给清理掉了,原来定义的间隔还是不停的执行着,具体的内容可以看aw同学的分析,讲的非常清晰了。于是为了避免出现未预期的程序问题,通常只好使用onEnterFrame来替代。
不过在单纯继承于Object,仅仅处理逻辑的类中,没有MovieClip来充当onEnterFrame的载体,为了创建interval还是要用到 setInterval 方法才行(除非你传一个MovieClip对象进来当holder)。于是参照分析中interval进出栈的做法,封装成一个Interval 类,使用起来就万无一失了。

  1. class Interval {
  2.     private var intervals : Array;
  3.     private var interval : Number;
  4.     public function Interval(obj : Object, func : String, time : Number) {
  5.         intervals = new Array();
  6.         interval = setInterval(obj, func, time);
  7.         intervals.push(interval);
  8.     }
  9.     public function clearIntervals() : Void {
  10.         for (var i in intervals) {
  11.             clearInterval(intervals[i]);
  12.             intervals.pop();
  13.         }
  14.         clearInterval(interval);
  15.     }
  16.     public function toString() : String{
  17.         return "[object interval]";
  18.     }
  19. }

调用:var x:Interval = new Interval(owner, “doSth”,500);
清除:x.clearIntervals();

flash的AStar寻路类 应该对研究tile的同学有帮助

May 3rd, 2006

具体用法需要研究的同学看三个public入口就知道了。

  1. /**
  2. * @author hirokimo
  3. * @data 2006-05-03
  4. * @usage A*path searching
  5. */
  6. class Astar {
  7.     private static var cost:Number = 10;
  8.     private static var isSearching:Boolean = false;
  9.     private static var broadcastInit = AsBroadcaster.initialize(Astar.prototype);
  10.     public var addListener:Function;
  11.     public var removeListener:Function;
  12.     private var broadcastMessage:Function;
  13.     private var path:Array;
  14.     private var openList:Array;
  15.     private var closeList:Array;
  16.     private var map:Array;
  17.     private var pointType:Array;
  18.     private var directData:Array;
  19.     //
  20.     public function Astar() {
  21.         path = new Array();
  22.         pointType = new Array();
  23.         openList = new Array();
  24.         closeList = new Array();
  25.         directData = new Array();
  26.         map = new Array();
  27.         initDirectData();
  28.     }
  29.     public function setMap(map_t:Array) {
  30.         map = map_t;
  31.         setPointType(map);
  32.     }
  33.     public function findPath(startX:Number, startY:Number, endX:Number, endY:Number) {
  34.         initData();
  35.         isSearching = true;
  36.         var startPoint:Object = new Object();
  37.         var endPoint:Object = new Object();
  38.         var prePoint:Object = new Object();
  39.         startPoint.X = startX;
  40.         startPoint.Y = startY;
  41.         endPoint.X = endX;
  42.         endPoint.Y = endY;
  43.         startPoint.parent = null;
  44.         startPoint.G = 0;
  45.         startPoint.H = manhattanDis(startPoint, endPoint);
  46.         startPoint.F = startPoint.G+startPoint.H;
  47.         prePoint = startPoint;
  48.         //send2List(startPoint, openList);
  49.         send2List(startPoint, closeList);
  50.         while (isSearching == true) {
  51.             for (var i = 0; i<directData.length; i++) {
  52.                 var tempPoint:Object = new Object();
  53.                 tempPoint.X = prePoint.X+directData[i][0];
  54.                 tempPoint.Y = prePoint.Y+directData[i][1];
  55.                 if (isOut(tempPoint) == true) {
  56.                 } else {
  57.                     if (tempPoint.Y == endPoint.Y && tempPoint.X == endPoint.X) {
  58.                         //found
  59.                         isSearching = false;
  60.                         send2List(tempPoint, closeList);
  61.                         tempPoint.parent = prePoint;
  62.                         tempPoint.G = prePoint.G+directData[i][2];
  63.                         tempPoint.H = manhattanDis(tempPoint, endPoint);
  64.                         tempPoint.F = tempPoint.G+tempPoint.H;
  65.                         //trace(closeList[closeList.length-1].X+" "+closeList[closeList.length-1].Y);
  66.                         //trace("path found!");
  67.                         drawPath();
  68.                         broadcastMessage("onPathFound");
  69.                         return;
  70.                     }
  71.                     if (getPointType(tempPoint) == "road" && isInList(tempPoint, closeList) == false) {
  72.                         //trace("四向之"+i+" tempPoint:"+tempPoint.X+" "+tempPoint.Y);
  73.                         //目标格不在关闭列表且是road
  74.                         if (isInList(tempPoint, openList) == false) {
  75.                             //目标格尚未开启
  76.                             send2List(tempPoint, openList);
  77.                             //trace("ol:"+openList[0].X+"=="+openList[0].Y);
  78.                             tempPoint.parent = prePoint;
  79.                             tempPoint.G = prePoint.G+directData[i][2];
  80.                             tempPoint.H = manhattanDis(tempPoint, endPoint);
  81.                             tempPoint.F = tempPoint.G+tempPoint.H;
  82.                         } else {
  83.                             //已经在开启列表中
  84.                             var tempG:Number = prePoint.G+directData[i][2];
  85.                             for (var j = 0; j<openList.length; j++) {
  86.                                 if (tempPoint.X == openList[j].X && tempPoint.Y == openList[j].Y) {
  87.                                     if (tempG<openList[j].G) {
  88.                                         //现在的路径G更好
  89.                                         openList[j].G = tempG;
  90.                                         openList[j].F = openList[j].G+openList[j].H;
  91.                                         openList[j].parent = prePoint;
  92.                                     }
  93.                                     break;
  94.                                 }
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.             if (openList.length == 0) {
  101.                 isSearching = false;
  102.                 broadcastMessage("onPathBlock");
  103.                 return;
  104.             }
  105.             sortList(openList);
  106.             prePoint = openList.shift();
  107.             send2List(prePoint, closeList);
  108.             //trace(closeList[closeList.length-1].X+" "+closeList[closeList.length-1].Y);
  109.         }
  110.     }
  111.     public function getPath():Array {
  112.         path.reverse();
  113.         trace("==========Path start===========");
  114.         for (var i = 0; i<path.length; i++) {
  115.             trace("path:"+path[i].X+path[i].Y);
  116.         }
  117.         trace("==========Path  end ===========");
  118.         return path;
  119.     }
  120.     private function initData():Void {
  121.         openList = [];
  122.         closeList = [];
  123.         path = [];
  124.     }
  125.     private function drawPath() {
  126.         //trace("set path");
  127.         closeList.reverse();
  128.         var currentPathPoint:Object = new Object();
  129.         currentPathPoint = closeList[0];
  130.         send2List(currentPathPoint, path);
  131.         //trace(currentPathPoint.X+" "+currentPathPoint.Y);
  132.         for (var i = 1; i<closeList.length; i++) {
  133.             if (currentPathPoint.parent == null) {
  134.                 break;
  135.             }
  136.             //trace(currentPathPoint.parent.X+" "+currentPathPoint.parent.Y);                                                           
  137.             send2List(currentPathPoint.parent, path);
  138.             currentPathPoint = currentPathPoint.parent;
  139.         }
  140.     }
  141.     private function getPointType(point_t:Object):String {
  142.         return pointType[point_t.X][point_t.Y];
  143.     }
  144.     private function setPointType(map_t:Array) {
  145.         for (var i = 0; i<map_t.length; i++) {
  146.             pointType[i] = new Array();
  147.             for (var j = 0; j<map_t[0].length; j++) {
  148.                 if (map_t[i][j] == 1) {
  149.                     pointType[i].push("wall");
  150.                 } else if (map_t[i][j] == 0) {
  151.                     pointType[i].push("road");
  152.                 }
  153.             }
  154.         }
  155.         //trace("第一行:"+pointType[0]);
  156.     }
  157.     private function initDirectData():Void {
  158.         //下,右,上,左
  159.         directData[0] = [1, 0, 10];
  160.         directData[1] = [0, 1, 10];
  161.         directData[2] = [-1, 0, 10];
  162.         directData[3] = [0, -1, 10];
  163.     }
  164.     private function sortList(list_t:Array):Void {
  165.         list_t.sortOn("F", Array.NUMERIC);
  166.     }
  167.     private function isOut(point_t:Object):Boolean {
  168.         if (point_t.X<0 || point_t.Y<0 || point_t.X>16 || point_t.Y>16) {
  169.             return true;
  170.         } else {
  171.             return false;
  172.         }
  173.     }
  174.     private function isInList(point_t:Object, list_t:Array):Boolean {
  175.         if (list_t.length == 0) {
  176.             return false;
  177.         }
  178.         for (var i = 0; i<list_t.length; ) {
  179.             if (list_t[i].X == point_t.X && list_t[i].Y == point_t.Y) {
  180.                 return true;
  181.                 break;
  182.             }
  183.             i++;
  184.             if (i == list_t.length) {
  185.                 return false;
  186.             }
  187.         }
  188.     }
  189.     private function manhattanDis(start_t:Object, end_t:Object):Number {
  190.         var dis:Number;
  191.         dis = Math.abs(start_t.X-end_t.X)+Math.abs(start_t.Y-end_t.Y);
  192.         return dis*cost;
  193.     }
  194.     private function send2List(point_t:Object, list_t:Array):Void {
  195.         list_t.push(point_t);
  196.     }
  197. }