package { import flash.display.Sprite; public class ExampleApplication extends Sprite { public function ExampleApplication() { var userName:String = "Bill Smith"; trace("My name is " + userName + "."); } } }
2,使用addEventListener来处理事件
package { import flash.display.Sprite; import flash.events.MouseEvent; public class ExampleApplication extends Sprite { private var _sprite:Sprite; public function ExampleApplication() { _sprite = new Sprite(); addChild(_sprite); _sprite.graphics.beginFill(0xffffff); _sprite.graphics.drawRect(0, 0, 400, 400); _sprite.graphics.endFill(); _sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); _sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); } private function onMouseDown(event:MouseEvent):void { _sprite.graphics.lineStyle(1, 0, 1); _sprite.graphics.moveTo(mouseX, mouseY); _sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseUp(event:MouseEvent):void { _sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseMove(event:MouseEvent):void { _sprite.graphics.lineTo(mouseX, mouseY); } } }
3,实例方法和类方法
package { import flash.display.Sprite; public class ExampleApplication extends Sprite { public function ExampleApplication() { for(var i:int=0;i<10;i++) { drawLine(); } } private function drawLine():void { graphics.lineStyle(1, Math.random() * 0xffffff, 1); graphics.moveTo(Math.random() * 400, Math.random() * 400); graphics.lineTo(Math.random() * 400, Math.random() * 400); } } } public static function showMessage():void { trace("Hello world"); } ExampleApplication.showMessage();
4,数据类型、循环、条件判断、异常处理等都类似于JavaScript,package、import、类继承、对象、方法都类似于Java