[面试最爱问的问题1]Flex/AS3的事件机制

July 19th, 2010 momoko8443 No comments

今天静下心来看了几篇as3事件机制的教程,终于整明白这个面试中最爱问的问题了,嘎嘎!

具体场景描述如下

容器深度:Application->Canvas->HBox->Button;

其中在HBox这个容器中有段代码

button.addEventListener(MouseEvent.Click,onButtonClickHandler);

阶段0:触发阶段,事件被触发,场景中的情况就是用户点击了Button,翻译为代码就是Button中执行了“ dispatch(new MouseEvent(MouseEvent.Click));”大家可以看下Event类中有个target的属性,这个属性就记录着Button这一事件发起者的对象

阶段1:捕获阶段,一旦有事件被触发,FP则会带着这个Event对象从整个程序的最上层开始往下遍历 ,顺序为Application->Canvas->HBox->Button,不停地匹配Event里面中的Target对象和当前遍历到的容器或是组件是不是为同一个对象

阶段2:目标阶段,终于在HBox这个容器中发现到了Button和Event中Target所指向的对象是同一个,ok,事件触发者Button终于被我挨家挨户的排查中找着了。

阶段3:冒泡阶段,从捕获到的目标Button,开始自下而上得往一级级parent走,看看到底是谁会关心Button被用户点击了。

于是再一级级往上HBox->Canvas->Application;果然在遍历到HBox的时候发现它曾经为Button的Click事件加过监听。

阶段4:执行阶段,事件的关心者HBox也找到了,那么执行onButtonClickHandler这个函数即可。

Categories: development Tags:

【转】Create Professional Flex Component

July 14th, 2010 momoko8443 No comments

Flex Builder has a great way to organize its components in tree mode, which is very a good way to organize things and make things clear to any user who are coming to Flex world.

By default, every component you create that is not part of default Flex components you will have placed in the [Custom] directory of Flex Components view in your Flex/Flash Builder, and no matter what properties you add to them, they will never be visible in the Flex Properties standard view.

But what if you want to customize that and create a component that have them all like flex components do? Well, it’s not that hard so let’s do that.

Create the Component

First, you have to create a new Flex Library Project. Do this by going to File->New and choose Flex Library Project. Give it a name, a location, choose whatever Flex SDK you wish to build this for and then click finish.

Now you have a blank library project in which you can create whatever components you want.
Is important to use packages correctly and namespaces and not just drop the component in the [src] folder (you’ll see later why). First create a package (eg. com.adm.component) in which you can add your custom component.

package com.adm.component
{
      import mx.containers.Canvas;

      public class mycomponent extends Canvas
      {
      }
}

Okay, now let’s create some methods to have something going. We’ll make this component be a big button with a method to enable the button and one to change it’s caption. We’ll use setters and getter’s for those properties because some other actions might be required when changing them. So:

package com.adm.component
{
     import mx.containers.Canvas;
     import mx.controls.Button;

      public class mycomponent extends Canvas
      {
            private var _title : String = 'Title';
            private var _active : Boolean = true;

            private var btn : Button = new Button();

            public function CustomComponent()
            {
                      super();
                      this.btn.width = 100;
                      this.btn.height = 100;
                      this.btn.label = this.label;
                      this.addChild(this.btn);
            }

            public function set title(val : String) : void
            {
                      this._title = val;
                      this.btn.label = val;
            }

            public function get title() : String
            {
                     return this._title;
            }

            public function set active(val : Boolean) : void
            {
                     this._active = val;
                     this.btn.enabled= val;
            }

            public function get active() : Boolean
            {
                    return this._active;
            }
      }
}

So, if you now build your project, a .swc file will be generated in your [bin] folder, which you can add in other projects and use. But now, the component will be placed in the [Custom] directory in the Components View and not the one you want. We’ll do that a bit later now let’s just….

Give it an icon

This is really simple. All you have to do is get a nice looking .png icon (16×16 pixels preferably) and place it next to your component. Then, use the IconFile metadata tag to link it to your component.

....
[IconFile("icon.png")]
public class mycomponent extends Canvas
{
       private var _title : String = 'Title';
       .......

Inspectable properties

If you have extra information about the property that will help code hints or the Property Inspector (such as enumeration values or that a String is actually a file path) then also add [Inspectable] metadata with that extra info. For our methods we have:

       ...
       [Inspectable(category="General", type="String", defaultValue="")]
       public function set title(val : String) : void
       .....
       .....
       [Inspectable(category="General", type="Boolean", defaultValue="true", enumeration="true,false")]
       public function set active(val : Boolean) : void

This will also help a lot when we’ll add this properties in the Flex Properties panel. For more informations about the [Inspectable] metadata tag visit http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001658.html

Create custom component folder

So, if you want to build a professional component, you can’t leave Flex add your component in the default [Custom] directory in the Components tree. So, in order to create your own folder, we must use few tricks.

First of all, you need two .xml files to describe the structure you want flex to use and overide it’s default behavior.
The first file is the manifest.xml which describes the components in the package and their namespaces. In our case we’ll have:

< ?xml version="1.0"?>
<componentpackage>
    <component id="mycomponent" class="org.adm.component.mycomponent"/>
</componentpackage>

Second, we need another xml to describe the way the designer will interpret all this.

< ?xml version="1.0" ?>
<design>
	<namespaces>
		<namespace prefix="adm" uri="http://www.adm.org" />
	</namespaces>
	<categories>
		<category id="Test" label="Test Panel" defaultExpand="true" />
	</categories>
	<components>
		<component name="mycomponent" namespace="adm" category="Test" displayName="Rename Me" />
	</components>
</design>

In the design.xml, you can specify the namespaces you used for the components, in this case adm will point to components in the org.adm folder.

Categories tag describes the folders you want added in the Components panel. Each category must have an id which you’ll use to tell components where they should reside, a label for the category to stand as the directory name in the Components panel. defaultExpand is an optional parameter which if set to true, the folder will be showed expanded by default.

In the components tag, you specify which component goes in what category and under what title. The name parameter must match the id of a component listed in the manifest.xml. All the other parameters are pretty self explanatory.

Next, you have to include this two files in .swc package. To do that, follow the steps:

* Right Click at your project and select Properties
* In the left choose Flex Library Build Path
* Select the assets tab and mark to include manifest.xml and design.xml files
* Now select the Flex Library Compiler and include your namespace URL (in this case http://www.adm.org)
* Include the manifest file .xml you’ve created
* Click apply and ok to finish

design Create professional Flex components
manifest Create professional Flex components

After this, you should end up with something like this

comppanel Create professional Flex components

Add properties to the Flex Properties view

As I said before, no matter what properties you add to your component, they will never be visible in the Flex Properties standard view, only in the category view and that only if you use the [Inspectable] metadata tag. But few more lines in the design.xml file should take care of that.

<component name="mycomponent" namespace="adm" category="Test" displayName="Rename Me">
		<mxmlproperties>
			<textfield id="title" name="Component Title:" />
			<combo id="active" name="Active:" />
		</mxmlproperties>
		<defaultattribute name="active" value="true" />
	</component>

The id of the mxmlProperties tag should be the function/variable names from your component you want to edit. You can also define default values for those properties using the defaultAttribute tag below. Here we’ve only used the textfield and the combo type but there are few more you can use.

<textfiled id="propertyOrStyle" name="Label:" [multiline="false"] />
<combo id="propertyOrStyle" name="Label:" />
<colorpicker id="propertyOrStyle" name="Label:" />
<filepicker id="propertyOrStyle" name="Label:" [wrapInEmbed="false"] />
<slider id="propertyOrStyle" name="Label:" min="0" max="10" increment="1" />

For combo boxes, if you use it for a Boolean property, it will automatically be populated with [true,false] values but if want something else, the [Inspectable] metadata tag has the enumeration property where you can define the properties from this combo.

flexprop Create professional Flex components

Another thing you must do in order to apply the values as soon as you change them from the properties view, is to set the functions/variables as [Bindable].

    ....
    [Inspectable(category="General", type="String", defaultValue="")]
    [Bindable]
    public function set title(val : String) : void
	....
	....
    [Inspectable(category="General", type="Boolean", defaultValue="true", enumeration="true,false")]
    [Bindable]
    public function set active(val : Boolean) : void
	....
	....

One more thing

Now, compiling this will give you a 300 something KB .swc file which is a bit large to distribute. The user will use this component inside a flex component so embedding all the libraries and SDK inside your .swc is useless. So the next step is to go to Properties->Flex Library Build Path->Library path tab, expand the build path library try and edit everything inside the SDK tree on Link Type and choose External

paths Create professional Flex components

Now the swc component only have 4KB. Big cut down, eh ?

Done

If this is to much trouble for you or you’ve missed something, I’ve attached the sources for this tutorial here. You can import this skeleton rename the package, namespace and the component and start building on this. I hope it all made sense and….happy coding.

Categories: development Tags:

XML、XMLList、XMLListCollection 三者的区别

March 4th, 2010 momoko8443 No comments

正好看到有网友在艾睿论坛上提问。http://bbs.airia.cn/FLEX/thread-10337-1-1.aspx

XML 和XMLList都继承于Object。如果XML是最小单位的话,XMLList与之的区别就是api doc中下面这句话。
The XMLList class contains methods for working with one or more XML elements. An XMLList object can represent one or more XML objects or elements (including multiple nodes or attributes), so you can call methods on the elements as a group or on the individual elements in the collection.

即XMLList是一个或多个XML的组合形式。
如XML的结构是
<data>
<item>value1</item>
<data>
那么XMLList的结构可以是一个XML
<data>
<item>value1</item>
<data>
也可以是多个XML
<data>
<item>value1</item>
<data>
<data>
<item>value2</item>
<data>

XMLListCollection 的继承于ListCollectionView,ListCollectionView这个类看着挺眼熟吧,不错我们最常用的 ArrayCollection就是继承于这个ListCollectionView。api doc中下面这句话描述了XMLListCollection的作用
The XMLListCollection class provides collection functionality to an XMLList object and makes available some of the methods of the native XMLList class.

XMLListCollection的作用是给XMLList 增加一些集合功能如addItem等,前提是你需要
new XMLListCollection(new XMLList())

Categories: others Tags: , ,

[转]正则表达式备忘

March 3rd, 2010 momoko8443 No comments

下表是元字符及其在正则表达式上下文中的行为的一个完整列表:
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。
$ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。
* 匹配前面的子表达式零次或多次。
+ 匹配前面的子表达式一次或多次。+ 等价于 {1,}。
? 匹配前面的子表达式零次或一次。? 等价于 {0,1}。
{n} n 是一个非负整数,匹配确定的n 次。
{n,} n 是一个非负整数,至少匹配n 次。
{n,m} m 和 n 均为非负整数,其中n < = m。最少匹配 n 次且最多匹配 m 次。在逗号和两个数之间不能有空格。
? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 ’\n’ 在内的任何字符,请使用象 ’[.\n]’ 的模式。
(pattern) 匹配pattern 并获取这一匹配。
(?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。
(?!pattern) 负向预查,与(?=pattern)作用相反
Read more…

[转]Flex组件的继承关系

February 24th, 2010 momoko8443 No comments

通过继承,会了某一个组件的用法,与它有同样的继承关系的组件也就了解了

FLEX组件继承关系:
Object->EventDispather->DisplayObject->InteractiveObject->DisplayObjectContainer->Sprite->UIcomponet->All components

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 容器类组件

Application继承关系
UIComponent->Container->LayoutContainer->Application

ApplicationControlBar继承关系
UIcomponent->Container->Box->ControlBar->ApplicationControlBar
其主要用于全局导航,其是ControlBar的子类,后者用于Panel及itleWindow
Read more…

Categories: framework Tags:

使用RSL减少swf体积进阶篇

November 17th, 2009 momoko8443 No comments

国庆刚过,手头上没什么事,看看论坛,打打酱油,闲着也是闲着随便写点项目开发中小心得吧

市面上RSL文章很多,基础的我就不写了(一般都是把framework_3.x.x.xxxx.swz文件剥离出去),这里写几则平时不常见的RSL使用小技巧

技巧一:
datavisualization_3.x.x.xxxx.swz 还有rpc_3.x.x.xxxx.swz这两个文件也是可以剥离出去的

技巧二:
有些同学把datavisualization和rpc剥离出去后,发现除了runtimes exception,说什么tween effect有问题,这个时候就需要在Flex build path面板中进行加载顺序的设置了,把framework提升到最上面。

pic
技巧三:
如果你将你的项目发布到Portal环境下,相对路径的URL都会进行转发,也就是说所有使用相对路径加载外部资源的都会失效,那么你就需要把你的swz文件放在一台http server上进行远程加载

pic2

Read more…

Categories: development Tags: ,

开源项目as3wordpresslib测试版发布

November 6th, 2009 momoko8443 No comments

写在前面的话:
艾睿原创团队群的讨论时给我的灵 感,as3wordpresslib项目从7月份开始勾划,当中由于工作原因搁置了2个月,其实实际的开发时间也就只有短短2周左右,由于需要筹备婚事, 我感觉今后几个月之内不能全力support这个项目,但是不想让项目夭折,所以选择今天把这个完成度90%的项目发布出来,希望感兴趣的朋友能学习它, 使用它,完善它,推广它。

项目简介

什么是as3wordpresslib?

as3wordpresslib 是一套as3类库,这套类库实现了wordpress 2.8中基于xmlrpc的service接口。flex/flash开发人员利用这套类库就可以轻易的访问wordpress后台,而把注意力集中在打 造自己的个性化Blog UI。

使用as3wordpresslib你可以做些什么?

1. 使用Flex/Flash技术,实现blog的UI,打造个性化blog。
2. 使用Flex/AIR技术,实现blog离线撰写发布程序。

开发as3wordpresslib的目的?

1. 使得希望学习flex/flash开发而又缺少后台支持的初级程序员可以迅速方便的focus在自己感兴趣的领域。(其实这是本project的真正目 的,希望每个初学者都可以毫无后顾之忧的使用as3wordpresslib写出自己第一个真正意义上的Hello World )
2. 希望能带动其他的Flexers能和我一起做些虽然看似dirty work但是对于Flex应用推广和发展却有较大帮助的公益劳动。

为什么选择WordPress作为Blog后台

1. wordpress 目前非常火热,大部分技术人员都拥有自己的wordpress blog。(良好的群众基础)
2. wordpress 更新迅速,支持用户自定义插件皮肤,这正是Flex项目在开发中需要考虑的问题。

Read more…

Categories: framework Tags: , , ,

一个Flex3er学Flex4(2)——Groups vs Canvas

August 5th, 2009 momoko8443 2 comments

这篇日志会维护Flex4中的Groups和Flex3中的Canvas的不同点,发现一点写一点。

正好艾睿论坛上有人问道这个问题。

Canvas中要设置它的背景和边框是十分容易的

<mx:Canvas backgroundColor=”#000000″ borderStyle=”solid” borderColor=”#FFFFFF” />

Groups中则比较复杂了。

<s:Group xmlns:fx=”http://ns.adobe.com/mxml/2009” xmlns:s=”library://ns.adobe.com/flex/spark” xmlns:mx=”library://ns.adobe.com/flex/halo”
height=”100″ width=”600″>
<s:Rect width=”100%” height=”100%”>
<s:stroke>
<s:Stroke color=”0×000000″ weight=”.5″ />
</s:stroke>
<s:fill>
<s:SolidColor color=”yellow”/>
</s:fill>
</s:Rect>
<mx:Label x=”24″ y=”21″ text=”Label” color=”#000000″/>
</s:Group>

Categories: Flex4 Tags: , ,

一个Flex3er学Flex4(1) ——加载启动参数flashvar

August 5th, 2009 momoko8443 No comments

flex3中的做法是在html-template下修改index.template.html如下

AC_FL_RunContent(
“src”, “${swf}”,
“width”, “${width}”,
“height”, “${height}”,
“align”, “middle”,
“id”, “${application}”,
“quality”, “high”,
“bgcolor”, “${bgcolor}”,
“name”, “${application}”,
“allowScriptAccess”,”sameDomain”,
“type”, “application/x-shockwave-flash”,
“pluginspage”, “http://www.adobe.com/go/getflashplayer“,
“flashvars”,”id=913092672″
);

然后在as3中使用Application.application.parameters["id"]来调用

Flex4中的设置方法有所改变,还是index.template.html,设置如下

<script type=”text/javascript”>
<!– For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. –>
var swfVersionStr = “${version_major}.${version_minor}.${version_revision}”;
<!– To use express install, set to playerProductInstall.swf, otherwise the empty string. –>
var xiSwfUrlStr = “${expressInstallSwf}”;
var flashvars = {};
flashvars.id= “913092672“;
var params = {};
params.quality = “high”;
params.bgcolor = “${bgcolor}”;
params.allowscriptaccess = “sameDomain”;
params.allowfullscreen = “true”;
var attributes = {};
attributes.id = “${application}”;
attributes.name = “${application}”;
attributes.align = “middle”;
swfobject.embedSWF(
“${swf}.swf”, “flashContent”,
“${width}”, “${height}”,
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
<!– JavaScript enabled so display the flashContent div in case it is not replaced with a swf object. –>
swfobject.createCSS(“#flashContent”, “display:block;text-align:left;”);
</script>

在as3中调用方式也发生了改变

FlexGlobals.topLevelApplication.parameters["id"]

Categories: Flex4 Tags: ,

转:《Flex 4 & Flash Builder 4 快速入门》正式发布

June 26th, 2009 momoko8443 5 comments

目 录 (图书目录已编入PDF”书签”栏)

第一章 Flash Builder 4 背景

1.1 从Flex Builder到Flash Builder

第二章 Flash Builder 4 界面
2.1 主界面
2.2 主菜单
2.3 工具条
2.4 主要窗口


第三章 Flash Builder 4 新特性
3.1 Package explorer
3.2 悬停时的ASDOC提示
3.3 Getter & Setter
3.4 自动生成EventHandler
3.5 条件断点

第四章 Flex SDK 4 新特性
4.1 主题
4.2 布局
4.3 特效
4.4 样式
4.5 状态
4.6 双向绑定
4.7 ASDoc
4.8 SWFObject 与 HTML Template


第五章 自定义组件开发

5.1 自定义Flex组件
5.2 MXML组件开发
5.3 ActionScript组件开发


第六章 与服务端通信

6.1 通过Http Service与服务端通信
6.2 通过Web Service与服务端通信
6.3 通过Remoting与服务端通信
6.4 与Flash Media Server交互



《Flash Builder 4 快速入门》内容简介

在Adobe发布Flash Builder 4后,艾睿论坛 (bbs.airia.cn)编辑团队协作完成了这部教学。 《Flash Builder 4 快速入门》6月25日正式发布第一个版本,今后还会根据读者需要进行更新。

本书适合谁读?

《Flash Builder 4 快速入门》除了适合想学习FLEX开发和正在学习FLEX开发的朋友,也广泛适用与FLASH、 Actionscript开发者进行转型和深入学习。包括.NET、JAVA开发人员在内,这本书都将为你日后的含RIA开发工作奠定牢固的知识基础。

章节简介:

本书1-2章主要为新接触FLEX开发的朋友提供入门帮助,以图文并茂的方式从零基础开始指引新手走进 FLEX开发,如果是有一定基础的FLEX开发者,这两章可以简单翻阅。3-5章开始进阶学习, 介绍FB4 / FLEX SDK 4 新特性与开发实例,适合绝大多数的FLEX开发者阅读。 第6章为通信服务方面的必备知识,这一章以实际案例的方式展示了FLEX与服务端通信的4种方式。

本书作者:

本书凝聚了艾睿编辑团队各位作者的大量心血,以下是参与本书编辑的作者,如果你在阅读的期间遇到问题也可以进入Flash Builder快速入门专栏联系作者,或进入作者的博客与其交流。

他们分别是(排名不分先后):

罗楷
http://www.flextheworld.com
kevin.luo.sl@gmail.com

郭峰
http://www.hydra1983.com
Edison@airia.cn

郑会宾
http://www.flexers.cn
momoko8443@163.com
唐凡
http://www.tangfanzone.com
woodytf@sina.com

陆仕桑
http://lushisang.com
lushisang@gmail.com



《Flash Builder 4 快速入门》专栏:
http://www.airia.cn/flashbuilder/

《Flash Builder 4快速入门 》PDF、源码包 下载页:


http://www.airia.cn/flashbuilder/fb4Qbook/source/


下载 Adobe Reader 9 阅读器:

下载地址http://www.onlinedown.net/soft/2696.htm


本帖(以上内容)欢迎大家踊跃转载。让更多的朋友顺利走入FLEX开发。


FQA:

为什么我打开需要密码?

编辑团队对PDF的”修改权限”设置了密码,我们希望可以保证该文档的完整性、安全性,不希望文档被任意篡改,最终目的是为了保障读者权益。但这并不影响阅读和学习,通常低版本的Adobe Reader 会报出需要密码,请使用 Adobe Reader 9 阅读器。

Categories: others Tags: ,