当前位置: 首页 > 图文教程 > Flash动画 > ActionScript > Flash AS3教程:小游戏开发实战尝试

ActionScript
FLASH 3D相册之利用BitmapData类制作
Flash 脚本游戏开发教程 第一课
Flash 脚本游戏开发教程第二课
Flash 脚本游戏开发教程第三课
Flash 脚本游戏开发教程第四课
Flash 脚本游戏开发教程第五课
Flash 脚本游戏开发教程第六课
Flash 脚本游戏开发教程第七课
Flash 脚本游戏开发教程第八课
Flash AS实现的蝌蚪摆尾动画的教程
从基础开始深入学Flash AS3教程(4)(译文)
从基础开始深入学Flash AS3教程(5)(译文)
从基础开始深入Flash AS3教程(2)(译文)
从基础开始深入学Flash AS3教程(3)(译文)
Flash AS3对单个图片进行角色动作化处理
从基础开始深入Flash AS3教程(1)(译文)
Flash教程:if条件语句的用法
Flash AS教程:_visible属性的详细讲解
Flash AS教程:图片环绕旋转效
Flash教程:trace()的使用

ActionScript 中的 Flash AS3教程:小游戏开发实战尝试


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-16   浏览: 163 ::
收藏到网摘: n/a

前面讲解了Flash AS3教程:Direction类和Dot类,前面都是理论的讲解,这篇来一个实战,做一个类似坦克游戏的程序。
/upload/tech/20090916/20090916063214_ffeabd223de0d4eacb9a3e6e53e5448d.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出来,详细的原因,请自己下载提供的源文件,下载下来看看吧。。不懂跟帖问!
点击下载源文件