效果如下
首先是源代碼,保存為main.as文件,關聯到Fla即可運行。
代碼如下package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.geom.Matrix;
[SWF(width=800, height=465,backgroundColor=0xffffff, frameRate=24)]
public class main extends Sprite {
private var pointNum:Number=0;
private var pTxt:TextField = addChild(new TextField()) as TextField;
private var matrix:Matrix = new Matrix();
public function main(){
var t:Timer = new Timer(500);
var tFun=function(){matrix.createGradientBox(800,465,Math.PI/2,0,0),graphics.beginGradientFill(GradientType.LINEAR,[0x00CCFF,0xFFFFFF],[0.3,1],[0,255],matrix),graphics.drawRect(0,0,800,465),t.addEventListener(TimerEvent.TIMER,function(e){var mc;
var mcFun = function(){mc=addChild(new balloon(Math.random()*10 + 1)),mc.x=Math.round(Math.random()*(700-30))+30,mc.y=465}.apply();
mc.addEventListener(MouseEvent.MOUSE_DOWN, function(e){removeChild(e.target.parent),pTxt.htmlText='<font size="20" color="#ff0000">Score:'+(++pointNum).toString()+'</font>'});}),t.start()}.apply();
}
}
}
import flash.display.Sprite;
import flash.events.Event;
class balloon extends Sprite{
private var speed:Number;
private var s:Sprite =addChild(new Sprite()) as Sprite;
public function balloon(_t){
var sFun=function(){s.graphics.lineStyle(1, 0),s.graphics.curveTo(-10, 30, 0, 60),s.graphics.beginFill(Math.random() * 0xffffff + 0xff000000),s.graphics.drawCircle(0,0,20),speed=Math.round(_t)}.apply();
s.addEventListener(Event.ENTER_FRAME,function(e){s.y-= speed;});
}
}
這段代碼,以}占一行,帶分號;的語句占一行,咱不搞一行寫N句的小聰明。
這段代碼,有些地方寫的不規范,比如用了匿名函數,而且對效率沒做優化(對象池),但是,作為一個完整的游戲,它已經可以跑起來了,去掉import那些,核心代碼也就20行。
雖然只有短短的30行代碼,但是我們用到了類的知識,對象,漸變,矩陣,事件處理,匿名函數,隨機運算,以及graphic繪圖等,可謂麻雀雖小五髒俱全哇