用纯Flash as3脚本实现飞机类型游戏
2009-01-04 19:17:32 来源:WEB开发网核心提示:当游戏结束时点击左上角xxms重新开始游戏,有2个类,用纯Flash as3脚本实现飞机类型游戏,一个是自己,一个是敌人
当游戏结束时点击左上角xxms重新开始游戏,有2个类,一个是自己,一个是敌人。自己类取名Ball1.as:
package {//ball1类是玩家操作的ball
import Flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
public class Ball1 extends SPRite {
public var lable:TextField=new TextField();
public var speed:int=60;
public var stage1:Stage;
public var ballList:Array=new Array();
public var timer:Timer=new Timer(1);
public function Ball1(stage1:Stage) {
this.stage1=stage1;
init();
}
public function init():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);
}
public function timerHandler(e:TimerEvent):void{
lable.text=""+e.target.currentCount+"ms";
}
public function mouseDownHandler(e:Event):void {
stage1.addEventListener(Event.ENTER_FRAME,enterFrameHandler1);
stage1.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
public function mouseUpHandler(e:Event):void {
stage1.removeEventListener(Event.ENTER_FRAME,enterFrameHandler1);
stage1.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
public function enterFrameHandler1(e:Event) {//控制ball的运动
var speedX:int=speed*(stage1.mouseX-x)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x));
var speedY:int=speed*(stage1.mouseY-y)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x));
var x1:int;
var y1: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;
} else if (x1>stage.stageWidth) {
x=stage.stageWidth;
} else {
x=x1;
}
if (y1<0) {
y=0;
} else if (y1>stage.stageHeight) {
y=stage.stageHeight;
} else {
y=y1;
}
}
public function enterFrameHandler2(e:Event):void {//判断游戏是否结束
for (var i:int; i<ballList.length; i++) {
if (Math.sqrt((x-ballList.x)*(x-ballList.x)+(y-ballList.y)*(y-ballList.y))<15) {
timer.reset();
for (var j:int; j<ballList.length; j++) {
ballList[j].stopRun();
}
lable.addEventListener(MouseEvent.CLICK,clickHandler);
break;
}
}
}
public function clickHandler(e:MouseEvent):void {//控制游戏开始
for (var j: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类,可以用作敌人的子弹
import flash.display.*;
import flash.events.*;
public class Ball2 extends Sprite {
public var ball1:Sprite;
public var speed:int=30;
public var speedX:int=0;
public var speedY:int=0;
public function Ball2(ball1:Sprite) {
this.ball1=ball1;
init();
initXy();
startRun();
}
public function init():void {//初始化
graphics.lineStyle(1,0);
graphics.drawCircle(0,0,5);
}
public function enterFrameHandler(e:Event){//控制子弹的运动
if(Math.random()<0.1){
var d:int=Math.sqrt((ball1.x-x)*(ball1.x-x)+(ball1.y-y)*(ball1.y-y));
if(d==0){d=1};
speedX=speed*(ball1.x-x)/d;
speedY=speed*(ball1.y-y)/d;
}
x+=speedX;
y+=speedY;
}
public function startRun(){
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
public function stopRun(){
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
public function initXy():void {//初始化子弹开始的位置
var xy:Number=Math.random();
if (xy<0.25) {
x=xy*550*4;
y=-10;
} else if (xy<0.5) {
x=560;
y=(xy-0.25)*400*4;
} else if (xy<0.75) {
x=(xy-0.5)*550*4;
y=410;
} else {
x=-10;
y=(xy-0.75)*400*4;
}
}
}
}然后用一个fla文件把2个类初始化:
import flash.text.*;
import flash.net.*;
var ball:Ball1=new Ball1(stage);//定义ball1
var ballList:Array=new Array();//定义balllist数组
for (var i:int=0; i<20; i++) {//实例化balllist
ballList.push(new Ball2(ball));
addChild(ballList);
ball.ballList=ballList;
}
addChild(ball);
var lable:TextField=new TextField();
lable.text="sliz";
lable.x=530;
lable.y=380;
addChild(lable);
lable.addEventListener( MouseEvent.CLICK , clickHandler );
function clickHandler( event:MouseEvent ):void {
navigateToURL( new URLRequest ("http://space.flash8.net/space/?534614" ) );
}minigame:minigame.rar
package {//ball1类是玩家操作的ball
import Flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
public class Ball1 extends SPRite {
public var lable:TextField=new TextField();
public var speed:int=60;
public var stage1:Stage;
public var ballList:Array=new Array();
public var timer:Timer=new Timer(1);
public function Ball1(stage1:Stage) {
this.stage1=stage1;
init();
}
public function init():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);
}
public function timerHandler(e:TimerEvent):void{
lable.text=""+e.target.currentCount+"ms";
}
public function mouseDownHandler(e:Event):void {
stage1.addEventListener(Event.ENTER_FRAME,enterFrameHandler1);
stage1.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
public function mouseUpHandler(e:Event):void {
stage1.removeEventListener(Event.ENTER_FRAME,enterFrameHandler1);
stage1.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
public function enterFrameHandler1(e:Event) {//控制ball的运动
var speedX:int=speed*(stage1.mouseX-x)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x));
var speedY:int=speed*(stage1.mouseY-y)/Math.sqrt((stage1.mouseY-y)*(stage1.mouseY-y)+(stage1.mouseX-x)*(stage1.mouseX-x));
var x1:int;
var y1: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;
} else if (x1>stage.stageWidth) {
x=stage.stageWidth;
} else {
x=x1;
}
if (y1<0) {
y=0;
} else if (y1>stage.stageHeight) {
y=stage.stageHeight;
} else {
y=y1;
}
}
public function enterFrameHandler2(e:Event):void {//判断游戏是否结束
for (var i:int; i<ballList.length; i++) {
if (Math.sqrt((x-ballList.x)*(x-ballList.x)+(y-ballList.y)*(y-ballList.y))<15) {
timer.reset();
for (var j:int; j<ballList.length; j++) {
ballList[j].stopRun();
}
lable.addEventListener(MouseEvent.CLICK,clickHandler);
break;
}
}
}
public function clickHandler(e:MouseEvent):void {//控制游戏开始
for (var j: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类,可以用作敌人的子弹
import flash.display.*;
import flash.events.*;
public class Ball2 extends Sprite {
public var ball1:Sprite;
public var speed:int=30;
public var speedX:int=0;
public var speedY:int=0;
public function Ball2(ball1:Sprite) {
this.ball1=ball1;
init();
initXy();
startRun();
}
public function init():void {//初始化
graphics.lineStyle(1,0);
graphics.drawCircle(0,0,5);
}
public function enterFrameHandler(e:Event){//控制子弹的运动
if(Math.random()<0.1){
var d:int=Math.sqrt((ball1.x-x)*(ball1.x-x)+(ball1.y-y)*(ball1.y-y));
if(d==0){d=1};
speedX=speed*(ball1.x-x)/d;
speedY=speed*(ball1.y-y)/d;
}
x+=speedX;
y+=speedY;
}
public function startRun(){
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
public function stopRun(){
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
public function initXy():void {//初始化子弹开始的位置
var xy:Number=Math.random();
if (xy<0.25) {
x=xy*550*4;
y=-10;
} else if (xy<0.5) {
x=560;
y=(xy-0.25)*400*4;
} else if (xy<0.75) {
x=(xy-0.5)*550*4;
y=410;
} else {
x=-10;
y=(xy-0.75)*400*4;
}
}
}
}然后用一个fla文件把2个类初始化:
import flash.text.*;
import flash.net.*;
var ball:Ball1=new Ball1(stage);//定义ball1
var ballList:Array=new Array();//定义balllist数组
for (var i:int=0; i<20; i++) {//实例化balllist
ballList.push(new Ball2(ball));
addChild(ballList);
ball.ballList=ballList;
}
addChild(ball);
var lable:TextField=new TextField();
lable.text="sliz";
lable.x=530;
lable.y=380;
addChild(lable);
lable.addEventListener( MouseEvent.CLICK , clickHandler );
function clickHandler( event:MouseEvent ):void {
navigateToURL( new URLRequest ("http://space.flash8.net/space/?534614" ) );
}minigame:minigame.rar
- ››asp.net页面弄成伪静态页面
- ››Asp.net 中将汉字转换成拼音的方法
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
更多精彩
赞助商链接