当前位置: 移动技术网 > IT编程>UI设计>设计软件 > Flash AS代码实现智力过河小游戏

Flash AS代码实现智力过河小游戏

2019年03月20日  | 移动技术网IT编程  | 我要评论
本教程主要用Flash AS来编写一个智力过河小游戏,全部使用AS代码来实现,教程比较基础,转发过来,希望对大家有所帮助... 14-07-24

本教本教程主要用flash as来编写一个智力过河小游戏,全部使用as代码来实现,教程比较基础,转发过来,希望对大家有所帮助。

游戏规则:

一只大狮子,一只小狮子,一只大老虎、一只小老虎、一只大花豹,一只小花豹要到河对面去,只有大动物和小老虎会划船,船每次只能乘两只动物。大动物之间互相吃不了,小动物之间互相吃不了,如果大动物不在同类的小动物就会被异类大动物吃掉。

复制粘贴到第一帧,测试:


复制代码
代码如下:
创建开始场景();
function 创建开始场景() {
创建txt_mc(this, 游戏名, 智力过河, 200, 100, 300, false);
创建txt_mc(this, 游戏开始, 游戏开始, 250, 200, 100, true);
创建txt_mc(this, 游戏说明, 游戏说明, 250, 250, 100, true);
游戏开始.onpress = function() {
删除开始场景();
创建游戏场景();
};
游戏说明.onpress = function() {
删除开始场景();
创建说明场景();
};
}
function 删除开始场景() {
游戏名.removemovieclip();
游戏开始.removemovieclip();
游戏说明.removemovieclip();
}
function 删除说明场景() {
规则.removemovieclip();
说明.removemovieclip();
游戏开始.removemovieclip();
}
function 创建说明场景() {
var 内容 = 一只大狮子,一只小狮子,一只大老虎、一只小老虎、一只大花豹,+\n+一只小花豹要到河对面去,只有大动物和小老虎会划船,船每次只能乘+\n+两只动物。大动物之间互相吃不了,小动物之间互相吃不了,如果大动+\n+物不在同类的小动物就会被异类大动物吃掉。;
创建txt_mc(this, 规则, 游戏规则, 230, 50, 150, false);
创建txt_mc(this, 说明, 内容, 50, 100, 120, false);
创建txt_mc(this, 游戏开始, 游戏开始, 250, 200, 100, true);
游戏开始.onpress = function() {
删除说明场景();
创建游戏场景();
};
}
function 创建txt_mc(路径, 名称, 文本, 坐标x, 坐标y, 大小, 框) {
var mc = 路径.createemptymovieclip(名称, 路径.getnexthighestdepth());
mc._x = 坐标x;
mc._y = 坐标y;
mc._xscale = mc._yscale=大小;
var txt = mc.createtextfield(txt, 0, 0, 0, 0, 0);
txt.text = 文本;
txt.selectable = false;
txt.autosize = true;
if (框 == true) {
mc.onrollover = function() {
this.txt.border = true;
this.onreleaseoutside = this.onrollout=function () {
this.txt.border = false;
};
};
}
return mc;
}
this.createemptymovieclip(遮照mc, this.getnexthighestdepth());
function 创建游戏场景() {
var 船上成员 = [];
var 左岸 = [大狮子, 小狮子, 大老虎, 小老虎, 大花豹, 小花豹];
this.createemptymovieclip(左岸mc, this.getnexthighestdepth());
创建河流();
this.createemptymovieclip(码头mc, this.getnexthighestdepth());
画方块(码头mc, 0, 348, 94, 50, true);
画方块(码头mc, 454, 348, 94, 50, true);
this.createemptymovieclip(提示mc, this.getnexthighestdepth());
左岸mc._y = 100;
左岸mc._x = 20;
for (var i = 0; i<左岸.length; i++) {
if (i%2 == 0) {
var mc = 创建txt_mc(左岸mc, 左岸[i], 左岸[i], 0, 40*i, 100, true);
mc.名字 = mc._name;
mc.划船 = true;
mc.方位 = 左岸;
mc.大小 = 大;
} else {
var mc = 创建txt_mc(左岸mc, 左岸[i], 左岸[i], 0, 40*i, 100, true);
mc.名字 = mc._name;
mc.划船 = false;
mc.方位 = 左岸;
mc.大小 = 小;
}
mc.onpress = function() {
if (船上成员.length == 2) {
提示信息(船上不能再乘座更多的动物了);
}
if (船上成员.length<2 && 船mc.行动 == false && this.方位 == 船mc.状态) {
this._visible = false;
this.方位 = 船上;
船上成员.push(this);
eval(船仓+船上成员.length).txt.text = this.名字;
}
};
}
左岸mc[小老虎].划船 = true;
this.createemptymovieclip(船mc, this.getnexthighestdepth());
船mc._y = 360;
船mc._x = 100;
船mc.y = 0;
船mc.状态 = 左岸;
船mc.行动 = false;
船mc.速度 = 20;
画船(船mc);
创建txt_mc(船mc, txt, ===>>, 40, -2, 100, false);
船mc.onpress = function() {
if (this.行动 == false && 是否能划船(船上成员) && 能否呆船上(船上成员) && 岸上(this.状态)) {
this.行动 = true;
船移动(this);
}
};
创建txt_mc(this, 船仓1, , 船mc._x+35, 船mc._y-20, 100, true);
创建txt_mc(this, 船仓2, , 船mc._x+35, 船mc._y-40, 100, true);
船仓1.onpress = function() {
if (船mc.状态 == 左岸) {
左岸mc[this.txt.text]._visible = true;
左岸mc[this.txt.text]._x = 0;
左岸mc[this.txt.text].方位 = 左岸;
删除(船上成员, this.txt.text);
this.txt.text = 船仓2.txt.text;
船仓2.txt.text = ;
}
if (船mc.状态 == 右岸) {
左岸mc[this.txt.text]._visible = true;
左岸mc[this.txt.text]._x = 480;
左岸mc[this.txt.text].方位 = 右岸;
删除(船上成员, this.txt.text);
this.txt.text = 船仓2.txt.text;
船仓2.txt.text = ;
var n = 0;
for (var i in 左岸mc) {
if (左岸mc[i].方位 == 右岸) {
n++;
if (n == 6) {
提示信息(地球已经不适合你居住+\n+快去上火星去吧!!);
创建结束画面();
return;
}
}
}
}
};
船仓2.onpress = function() {
if (船mc.状态 == 左岸) {
左岸mc[this.txt.text]._visible = true;
左岸mc[this.txt.text]._x = 0;
左岸mc[this.txt.text].方位 = 左岸;
删除(船上成员, this.txt.text);
this.txt.text = 船仓2.txt.text;
船仓2.txt.text = ;
}
if (船mc.状态 == 右岸) {
左岸mc[this.txt.text]._visible = true;
左岸mc[this.txt.text]._x = 480;
左岸mc[this.txt.text].方位 = 右岸;
删除(船上成员, this.txt.text);
this.txt.text = 船仓2.txt.text;
船仓2.txt.text = ;
}
};
返回开始();
}
function 画船(mc) {
mc.linestyle(0, 0);
mc.moveto(0, 0);
mc.lineto(10, 15);
mc.lineto(100, 15);
mc.lineto(110, 0);
mc.lineto(0, 0);
}
function 船移动(mc) {
mc.onenterframe = function() {
this._x += this.速度;
船仓1._x += this.速度;
船仓2._x += this.速度;
this.状态 = 航行;
if (this._x>=340) {
this.行动 = false;
this.速度 *= -1;
this.txt.txt.text = <<===;
this.状态 = 右岸;
delete this.onenterframe;
}
if (this._x<=100) {
this.行动 = false;
this.速度 *= -1;
this.txt.txt.text = ===>>;
this.状态 = 左岸;
delete this.onenterframe;
}
};
}
function 是否能划船(数组) {
for (var i = 0; i<数组.length; i++) {
if (数组[i].划船 == true) {
return true;
}
}
提示信息(这条船上没有能划船的动物);
return false;
}
function 能否呆船上(数组) {
if (数组.length == 2) {
var txt0 = 数组[0].名字;
var txt1 = 数组[1].名字;
if (数组[0].大小 == 数组[1].大小 || txt0.substring(1, 3) == txt1.substring(1, 3)) {
return true;
}
} else {
return true;
}
提示信息(船上的大动物会吃掉小动物);
return false;
}
function 岸上(岸) {
//这个算法不好`实在想不出其他的好算法
var 大数组 = new array();
var 小数组 = new array();
for (var i in 左岸mc) {
if (左岸mc[i].方位 == 岸) {
if (左岸mc[i].大小 == 大) {
大数组.push(左岸mc[i]._name.substring(1, 3));
}
if (左岸mc[i].大小 == 小) {
小数组.push(左岸mc[i]._name.substring(1, 3));
}
}
}
var 小记数 = 小数组.length;
var 大记数 = 大数组.length;
if (小记数<=0) {
return true;
}
if (大记数<=0) {
return true;
}
var 记数 = 0;
for (var i = 0; i<小记数; i++) {
var 对象 = 小数组[i];
for (var n = 0; n<大记数; n++) {
if (对象 == 大数组[n]) {
记数++;
break;
}
}
}
if (记数 == 小记数) {
return true;
} else {
提示信息(岸上的大动物会吃掉小动物);
return false;
}
}
function 删除(数组, 对象) {
for (var i = 0; i<数组.length; i++) {
if (数组[i].名字 == 对象) {
数组.splice(i, 1);
return true;
}
}
}
function 画方块(mc, x, y, 宽, 高, 填充) {
if (填充 == true) {
mc.beginfill(0xffffff, 100);
}
mc.linestyle(0, 0);
mc.moveto(x, y);
mc.lineto(x+宽, y);
mc.lineto(x+宽, 高+y);
mc.lineto(x, 高+y);
mc.lineto(x, y);
mc.endfill();
}
function 创建河流() {
var 河水= ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
var mc = 创建txt_mc(this, 河流, 河水, 0, 365, 100, false);
mc.x = 0;
mc.onenterframe = function() {
this._x += math.cos(this.x)*1;
this.x += 0.1;
};
}
function 提示信息(内容) {
提示mc.clear();
画方块(提示mc, 180, 50, 200, 70);
提示mc.计时 = 0;
提示mc[提示].removemovieclip();
提示mc.onenterframe = function() {
if (this.计时 == 0) {
this._visible = true;
创建txt_mc(提示mc, 提示, 内容, 提示mc._x+190, 提示mc._y+70, 115, false);
} else if (this.计时>30) {
this._visible = false;
delete onenterframe;
}
this.计时++;
};
}
function 创建结束画面() {
左岸mc.removemovieclip();
船mc.removemovieclip();
}
function 返回开始() {
创建txt_mc(_root, 开始, 重新+\n+开始, 490, 360, 100, true);
开始.onpress = function() {
loadmovie(_url, _root);
};
}
画方块(遮照mc, 0, 0, 550, 400, true);
_root.setmask(遮照mc);
画方块(this, 0, 0, 548, 398, false);
////////////////////////////////////////////////////////////
var 菜单 = new contextmenu();
菜单.hidebuiltinitems();
var qq = new contextmenuitem(qq:31559783, 实行函数);
qq.separatorbefore = true;
菜单.customitems.push(qq);
this.menu = 菜单;
function 实行函数() {
}

教程结束,以上就是flash as代码实现智力过河小游戏,希望大家喜欢本教程!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网