Flash教程:用纯as3脚本实现飞机类型游戏
作者 佚名技术
来源 服务器技术
浏览
发布时间 2012-07-07
当游戏结束时点击左上角xxms重新开始游戏,有2个类,一个是自己,一个是敌人。自己类取名Ball1.as: package{//ball1类是玩家操作的ball importFlash.display.*; importflash.events.*; importflash.text.*; importflash.utils.*; publicclassBall1extendsSprite{ publicvarlable:TextField=newTextField(); publicvarspeed:int=60; publicvarstage1:Stage; publicvarballList:Array=newArray(); publicvartimer:Timer=newTimer(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:int=speed*(stage1.mouseX-x)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x)); varspeedY:int=speed*(stage1.mouseY-y)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x)); varx1:int; vary1:int; if(Math.abs(speedX)<Math.abs(stage1.mouseX-x)){ x1=x+speedX; }else{ x1=stage1.mouseX; } if(Math.abs(speedY)<Math.abs(stage1.mouseY-y)){ y1=y+speedY; }else{ y1=stage1.mouseY; } if(x1<0){ x=0; }elseif(x1>stage.stageWidth){ x=stage.stageWidth; }else{ x=x1; } if(y1<0){ y=0; }elseif(y1>stage.stageHeight){ y=stage.stageHeight; }else{ y=y1; } } publicfunctionenterFrameHandler2(e:Event):void{//判断游戏是否结束 for(vari:int;i<ballList.length;i++){ if(Math.sqrt((x-ballList.x)*(x-ballList.x)+(y-ballList.y)*(y-ballList.y))<15){ timer.reset(); for(varj:int;j<ballList.length;j++){ ballList[j].stopRun(); } lable.addEventListener(MouseEvent.CLICK,clickHandler); break; } } } publicfunctionclickHandler(e:MouseEvent):void{//控制游戏开始 for(varj:int;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.*; publicclassBall2extendsSprite{ publicvarball1:Sprite; publicvarspeed:int=30; publicvarspeedX:int=0; publicvarspeedY:int=0; publicfunctionBall2(ball1:Sprite){ this.ball1=ball1; init(); initXy(); startRun(); } publicfunctioninit():void{/ |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于Flash教程:用纯as3脚本实现飞机类型游戏的所有评论