聲明: 本文并非教程,充其量是個(gè)示例,
直線繪制方法之“線隨鼠動(dòng)”
。讀者如遇不解之處請(qǐng)盡量參考幫助手冊(cè)(F1)自行解決!
當(dāng)然對(duì)于回帖中提問(wèn)都會(huì)一一解答。
在閱讀代碼時(shí),請(qǐng)注意理解以下要點(diǎn):
1 偵聽(tīng)事件改為了onMouseMove
2 數(shù)組對(duì)象的應(yīng)用
點(diǎn)這里下載演示效果文件
將以下代碼粘貼到第一幀即可:
注:所示代碼符合ActionScript. 2.0編碼標(biāo)準(zhǔn),已在Macromedia Flash MX Professional 2004(Version 7.2)上調(diào)試成功!
// 功能:在給定影片上,根據(jù)起止點(diǎn)坐標(biāo)畫(huà)
function drawLine(drawingTable_mc:MovieClip, startPointX:Number, startPointY:Number, endPointX:Number, endPointY:Number, lineStyle_array:Array) {
with (drawingTable_mc) {
// 設(shè)置線型
lineStyle(lineStyle_array[0], lineStyle_array, lineStyle_array);
// 設(shè)置起點(diǎn)、終點(diǎn)
moveTo(startPointX, startPointY);
lineTo(endPointX, endPointY);
}
}
//
// 舞臺(tái)中心點(diǎn)坐標(biāo)
var centerPointX:Number = Stage.width / 2;
var centerPointY:Number = Stage.height / 2;
// 設(shè)置基層,相對(duì)于_root而言,0-9作為預(yù)留層
var BASE_LAYER:Number = 10;
// 創(chuàng)建繪圖板
var temporary_mc:MovieClip = _root.createEmptyMovieClip("drawingTable", BASE_LAYER);
// 初始化計(jì)數(shù)器
var counter:Number = 0;
// 設(shè)置自動(dòng)繪制的線條總數(shù)
var LINE_TOTAL:Number = 10;
// 初始化起點(diǎn)坐標(biāo)數(shù)組
var startPointX_array:Array = new Array();
var startPointY_array:Array = new Array();
// 初始化線形數(shù)組
var lineStyle_array:Array = new Array();
// 自動(dòng)畫(huà)線
temporary_mc.onEnterFrame. = function() {
// 將各個(gè)線段的起點(diǎn)坐標(biāo)、線型參數(shù)保存至相應(yīng)的數(shù)組中
var lineNumber:Number = startPointX_array.push(random(Stage.width));
startPointY_array.push(random(Stage.height));
//:NOTE: 這里是數(shù)組嵌套
lineStyle_array.push([random(10), random(0xffffff), random(50) + 51]);
// 調(diào)用畫(huà)線函數(shù),并傳入相應(yīng)參數(shù)
drawLine(temporary_mc, centerPointX, centerPointY, startPointX_array[lineNumber1], startPointY_array[lineNumber1], lineStyle_array[lineNumber1]);
counter++;
if (counter == LINE_TOTAL) {
//trace(startPointX_array.length);
delete this.onEnterFrame;
}
};
// 創(chuàng)建鼠標(biāo)偵聽(tīng)器
mouseListener = new Object();
// 設(shè)置偵聽(tīng)事件及其處理函數(shù)
mouseListener.onMouseMove = function() {
temporary_mc.clear();
{
drawLine(temporary_mc, _root._xmouse, _root._ymouse, startPointX_array[i], startPointY_array[i], lineStyle_array[i]);
}
updateAfterEvent();
};
// 為鼠標(biāo)添加偵聽(tīng)器
Mouse.addListener(mouseListener);