as3游戏编程视频教程

2015年03月11日 14:22 0 点赞 0 评论 更新于 2025-11-21 16:52

在之前的讲解中,我们介绍了Flash AS3教程里的Direction类和Dot类,那些内容主要侧重于理论方面。在本篇文章中,我们将进行一次实战,开发一个类似坦克游戏的程序。

这几天我们已经编写了一些类,正所谓“是驴子是骡子,拉出来遛遛”。我们先来看看下面这个Flash动画,它展示了一个类似坦克游戏的demo程序。

这个demo程序具有以下特点:

  • 使用Direction类来进行方向控制。
  • 使用Dot类来计算距离。

在运用了Direction类和Dot类之后,这个demo程序的实现变得异常简单。本篇文章的主要目的是通过这个例子,让大家熟悉Direction类和Dot类的使用方法。

如果大家有任何疑问,可以在文章后面跟帖提问。如果有高手发现文章中有错误的地方,也请不吝指正,多谢!

下面是该程序的Fla源代码:

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;
}

另外,在源代码中,有个地方多次对tanktower属性进行引用,并获取其xy坐标或者旋转值。可能有人会问,AS3不是不支持类似mc那样直接访问显示对象吗,为什么这里却可以呢?原因是我把素材绑定在Tank类上,并且对Tank类做了如下编写:

package {
import flash.display.Sprite;

public class Tank extends Sprite {
public function Tank() {
}

public function get tower():Sprite {
return towerMc;
}
}
}

作者信息

boke

boke

共发布了 3994 篇文章