flashas3:用纯Flash as3脚本实现飞机类型游戏

  当游戏结束时点击左上角xxms重新开始游戏有2个类个是自己个是敌人自己类取名Ball1.as:

package{//ball1类是玩家操作ball
importflash.display.*;
importflash.events.*;
importflash.text.*;
importflash.utils.*;
publicBall1extendsSprite{
publicvarlable:TextField=TextField;
publicvarspeed:=60;
publicvarstage1:Stage;
publicvarballList:Array=Array;
publicvartimer:Timer=Timer(1);
publicfunctionBall1(stage1:Stage){
this.stage1=stage1;
init;
}
publicfunctioninit:void{//
stage1.addChild(lable);
graphics.lineStyle(1,0);
graphics.drawCircle(0,0,10);
x=stage1.stageWidth/2;
y=stage1.stageHeight/2;
stage1.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
stage1.addEventListener(Event.ENTER_FRAME,enterFrameHandler2);
timer.start;
timer.addEventListener(TimerEvent.TIMER,timerHandler);
}
publicfunctiontimerHandler(e:TimerEvent):void{
lable.text=""+e.target.currentCount+"ms";
}
publicfunctionmouseDownHandler(e:Event):void{
stage1.addEventListener(Event.ENTER_FRAME,enterFrameHandler1);
stage1.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
publicfunctionmouseUpHandler(e:Event):void{
stage1.removeEventListener(Event.ENTER_FRAME,enterFrameHandler1);
stage1.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
publicfunctionenterFrameHandler1(e:Event){//控制ball运动
varspeedX:=speed*(stage1.mouseX-x)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x));
varspeedY:=speed*(stage1.mouseY-y)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x));
varx1:;
vary1:;
(Math.abs(speedX)<Math.abs(stage1.mouseX-x)){
x1=x+speedX;
}{
x1=stage1.mouseX;
}
(Math.abs(speedY)<Math.abs(stage1.mouseY-y)){
y1=y+speedY;
}{
y1=stage1.mouseY;
}
(x1<0){
x=0;
}(x1>stage.stageWidth){
x=stage.stageWidth;
}{
x=x1;
}
(y1<0){
y=0;
}(y1>stage.stageHeight){
y=stage.stageHeight;
}{
y=y1;
}
}
publicfunctionenterFrameHandler2(e:Event):void{//判断游戏是否结束
for(vari:;i<ballList.length;i){
(Math.sqrt((x-ballList.x)*(x-ballList.x)+(y-ballList.y)*(y-ballList.y))<15){
timer.re;
for(varj:;j<ballList.length;j){
ballList[j].stopRun;
}
lable.addEventListener(MouseEvent.CLICK,clickHandler);
;
}
}
}
publicfunctionclickHandler(e:MouseEvent):void{//控制游戏开始
for(varj:;j<ballList.length;j){
ballList[j].initXy;
ballList[j].startRun;
}
lable.text="";
x=stage1.stageWidth/2;
y=stage1.stageHeight/2;
timer.start;
lable.removeEventListener(MouseEvent.CLICK,clickHandler);
}
}
}


  敌人类Ball2.as:

package{//ball2类可以用作敌人子弹
importflash.display.*;
importflash.events.*;
publicBall2extendsSprite{
publicvarball1:Sprite;
publicvarspeed:=30;
publicvarspeedX:=0;
publicvarspeedY:=0;
publicfunctionBall2(ball1:Sprite){
this.ball1=ball1;
init;
initXy;
startRun;
}
publicfunctioninit:void{//
graphics.lineStyle(1,0);
graphics.drawCircle(0,0,5);
}
publicfunctionenterFrameHandler(e:Event){//控制子弹运动
(Math.random<0.1){
vard:=Math.sqrt((ball1.x-x)*(ball1.x-x)+(ball1.y-y)*(ball1.y-y));
(d0){d=1};
speedX=speed*(ball1.x-x)/d;
speedY=speed*(ball1.y-y)/d;
}
xspeedX;
yspeedY;
}
publicfunctionstartRun{
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
publicfunctionstopRun{
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
publicfunctioninitXy:void{//化子弹开始位置
varxy:Number=Math.random;
(xy<0.25){
x=xy*550*4;
y=-10;
}(xy<0.5){
x=560;
y=(xy-0.25)*400*4;
}(xy<0.75){
x=(xy-0.5)*550*4;
y=410;
}{
x=-10;
y=(xy-0.75)*400*4;
}
}
}
}


  然后用个fla文件把2个类化:

importflash.text.*;
importflash.net.*;
varball:Ball1=Ball1(stage);//定义ball1
varballList:Array=Array;//定义balllist
for(vari:=0;i<20;i){//例子化balllist
ballList.push(Ball2(ball));
addChild(ballList);
ball.ballList=ballList;
}
addChild(ball);
varlable:TextField=TextField;
lable.text="sliz";
lable.x=530;
lable.y=380;
addChild(lable);
lable.addEventListener(MouseEvent.CLICK,clickHandler);
functionclickHandler(event:MouseEvent):void{
navigateToURL(URLRequest("http://space.flash8.net/space/?534614"));
}


Tags:  flashcs4as3 flashas3编程 flashas3教程 flashas3

延伸阅读

最新评论

发表评论