最近折騰AS3.0的學習,做了一個小練習,收藏下,哈哈。
下面附一個滑鼠點選FLASH,舞台變換各種顔色的代碼:
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.display.Shape;
//為舞台stage注冊滑鼠點選事件
stage.addEventListener(MouseEvent.MOUSE_DOWN,changeColor);
function changeColor(e:MouseEvent):void{
//每次點選時重新生成一個正方形
var newShape:Shape=createSquare();
addChild(newShape);
function createSquare():Shape{
var squareSize:Number=200;
var square:Shape=new Shape();
square.graphics.beginFill(Math.random()*0XFFFFF);
square.graphics.drawRect(0,0,squareSize,squareSize);
return square;
}
}
定時器(每秒種列印出目前顯示是第幾秒,停止也會列印):
import flash.utils.Timer;
import flash.events.TimerEvent;
//建立事件偵聽器
var mTimer:Timer=new Timer(1000,10);
function tickHandler(event:TimerEvent):void{
trace("現在是第"+event.target.currentCount+"秒");
function timeComplete(event:TimerEvent):void{
trace("計時停止");
//注冊事件偵聽器
function reLis():void{
mTimer.addEventListener(TimerEvent.TIMER,tickHandler);
mTimer.addEventListener(TimerEvent.TIMER_COMPLETE,timeComplete);
//啟動定時器
mTimer.start();
reLis();
//登出事件偵聽器
//mTimer.removeEventListener(TimerEvent.TIMER,tickHandler);
本文轉自shyy8712872 51CTO部落格,原文連結:http://blog.51cto.com/shuyangyang/900878,如需轉載請自行聯系原作者