Aedis.Ju
显示标签为“ActionScript3”的博文。显示所有博文
显示标签为“ActionScript3”的博文。显示所有博文

2007年5月24日星期四

ActionScript3 Tip Of the Day Update!(Version 20070518)


以前就介绍过ActionScript3 Tip Of the Day,只要过了英语四级就不会有难度,这次的更新不仅是内容上的扩张和补充,最重要的是,作者已经为我们清楚得分好了章节,所以有些内容也是有重复的。









Announce: senocular has the real copyWrite!

通过DataField来正确处理DataGrid的ColumnIndex的变化

由于客户需求的不确定,我们常常会修改DataGrid里面的内容,时而多一列时而少一列,因此它引发了DataGrid的ColumnIndex的变化。
当我们在AcitonScript代码中对columnIndex进行判断时,常常会忘记改变columnIndex从而引发bug。

sample.mxml

<mx:DataGrid id="PersonInfoDG" dataProvider="{PersonInfo_ARRAY}">
<mx:columns>
<mx:Array>
<mx:DataGridColumn headerText="First Name" dataField="firstName"/>
<mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>


sample.as

[Bindable]
private var PersonInfo_ARRAY:Array =
[{firstName:"aaa", lastName:"bbb"},
{firstName:"ccc", lastName:"ddd"}];

private function initListener():void {
PersonInfoDG.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, doDoubleClick);
}

private function doDoubleClick(event:ListEvent):void {
if (event.rowIndex > 0) {
if(event.columnIndex == 1) {
Alert.show("It's Last Name Column!")
}
}
}


增加了Address字段

sample.mxml

<mx:DataGrid creationComplete="initListener()" id="PersonInfoDG" dataProvider="{PersonInfo_ARRAY}"
doubleClickEnabled="true">
<mx:columns>
<mx:Array>
<mx:DataGridColumn headerText="First Name" dataField="firstName"/>
<mx:DataGridColumn headerText="Address" dataField="address"/>
<mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>


sample.as

[Bindable]
private var PersonInfo_ARRAY:Array =
[{firstName:"aaa", lastName:"bbb", address:"AB Road"},
{firstName:"ccc", lastName:"ddd", address:"CD Road"}];
private function initListener():void {
PersonInfoDG.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, doDoubleClick);
}

private function doDoubleClick(event:ListEvent):void {
if (event.rowIndex > 0) {
if(event.columnIndex == 1) {
Alert.show("It's Last Name Column!")//It's Wrong! It has change to 2
}
}
}

那我们怎么样才能今可能得减少这种错误呢?我们发现,在columnIndex在跟着不断变化的同时,我们所要表示的内容一般是不变的。是的,我们可以通过字段dataField来进行判断,而且不会有影响。


private function doDoubleClick(event:ListEvent):void {
if (event.rowIndex > 0) {
for(var i:Number; i < PersonInfoDG.columns.length; i++) {
if(PersonInfoDG.columns[i].dataField == "lastName" && i == event.rowIndex) {
Alert.show("It's Last Name Column!")//It's Right! :)
Break;
}
}
}
}

2007年4月27日星期五

mx_internal_uid的在数据传递时的重复解决方法

关于这个【mx_internal_uid】的问题,一直抽空想写出来,乘着今天日本那边休息,还是把它给记录下来吧,希望对其他的FLEX爱好者在碰到此类问题的时候有所帮助。


我们首先了解一下【mx_internal_uid】的概念:

uid : String---The unique identifier for this object


UID它一共有32位,其中通过 ActionScript's Math.random()来产生24位,后面的8位是current date-time来决定的 ,所以只要不要有中彩票的运气(24位的Math.random(),足球彩票才14场猜0.1.3...),再加上时间不停止,所以它无论如何也是在这世界上是唯一的 。

当我们对2个DataGrid进行传递信息直接的通信时候,我们会常常这么做:



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>

<![CDATA[

import mx.utils.UIDUtil;

import mx.collections.ArrayCollection;

[Bindable]

public var dataArr:Array = [{label:"Red", data:"#FF0000"},

{label:"Green", data:"#00FF00"},

{label:"Blue", data:"#0000FF"}

];

[Bindable]

public var problemArr:Array = new Array();

private function addFromTopDG():void {

problemArr.push(fromDG.selectedItem);
//problemArr.push(ObjectUtil.copy(fromDG.selectedItem));

toDG.dataProvider = problemArr;



}

]]>

</mx:Script>

<mx:DataGrid id="fromDG" width="600" height="200" dataProvider="{dataArr}">

<mx:columns>

<mx:Array>

<mx:DataGridColumn width="300" dataField="mx_internal_uid"/>

<mx:DataGridColumn dataField="label"/>

<mx:DataGridColumn dataField="data"/>

</mx:Array>

</mx:columns>

</mx:DataGrid>

<mx:Button label="Add from Top_DG" click="addFromTopDG()"/>

<mx:DataGrid id="toDG" width="600" height="200" dataProvider="{problemArr}">

<mx:columns>

<mx:Array>

<mx:DataGridColumn width="300" dataField="mx_internal_uid"/>

<mx:DataGridColumn dataField="label"/>

<mx:DataGridColumn dataField="data"/>

</mx:Array>

</mx:columns>

</mx:DataGrid>

</mx:Application>



这时候我们会发现很奇怪的事情发生了,当我们对fromDG数据连续传输到toDG的时候,toDG的数据如果是相同的情况(如很多个Red),我们发现对数据我们用鼠标无法聚焦,仔细一看,原来是【mx_internal_uid】一样呀,从中,我们可以了解以下信息:

1.因为引用关系一直存在,所以传过来的东西是一直一样的。如果把toDG放在一个TitleWindow里传一次再关一次,即削除引用关系,它是没有问题的。

2.就算是ObjectUtil.copy(obj),只要在引用关系还存在的情况下,它copy出来的uid竟然是一样的。那么这样的话Adobe要不要考虑一下重写这个方法呢?毕竟我要的只是里面的值一样而不要把UID一起传过来啊。


解决方法:


合理利用UIDUtil这个类为我们创造合理的UID。




private function addFromTopDG():void {

var obj:Object = ObjectUtil.copy(fromDG.selectedItem);

obj.mx_internal_uid = UIDUtil.createUID();


//这里千万不要直接fromDG.selectedItem.mx_internal_uid = UIDUtil.createUID();

//因为上面说过这样引用关系还存在。

(toDG.dataProvider as ArrayCollection).addItem(obj);

obj = null;//这个就靠自己的CODE 风格啦,不要也没关系



恩,这样问题就解决啦。

2007年2月5日星期一

Sprites和Shapes的区别之一

为什么Button的mouseChildren是false呢?


mouseChildren顾名思义,就是这个存在在object的
孩子是否应该响应到mouse events事件
我们知道Button的组成包括了UITextField,另外包括了很多的skins和icons.

skins包括了upSkinName,overSkinName,downSkinName,disabledSkinName,selectedUpSkinName,selectedOverSkinName,selectedDownSkinName,selectedDisabledSkinName;
icons则包括了iconName,upIconName,overIconName,downIconName,disabledIconName,selectedUpIconName,selectedOverIconName,selectedDownIconName,selectedDisabledIconName.
skins和icons这些用户其实都是可以重新自定义的.

最主要的目的是为了保证mouse events是从Button它自己本身的触发,而不是它的skins,icons,UITextField.

我们在定义skins和icons的时候,会用到Sprites和Shapes,如果mouseChildren是true的话,用到Sprites的会无法响应click事件,Shapes还会是正常的.为什么呢?这是因为用到Sprites,做click事件的时候,它将直接响应到skins或者icons,而不是Button它本身,而Shapes是不会响应mouse events的.为了不让有这种情况发生,mouseChildren = false.

knowledge:

mouseChildren : BooleanDetermines whether or not the children of the object are mouse enabled.

Shape ---> DisplayObject --->EventDispatcher ---> Object
The Shape class is used to create lightweight shapes by using the ActionScript drawing application program interface (API). The Shape class includes a graphics property, which lets you access methods from the Graphics class. The Sprite class also includes a graphicsproperty, and it includes other features not available to the Shape class. For example, a Sprite object is a display object container, whereas a Shape object is not (and cannot contain child display objects). For this reason, Shape objects consume less memory than Sprite objects that contain the same graphics.

Sprite --->DisplayObjectContainer--->InteractiveObject ---> DisplayObject ---> EventDispatcher ---> Object
The Sprite class is a basic display list building block: a display list node that can display graphics and can also contain children. A Sprite object is similar to a movie clip, but does not have a timeline. Sprite is an appropriate base class for objects that do not require timelines. For example, Sprite would be a logical base class for user interface (UI) components that typically do not use the timeline.The Sprite class is new in ActionScript 3.0. It provides an alternative to the functionality of the MovieClip class, which retains all the functionality of previous ActionScript releases to provide backward compatibility.

2007年1月30日星期二

AS3 Tip Of the Day

I thought that I haven’t any times to write any words about AS3.
In
kirupaForum, I found that senocular had wrirted a great article!
Here are his words:
The release of Flex Builder 2 is around the corner and though the next version of Flash is still a ways away, ActionScript 3 will be a big part of Flex 2 and the impending release of Flash Player 9 (which arrives with Flex). ActionScript 3 is the next step forward and to help with the transition (for those of you deciding to make it), I thought, since I've been working with AS3 a bit lately, I'd make a new Tip of the Day thread for ActionScript 3.0 to help people prepare. -- senocular

Announce: senocular has the real copyWrite


我想我根本没有什么时间去写一些关于AS3的东西,
但在
kirupaForum,有一位名叫senocular的小伙子写好了下面这篇精彩的文章,
下面是他的的话:
The release of Flex Builder 2 is around the corner and though the next version of Flash is still a ways away, ActionScript 3 will be a big part of Flex 2 and the impending release of Flash Player 9 (which arrives with Flex). ActionScript 3 is the next step forward and to help with the transition (for those of you deciding to make it), I thought, since I've been working with AS3 a bit lately, I'd make a new Tip of the Day thread for ActionScript 3.0 to help people prepare. -- senocular

声明: senocular拥有最终的著作版权.


--- ---Aedis.Ju

一不小心有了时间就整理出来了这份稿子,ActionScript3小贴士100真得很好,有兴趣看看的就试试吧...

下载AS3 Tip Of the Day - Aedis From Senocular.DOC版本
下载AS3 Tip Of the Day - Aedis From Senocular.PDF版本
下载办法:1. 请点击下载地址下载文件2. 用鼠标右键点击下载地址,选择下载工具下载 如果有下不到的,就请麻烦一下去www.mofile.com提取一下,提取码分别为7717702603510744和2692680849011767PS:英文的,不过应该都能看得懂