Flash ActionScript2.0是一种面向对向的编程语言,利用它可以开发出功能强大的应用程序.以早期的ActionScript 1.0相比,它在结构化编程上具有明显的优势,可以使程序具有更强的可移植性,可重性,和伸缩性.
本文通过一个推箱子游戏的开发过程来逐步讲述如何利用ActionScript2.0 进行开发.
在进行一个项目的开发前,项目规化是必不可少的.我们先来思考一下推箱子游戏包含哪些必要的元素呢?
我们都需要将这些元素抽象化为对象(Object),各个对象包含有各自特有的属性和方法.比如,玩家具有位置属性(currentPos),它表示玩家当全的位置,玩家还有move方法,它使玩家移到下一个位置.等腰三角形这些问题搞清楚了,我们就要进行具体的实现.
var _stepSize:Number;//步长
var _wall:MovieClip;//墙壁电影剪彩辑
var startPos:Pos;//初始置
var currentPos:Pos;//当前位置
var left:Number;//左边距
var top:Number;//上边距
var dir:Number;//移动方向
var boxes:Array;
var player_path_stack:Array;//玩家移动的路线
var boxes_path_stack:Array;//箱子移动的路线
//构造函数,初始化
function Player() {
_stepSize = 40;
currentPos = null;
left = 0;
top = 0;
dir = 0;
player_path_stack=new Array();
boxes_path_stack=new Array();
boxes = new Array();
}
//设置玩家的起始位置
function setStartPos(p) {
startPos = new Pos(p.col, p.row);
currentPos = startPos;
this._x = currentPos.col*_stepSize+left;
this._y = currentPos.row*_stepSize+top;
}
//为玩家指定一个wall实例
function setWall(w) {
_wall = w;
}
//为玩家指定一个boxes数组,存有所有的箱子实例
function setBoxes(b) {
boxes = b;
}
//通过按键控制玩家
function setKeyHandle() {
this.onKeyDown = function() {
if (Key.isDown(Key.LEFT)) {
dir = 2;//左键
} else if (Key.isDown(Key.RIGHT)) {
dir = 0;//右键
} else if (Key.isDown(Key.UP)) {
dir = 3;//上键
} else if (Key.isDown(Key.DOWN)) {
dir = 1;//下键
}
var nextObject = this.getNextObject(); //取得玩家下一个位置上的物体
if (nextObject == "BOX") { //玩家下一个位置上是个箱子
var box_pushed = getPushedBox();//取得此位置上的这个箱子
//被推到的箱子存在
if (box_pushed) {
if (box_pushed.getNextObject() == "NOTHING") {//被推箱子的下个位置没有障碍
boxes_path_stack.push({box:box_pushed,pos:new Pos(box_pushed.pos.col,box_pushed.pos.row)});
box_pushed.move();//被推箱子移一步
this.move();//玩家移一步
}
}
} else if (nextObject == "NOTHING") {//玩家的下个位置玩障碍
this.move();
boxes_path_stack.push(null);
}
};
Key.addListener(this); //监听按键输入
}
//取得被推到的箱子
function getPushedBox() {
var nextPos:Pos;
nextPos =currentPos.getNextPos(dir);
for (var i = 0; i<boxes.length; i++) {
if (boxes[i].pos.equals(nextPos)) {
return boxes[i];
break;
//trace(box_pushed);
}
}
return null;
}
//取得玩家下个位置上的物块类型( 墙壁, 箱子, 无障碍)
function getNextObject():String {
//var obj:String;
var nextPos:Pos;
nextPos = currentPos.getNextPos(dir);
if (_wall.brickMatrix[nextPos.row][nextPos.col] == 1) {
return "WALL";
}
for (var i = 0; i<boxes.length; i++) {
if (boxes[i].pos.equals(nextPos)) {
return "BOX";
}
}
return "NOTHING";
}
//移到下个位置
function move() {
player_path_stack.push(currentPos);
var c=currentPos.getNextPos(dir);
currentPos=c;
this._x = left+this.currentPos.col*_stepSize;
this._y = top+this.currentPos.row*_stepSize;
}
//返回上一步
function reback(){
if(player_path_stack.length>0){
var pre=player_path_stack.pop();
currentPos=pre;
this._x = left+this.currentPos.col*_stepSize;
this._y = top+this.currentPos.row*_stepSize;
}
if(boxes_path_stack.length>0 ){
var obj=boxes_path_stack.pop();
if(obj!=null){
var box=obj.box;
var pos=obj.pos;
box.pos=pos;
box._x=box.left+box.pos.col*box._stepSize;
box._y=box.top+box.pos.row*box._stepSize;
}
}
}
}
//1 2 3 4 5 6 7 8 9
matrixs[3]= [[1, 1, 1, 1, 1, 1, 1, 1, 0],//1
[1, 0, 0, 0, 1, 0, 0, 1, 0],//2
[1, 0, 0, 0, 0, 0, 0, 1, 0],//3
[1, 1, 3, 1, 1, 0, 0, 1, 0],//4
[0, 1, 0, 4, 1, 0, 1, 1, 1],//5
[0, 1, 0, 0, 0, 0, 2, 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[0, 0, 1, 4, 3, 3, 2, 1, 0],//5
[0, 0, 1, 0, 0, 4, 0, 1, 0],//6
[0, 0, 1, 0, 0, 1, 1, 1, 0],//7
[0, 0, 1, 1, 1, 1, 0, 0, 0],//8
[0, 0, 0, 0, 0, 0, 0, 0, 0] //9
];
}
function getMatrix(d){
return matrixs[d];
}
}
示例:
点击这里下载源文件
如果你还有任何不明白之处,可发邮件或QQ给我.xiake1860@ncepubj.edu.cn QQ:4077130