Flex的mxml中调用绘图As代码
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="initView()"> <mx:Label x="10" y="10" text="As3.0 -> Flex!"> </mx:Label> <mx:Canvas x="0" y="0" id="canvas" width="100%" height="100%"> </mx:Canvas> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ //需要使用Canvas来包含UIComponent,然后将自己定义的As类添加到Component中 import mx.core.UIComponent; private function initView():void { var component:UIComponent=new UIComponent(); canvas.addChild(component); component.addChild(new MyDemo()); } ]]> </fx:Script> </s:Application>
MyDemo.as:
package { import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.utils.Timer; import mx.controls.Label; /** * 将上一次记录的Blog <<flex(入门)之timer的使用,键盘,鼠标的监听>> 的As代码放到flex的Web项目中去调用,这个时候会有问题 * 原因出在了舞台上,然后在网上搜索了一下,问题解决了。 * * */ //窗体大小,绘制帧率 public class MyDemo extends Sprite { private var circle:Shape; private var label:Label; private var timer:Timer; public function MyDemo() { super(); label = new Label(); label.x = 30; label.y = 30; addChild(label); circle = new Shape(); circle.x = 50; circle.y = 100; addChild(circle); circle.graphics.beginFill(0xee2f2f); circle.graphics.drawCircle(circle.x,circle.y,40); circle.addEventListener(Event.ENTER_FRAME,changePos);//每帧调用changePos函数 //------------------需要增加Event.ADDED_TO_STAGE时间监听-------------------// this.addEventListener(Event.ADDED_TO_STAGE,addToStageHandler); //------------------通过addToStageHandler()来添加舞台监听-------------------// timer = new Timer(1000,5); //设置延迟时间是1秒,循环执行5次timerExcute() timer.addEventListener(TimerEvent.TIMER,timerExcute); timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete);//执行完成后调用timerComplete timer.start();//启动timer } public function changePos(event:Event):void { if(circle.y >= 300) { circle.y = 1; }else if(0<circle.y && circle.y < 300) { circle.y+=5; } } //不可以直接在flex文件中调用as的代码(抛空对象异常),然后as代码中直接使用stage对象, public function addToStageHandler(event:Event):void { stage.addEventListener(MouseEvent.CLICK,onClick); //舞台上添加鼠标点击监听事件 stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);//舞台添加键盘监听 } //在点击的地方产生绿色方形 public function onClick(event:MouseEvent):void { var rect:Shape = new Shape(); addChild(rect); rect.graphics.beginFill(0x11ff00); rect.graphics.drawRect(event.stageX,event.stageY,20,20); } public function keyDown(event:KeyboardEvent):void { var count:int = 0 if(event.keyCode == 13) //监听Enter键 { var rect:Shape = new Shape(); addChild(rect); rect.graphics.beginFill(0x00f1f0); rect.graphics.drawRect(Math.random()*400,Math.random()*300,10,10); } } public function timerExcute(event:TimerEvent):void { var rect:Shape = new Shape(); addChild(rect); rect.graphics.beginFill(0x0f11f0); rect.graphics.drawRect(Math.random()*400,Math.random()*300,10,10); } public function timerComplete(event:TimerEvent):void { var rect:Shape = new Shape(); addChild(rect); rect.graphics.beginFill(0x000000); rect.graphics.drawRect(Math.random()*400,Math.random()*300,30,60); } } }