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

ActionScript
Flash as与影片剪辑的调用
Flash AS2教程:缓冲运动
flash as2的缓动类
FLASH AS教程:ActionScript3简介
Flash AS3教程:编程基础
ActionScript和网页JavaScript互相调用
Flash ActinScript 3.0基础知识
ActionScript 3.0的流程控制语法
ActinScript3.0开发基础知识:可重用代码
Flash自定义函数实例
ActoinScript教程:小数坐标处理
AS教程:鼠标拖拽与旋转MC
Flash AS3基础教程:掌握鼠标事件应用
Flash AS3教程:快速制作烟雾动画
Flash AS代码书写约定和书写习惯
学习Flex:常见的Flex代码
Flex教程:ScriptLimits标签
Flash AS教程:propertydata问题解决
Flash AS3单列模式实现方式
as3中通过ByteArray进行对象的深度拷贝

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


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