交互性是FLASH的灵魂,那我们如何在FP8.5实现交互呢?以往我们可以通过添加on(release){} 或 this.onRelease = function(){}之类的语句来和MovieClip进行交互。在AS3.0中,我们必须通过类似以往V2组件的交互方式addEventListener来实现这一功能。
就象我们前面提到的一样,现在我们要用Sprite来代替MovieClip了。
//TEST6.as
package {
import flash.events.MouseEvent;
import flash.events.MouseEventType;
import flash.util.trace;
import flash.display.Sprite;
public class Test extends Sprite {
public function Test() {
addChild( new MySprite());
}
}
private class MySprite extends Sprite
{
public function MySprite(){
createUI();
}
private function createUI(){
graphics.beginFill(0xff0000);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
useHandCursor = true;
addEventListener( MouseEventType.MOUSE_UP, mouseListener);
}
public function mouseListener( evt:MouseEvent){
trace( "Test::mouseListener " + this + ":" + evt);
}
}
}
同AS2.0中的addEventListener比较一下,我们不再需要用Delegate.create将一个function绑定到handler上,这大大简化了我们的编码过程。
就象我们前面提到的一样,现在我们要用Sprite来代替MovieClip了。
//TEST6.as
package {
import flash.events.MouseEvent;
import flash.events.MouseEventType;
import flash.util.trace;
import flash.display.Sprite;
public class Test extends Sprite {
public function Test() {
addChild( new MySprite());
}
}
private class MySprite extends Sprite
{
public function MySprite(){
createUI();
}
private function createUI(){
graphics.beginFill(0xff0000);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
useHandCursor = true;
addEventListener( MouseEventType.MOUSE_UP, mouseListener);
}
public function mouseListener( evt:MouseEvent){
trace( "Test::mouseListener " + this + ":" + evt);
}
}
}
同AS2.0中的addEventListener比较一下,我们不再需要用Delegate.create将一个function绑定到handler上,这大大简化了我们的编码过程。
回复Comments
作者:
{commentrecontent}