手机
当前位置:查字典教程网 >网页设计 >Flash教程 >Flash AS3教程:小游戏开发实战尝试
Flash AS3教程:小游戏开发实战尝试
摘要:前面讲解了FlashAS3教程:Direction类和Dot类,前面都是理论的讲解,这篇来一个实战,做一个类似坦克游戏的程序。http://...

前面讲解了Flash AS3教程:Direction类和Dot类,前面都是理论的讲解,这篇来一个实战,做一个类似坦克游戏的程序。

http://www.jb51.net/files/media/dirtanke.swf

这几天也写了一些类了,是驴子还是骡子,拿出来遛一遛就知道了,先看这个上面这个flash动画!

一个类似坦克游戏的demo程序

使用Direction类来进行方向控制

使用Dot类来计算距离

用上Direction类和Dot类之后,这个demo程序变得异常简单额。。

也没什么好说,主要透过这个例子,让大家类熟悉Direction类和Dot类的使用方法

不懂的可以在后面跟帖提问,高手如果看到什么有错误的地方,请指正出来,多谢指教

下面是fla的源代码:

CODE:

import index.base.game.Direction;

import index.base.events.DirectionEvent;

import index.base.geom.Dot;

//舞台属性设置

stage.showDefaultContextMenu = false;

stage.align = "TL";

stage.scaleMode = "noScale";

//创建坦克

var tank:Tank = new Tank;

tank.x = tank.y = 250;

addChild(tank);

//创建绑定坦克的点

var dot:Dot = new Dot;

dot.bind(tank);

//坦克移动

var dirTank:Direction = new Direction(stage);

//炮台转动

var dirTower:Direction = new Direction(stage,true,87,83,65,68);

//坦克炮台事件

dirTank.addEventListener(DirectionEvent.DO,doTankFun);

dirTower.addEventListener(DirectionEvent.DO,doTowerFun);

//坦克移动

function doTankFun(e:DirectionEvent):void{

if(e.up){

dot.go(2,true);

}

if(e.down){

dot.go(-2,true);

}

if(e.left){

tank.rotation -= 2;

}

if(e.right){

tank.rotation = 2;

}

if(tank.x < 0) tank.x = 0;

if(tank.y < 0) tank.y = 0;

if(tank.x > stage.stageWidth) tank.x = stage.stageWidth;

if(tank.y > stage.stageHeight) tank.y = stage.stageHeight;

}

//是否可以发射炮台,子弹

var isBullet:Boolean = true;

var isShell:Boolean = true;

//炮台发射转动

function doTowerFun(e:DirectionEvent):void{

if(e.up && isBullet){

var bullet:Bullet = new Bullet;

bullet.x = tank.x;

bullet.y = tank.y;

bullet.rotation = tank.rotation tank.tower.rotation;

bullet.addEventListener(Event.ENTER_FRAME,bulletFun);

addChild(bullet);

isBullet = false;

setTimeout(function(){isBullet = true},200);

}

if(e.down && isShell){

var shell:Shell = new Shell;

shell.x = tank.x;

shell.y = tank.y;

shell.rotation = tank.rotation;

shell.addEventListener(Event.ENTER_FRAME,shellFun);

addChild(shell);

isShell = false;

setTimeout(function(){isShell = true},500);

}

if(e.left){

tank.tower.rotation -= 5;

}

if(e.right){

tank.tower.rotation = 5;

}

}

//炮台

function shellFun(e:Event):void{

var tmp:Shell = e.currentTarget as Shell;

var d:Dot = new Dot(tmp.x,tmp.y,tmp.rotation);

d.bind(tmp);

d.go(4,true);

if(tmp.x < 0 || tmp.x > stage.stageWidth || tmp.y < 0 || tmp.y > stage.stageHeight){

removeChild(tmp);

tmp.removeEventListener(Event.ENTER_FRAME,shellFun);

}

tmp = null;

d = null;

}

//子弹

function bulletFun(e:Event):void{

var tmp:Bullet = e.currentTarget as Bullet;

var d:Dot = new Dot(tmp.x,tmp.y,tmp.rotation);

d.bind(tmp);

d.go(5,true);

if(tmp.x < 0 || tmp.x > stage.stageWidth || tmp.y < 0 || tmp.y > stage.stageHeight){

removeChild(tmp);

tmp.removeEventListener(Event.ENTER_FRAME,bulletFun);

}

tmp = null;

d = null;

}另外注意源代码,有个地方多次对tank的tower属性就行引用,并且返回他的x,y或者旋转值,有人就会问了,as3不是不支持类似mc那样的直接访问显示对象,为什么我这儿却可以?

愿意是我把素材绑定在Tank类上,并且对Tank类做了以下编写:

CODE:

package{

import flash.display.Sprite;

public class Tank extends Sprite{

public function Tank(){

}

public function get tower():Sprite{

return towerMc;

}

}

}光看这个类,也许你还是不明白,是什么原因,为什么会多出来一个towerMc出来,详细的原因,请自己下载提供的源文件,下载下来看看吧。。不懂跟帖问!

点击下载源文件

【Flash AS3教程:小游戏开发实战尝试】相关文章:

Flash游戏开发实例制作旋转坦克

Flash AS 教程:交互动画

Flash教程:互动的橡皮刷

Flash教程:卡通人物基本五官造型和绘制方法

Flash教程:模拟幸运号抽奖机

Flash AS 教程:类和面向对象编程

Flash AS教程:制作彩色线条动画

Flash AS3教程:Random类

Flash AS2教程:鼠标感应放大缩小图片效果

Flash CS4教程:制作切换扑克牌效果

精品推荐
分类导航