ActionScript 3开发的几个注意点

 1、MouseEvent在某些时候要记得运行MouseEvent.updateAfterEvent();方法。主要表现在拖动的时候。

如果不执行该方法,会有延迟,比如拖动会有延迟。

2、某些时候要记得stopImmediatePropagation(参考:做个笔记Event的stopImmediatePropagation和stopPropagation方法)

主要表现在单击某元素的时候,不希望这个click操作到达下一层。因为stopImmediatePropagation立即进行了事件冒泡。防止对事件流中当前节点中和所有后续节点中的事件侦听器进行处理。

Example:

3、同AJAX一样,多次请求一个动态页面请加个时间戳,防止缓存。

var date:Date = new Date();
xmlLoader.load("xml.aspx?datestamp="+date.getMilliseconds());
//xmlLoader是我自己的类,继承了Loader类。


4、(感谢望月狼的留言)
mouseEnabled  = false
?.tabEnabled = false
?.mouseChildren = false
?.tabChildren = false
把DisplayObjectContainer所继承的这四个属性都设掉,可降低cpu运算率

5、Flex中使用TitleWindow时如果要通过代码控制CloseButton按钮记得使用invalidateDisplayList();方法

(参考:使用代码控制TitleWindow的showCloseButton的奇怪问题)

它其实实现的是mx.core.UIComponent的updateDisplayList()方法。但是不可能直接使用该方法。

Help中是这么写的。

Draws the object and/or sizes and positions its children. This is an advanced method that you might override when creating a subclass of UIComponent.

You do not call this method directly. Flex calls the updateDisplayList() method when the component is added to a container using the addChild() method, and when the component’s invalidateDisplayList() method is called.

If the component has no children, this method is where you would do programmatic drawing using methods on the component’s Graphics object such as graphics.drawRect().

If the component has children, this method is where you would call the move() and setActualSize() methods on its children.

Components may do programmatic drawing even if they have children. In doing either, you should use the component’s unscaledWidth and unscaledHeight as its bounds.

It is important to use unscaledWidth and unscaledHeight instead of the width and height properties.

 

在写AS3代码的过程中,有些编译错误,或者运行错误,是很难检查。这儿我总结了一些曾经碰到过的问题。
1、TypeError: Error #1006: value 不是函数。
这是在运行时的报错,我们来看看是一段什么样的代码导致他报错的
大家先找找这段代码错在哪,为什么导致这个错误,然后在看看后面说的错误原因
private function clickHandle(e:MouseEvent):void{
  if(e.currentTarget is Sprite){
    e.updateAfterEvent()
    (e.currentTarget as Sprite).visible = false;
  }
}
如果你是传统程序员,这个错误一般情况来说,你都不会犯的;如果你是AS2到AS3的,或者说你以前写asp等网页程序的,这个问题的引发几率就相对比较高了。
我们都知道AS3的代码,如果没有";"的情况下也是可以编译、可以运行的。不过恰恰上述代码在e.updateAfterEvent()后因为没有";",导致他运行错误。因为这段代码在运行时,它是被当成了这样在运行
e.updateAfterEvent()(e.currentTarget as Sprite).visible = false;
updateAfterEvent返回的是void类型,那么把他当成函数来调用,自然会报错 value 不是函数。
总结1:大家明白了吗?从上面这个例子可以看出,养成加";"号的习惯还是非常重要的。

2、TypeError: Error #1034: 强制转换类型失败:无法将 flash.events::Event 转换为 MyEvent。
同样这也是一个运行错误,我们来看看是一段什么样的代码导致他报错的
大家也先找找他的错在哪

首先先看一下这个MyEvent类

package
{
  import flash.events.Event;

  public class MyEvent extends Event
  {
    public static const MY:String = "my";
    
    public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
    {
      super(type, bubbles, cancelable);
    }
    
  }
}

然后侦听这个事件,在得到它后转发到spr对象中
addEventListener(MyEvent.MY,myHandle);
private function myHandle(e:MyEvent):void{
  spr.dispatchEvent(e);
}
再就是,spr对象中侦听了这个事件
spr.addEventListener(MyEvent.MY,myHandle);
private function myHandle(e:MyEvent):void{
  trace(e);
}
最后在这个侦听的地方就报错了。

在殿堂之路中,继承Event自定义事件举例中讲过这种问题的解决办法,但没有讲的很详细
很多新手在看高手的代码,都会看到自定义事件中都会重构clone这个方法,但是自己尝试后发现,不重构clone也不会出现什么问题,以至于有些人在写项目赶时间时,也不太喜欢100%模仿高手的写法,就会把重构clone这个看似很小,其实隐藏了秘密的东西给忽略掉。
在殿堂之路中有句原话:

以下摘抄《殿堂之路》第300页原话
其中clone()方法最为重要,使用clone()方法可以返回当前事件对象的一个拷贝。这个clone()方法一般不需要我们手工调用,当我们将一个事件对象重复发送时,EventDispatcher类会自动调用clone()方法,产生这个事件的拷贝。
而上面的代码中就是在将MyEvent事件重复发送,好理解的说就是转发MyEvent事件,如果没有重构MyEvent事件,那么EventDispatcher在重复发送时,调用的clone是Event的clone方法,那么clone出来的事件自然就是Event类,而不是MyEvent,所以你在侦听时使用e:MyEvent类型接收时,自然就会报错无法将Event类转换成MyEvent类。

你可能感兴趣的:(开发,职场,actionscript,休闲,3)