实例1:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="717"
height="554"
backgroundGradientAlphas="[1.0, 1.0]"
backgroundGradientColors="[#FFFFFF, #FFFFFF]"
verticalAlign="middle"
creationComplete="init();">
<mx:Canvas x="301"
y="78"
width="300"
height="250"
id="pop"
creationComplete="drawBorder();">
</mx:Canvas>
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private var timer1:Timer=new Timer(20);
private var timer2:Timer=new Timer(20);
private var timer3:Timer=new Timer(20);
private var timer4:Timer=new Timer(20);
private var timer5:Timer=new Timer(20);
private var timer6:Timer=new Timer(20);
private var line1X:Number=150;
private var line1Y:Number=250;
private function init():void
{
timer1.addEventListener(TimerEvent.TIMER, drawLine1);
timer2.addEventListener(TimerEvent.TIMER, drawLine2);
timer3.addEventListener(TimerEvent.TIMER, drawLine3);
timer4.addEventListener(TimerEvent.TIMER, drawLine4);
timer5.addEventListener(TimerEvent.TIMER, drawLine5);
timer6.addEventListener(TimerEvent.TIMER, drawLine6);
}
private function drawLine1(e:TimerEvent):void
{
var _lineToX:Number=line1X + 6;
var _lineToY:Number=line1Y - 12;
pop.graphics.moveTo(line1X, line1Y);
pop.graphics.lineTo(_lineToX, _lineToY);
line1X=_lineToX;
line1Y=_lineToY;
if (line1X >= 180)
{
timer1.stop();
timer2.start();
}
}
private function drawLine2(e:TimerEvent):void
{
var _lineToX:Number=line1X + 22;
pop.graphics.moveTo(line1X, line1Y);
pop.graphics.lineTo(_lineToX, line1Y);
line1X=_lineToX;
if (line1X >= 290)
{
timer2.stop();
timer3.start();
}
}
private function drawLine3(e:TimerEvent):void
{
var _lineToY:Number=line1Y - 20;
pop.graphics.moveTo(line1X, line1Y);
pop.graphics.lineTo(line1X, _lineToY);
line1Y=_lineToY;
if (line1Y <= 10)
{
timer3.stop();
timer4.start();
}
}
private function drawLine4(e:TimerEvent):void
{
var _lineToX:Number=line1X - 20;
pop.graphics.moveTo(line1X, line1Y);
pop.graphics.lineTo(_lineToX, line1Y);
line1X=_lineToX;
if (line1X <= 10)
{
timer4.stop();
timer5.start();
}
}
private function drawLine5(e:TimerEvent):void
{
var _lineToY:Number=line1Y + 20;
pop.graphics.moveTo(line1X, line1Y);
pop.graphics.lineTo(line1X, _lineToY);
line1Y=_lineToY;
if (line1Y >= 190)
{
timer5.stop();
timer6.start();
}
}
private function drawLine6(e:TimerEvent):void
{
var _lineToX:Number=line1X + 13;
pop.graphics.moveTo(line1X, line1Y);
pop.graphics.lineTo(_lineToX, line1Y);
line1X=_lineToX;
if (line1X >= 140)
{
timer6.stop();
}
}
public function drawBorder():void
{
pop.graphics.lineStyle(2, 0xFF00FF, 1);
pop.graphics.moveTo(150, 250);
timer1.start();
// pop.graphics.lineTo(180, 190);
// pop.graphics.moveTo(180, 190);
// pop.graphics.lineTo(290, 190);
// pop.graphics.moveTo(290, 190);
// pop.graphics.lineTo(290, 10);
// pop.graphics.moveTo(290, 10);
// pop.graphics.lineTo(10, 10);
// pop.graphics.moveTo(10, 10);
// pop.graphics.lineTo(10, 190);
// pop.graphics.moveTo(10, 190);
// pop.graphics.lineTo(140, 190);
}
]]>
</mx:Script>
</mx:Application>
实例2:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" borderColor="#FFFFFF" >
<mx:Script>
<![CDATA[
import mx.effects.Move;
import mx.controls.*;
import mx.core.*;
import mx.effects.*;
public var lines :Sprite = new Sprite ();
public var lines1 :flash.display.Sprite = new Sprite ();
public function TestPaint():void
{
//Alert.show("Hello World!");
lines.graphics.lineStyle(2,0x0099ff,1);
DrawLine(lines,lbl,btn1);
DrawLine(lines,lbl,btn2);
DrawLine(lines,lbl,btn3);
var comp: UIComponent = new UIComponent();
comp.addChild(lines);
addChild(comp);
}
public function DrawLine(lines:Sprite,uFrom:UIComponent,uTo:UIComponent):void
{
lines.graphics.moveTo(uFrom.x+uFrom.width/2,uFrom.y+uFrom.height/2);
lines.graphics.lineTo(uTo.x+uTo.width/2,uTo.y+uTo.height/2);
}
public function btn1_mouseOver(event:Event):void
{
lines1.graphics.lineStyle(2,0xd43dd6,1);
DrawLine(lines1,btn1,btn2);
DrawLine(lines1,btn1,lbl);
var comp: UIComponent = new UIComponent();
comp.addChild(lines1);
addChild(comp);
}
public function btn1_mouseOut(event:Event):void
{
lines1.graphics.clear();
DrawLine(lines,lbl,btn1);
}
public function Testmove():void
{
var move:Move = new Move();
if(btn2.x!=lbl.x)
{
move.target=btn2;
move.end();
move.xTo=lbl.x;
move.yTo=lbl.y;
move.play();
}
else
{
move.target=btn2;
move.end();
move.xTo=340;
move.yTo=104;
move.play();
}
}
]]>
</mx:Script>
<mx:Button x="176" y="34" label="btn1" id="btn1" mouseOver="btn1_mouseOver(event)" mouseOut="btn1_mouseOut(event)"/>
<mx:Button x="340" y="104" label="btn4" id="btn2"/>
<mx:Button x="53" y="167" label="btn3" id="btn3"/>
<mx:Button x="131" y="373" label="Paint" click="TestPaint()"/>
<mx:Label x="176" y="169" text="LblMiddle" width="71" height="20" id="lbl"/>
<mx:Button x="303" y="324" label="Move" click="Testmove()"/>
</mx:Application>
public var lines1 :flash.display.Sprite = new Sprite (); //
lines1.graphics.lineStyle(2,0x0099ff,1);// 设置颜色 粗细
lines1.graphics.moveTo(uFrom.x+uFrom.width/2,uFrom.y+uFrom.height/2); //从某个坐标开始
lines1.graphics.lineTo(uTo.x+uTo.width/2,uTo.y+uTo.height/2); //画到某个坐标
//因为 addChild方法 不能识别 非继承mc.core.UIComponent的控件 所以先声明一个组件 把控件放进去然后在放进界面中
var comp: UIComponent = new UIComponent();
comp.addChild(lines);
addChild(comp);