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

ActionScript
FLASH AS3与网页JS参数值传递的问题
Flash AS3的parameters对象处理网页参数
Flash教程 认识Flash ActionScript的环境
Flash ActionScript编程基础
Flash AS3代码实现鼠标跟随喷枪涂鸦效果
falsh 跨域调用配置
Flash AS3.0 实例教程 喷泉动画特效
AS3 Loader与URLLoader的比较
ColorTransform类调整显示对象的颜色值
Flash AS3 快速制作烟雾动画
Flash AS3 制作文字飞出动画
ActionScript 学习小心得
ActionScript3.0读取网页FlashVars中的参数的问题
通过实例学习AS3.0:案例三
通过实例学习Flash AS3.0:案例四
通过实例学习Flash AS3.0:案例五
通过实例学习Flash AS3.0:案例六
Flash教程:认识Flash ActionScript的环境
Flash as入门(1):认识AS面板
Flash as入门(3):AS基本语法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-16   浏览: 167 ::
收藏到网摘: 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出来,详细的原因,请自己下载提供的源文件,下载下来看看吧。。不懂跟帖问!
点击下载源文件