Flex开发问答 Flex Development Q&A

Lukialee 于 05月04日 2009编辑 分类: AS3开发技巧, Flex技巧

在Flex开发中,我们会遇到各种各样棘手的问题,查找手册依然不得其解,您可以留言提问,我们会与您共同解决。

1.如何比较两张图片是否一样呢? How to compare two pictures?

这个问题并不能笼统回答,单从技术角度讲,可以将两个图片转换成BitmapData来比较。

BitmapData类的compare方法,可以比较出两个图片不同处

我这里不给例子啦,请参考下面的链接。

Change Image into BitmapData to Compare.

Source code?Please click these:

Flex中如何通过BitmapData类的compare()函数对嵌入的两个bitmap进行比较的例子

http://blog.minidx.com/2008/10/05/1486.html

Comparing two bitmap images using the BitmapData class
http://blog.flexexamples.com/2007/09/19/comparing-two-bitmap-images-using-the-bitmapdata-class/

2.如何调试任意一个目录下的Flex程序?How to debug a swf file under any folder?

关键在于指定要调试的链接地址,在调试选项中修改。It is needed to modify Debug Configurations, locate a new URL or path you what to debug.

在主程序文件上点右键,Debug As—>Debug Configurations
image

此时,只要把Use defaults前面的勾去掉,不要用默认设置,就可以将调试的目标页面设成任意链接了。image

3.为什么在Flex中更改了光标图标后,会觉得卡?After change the default mouse cursor,the mouse can’t move smoothly.

CursorManager所设的鼠标光标,是受到帧频限制的。Flex默认帧频为24,

可以试试在Application标签中设置frameRate="40",你的光标就会更流畅些。但也更耗费CPU。

We can use CursorManager.setCursor() to set a new cursor icon. But you will find it move "slowly". Because SWF is limited by framerate. The framerate of Flex Application is 24. Change it to 40, more smoothly but the CUP occupancy rate is higher.

4.定制右键弹出菜单

右键菜单一直是Flash的一个弱点,虽然可以简单地通过ContextMenu来实现右键菜单,但没有子菜单,顶多能加分隔线。

5.什么是Accordion:可折叠的面板控件

在Flex组件面板中有这么个组件,但如何理解呢?

Accordion这个单词,可以翻译作:

n.

手风琴,折式地图

adj.

可折叠的,可折叠的

所以可以理解为“可折叠的面板控件”,记住它是一个导航器容器。

6.什么情况下使用什么设计模式?

简单地说,MVC模式可以套用到任何程序中,但情况复杂的时候,要在MVC基础上,或结合MVC,再使用更多模式。

image

截图来自ActionScript 3.0 Design Patterns

    创建阶段 构造阶段 行为、执行阶段
  Class 工厂方法 适配器(类) 模板
使用范围        
  Object 单例 适配器(对象) 命令
        观察者
        状态
        策略

7.AS3动态加载图片并控制其大小(适用于Flash和Flex)

Dynamic load image and control its size.

import flash.display.Loader;
import flash.display.Bitmap;
var bg:Loader = new Loader();
var url:String = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
var container:Sprite = new Sprite();
container.x = 0;
container.y = 0;
addChild(container);
bg.x = 0;
bg.y = 0;
bg.load(new URLRequest(url));
bg.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
function imgLoaded(event:Event):void {
Bitmap(bg.content).smoothing=true;
container.addChild(bg.content);
container.width = 251;
container.height = 261;
}

你可以在图片加载后,设定其尺寸。如果不设定,此时也能得到图片的真实尺寸。

8.Flash Player支持mp4视频么?Dose flash Player support mp4 video?

可以播放MP4么?当然了。引用官方文档中的话:

Flash Player 9.0.115.0 and later versions support files derived from the standard MPEG-4 container format including F4V, MP4, M4A, MOV, MP4V, 3GP, and 3G2 if they contain H.264 video and/or HEAAC v2 encoded audio.

所以,要确保Flash Player的版本在9.0.115之上。很容易吧。

So, make sure the version of Flash Player is above 9.0.115.    That’s easy.

9.AS3如何处理JSON数据?

得用到AS3 CORELIB
http://code.google.com/p/as3corelib/

其中有个JSON类,有两个静态方法:
JSON.encode(obj);//convert an object to json string
JSON.decode(jsonStr);//decode json string to an object

10.XML, XMLList, Object, Array and ArrayCollection 如何互相转化? How to convert these data to another type?

这里提供个思路。

若是将XML节点的属性转为Array的元素,就遍历这个XML节点的属性,挨个儿push进数组中。

若是将XML和Object互相转换,则可以用mx自带的“XML编码器”和“XML解码器”

SimpleXMLDecoder

decodeXML(dataNode:XMLNode):Object

SimpleXMLEncoder

encodeValue(obj:Object, qname:QName, parentNode:XMLNode):XMLNode

 

11.能否设置mx:TextArea的数据每次加入一行之后自动向下滚动,显示最新信息?

ta.verticalScrollPosition = ta.maxVerticalScrollPosition;

在Adobe官方提供的例子中,TelnetSocket演示了这个功能。
http://livedocs.adobe.com/flex/3/flex3_actionscript_examples.zip

12.为何Flash Player有时崩溃之前不报错?

并不是所有内部错误都会报告出来,有时候顶级的、核心的错误,FlashPlayer会自己处理,直到崩溃也不提示任何信息。比如最近我遇到的一个问题,Flex中的HSlider刻度超多的时候,会导致播放器崩溃。没有任何征兆,浏览器直接挂掉,这如何能猜到呢?

13.在Flex中自定义组件,createChildren()是什么时候被调用的?

Flex calls the createChildren() method in response to the call to the addChild() method to add the component to its parent. This is an advanced method that you might override when creating a subclass of UIComponent.

只有父容器执行addChild()的时候才会被调用。

2 Responses to “Flex开发问答 Flex Development Q&A”

  1. chii Says:

    请问怎么动态用图片替换鼠标形状

    我在做个游戏
    希望鼠标点中某个道具的时候,鼠标就变成道具的样子,请问怎么实现

    见过有人用下面这个方法,但是这个例子里的图片是静态路径,有没有办法动态指定一张图片
    [Embed(source="ClickHand.gif")]
    public var clickHand:Class;//图标
    var cursorID:Number = CursorManager.setCursor(clickHand);//更改鼠标图标

    CursorManager.removeCursor(cursorID);//还原鼠标图标

  2. Lukialee Says:

    方法很多的。
    这个Class本质上就是一个显示对象,官方文档中写:
    You can use a JPEG, GIF, PNG, or SVG image, a Sprite object, or a SWF file as the cursor image.
    不仅可以嵌入,也可以动态加载。参考一下别人的文章:
    http://chargedweb.com/labs/2010/03/12/flex-cursormanager-vs-jloa/
    http://stackoverflow.com/questions/428559/flex-converting-a-jpg-to-a-class-at-runtime

发表评论 Leave a Reply