注:转载请注明出处:http://blog.csdn.net/u011669081/article/details/52368251
前言在收到腾讯面试邀请的时候,作为应届生还有点小激动,面试的岗位居然是我未接触的游戏开发,额,四面之后,就出了个题目,一周之内自学flash并且开发天天爱消除。然后就硬着头皮干了。
设计思路
天天爱消除的游戏方式其实很简单,首先游戏刚进入是必须生成一个随机的环境(我们这里可以把它看成7*7的数组),用户操作相邻元素的替换,当3个以上相同元素在同行同列情况下,要消除,然后上行下移,重复这些逻辑,直到时间耗尽,游戏结束。手机版如下截图:
实现源码思路想清除后,就是将每一步思路写成代码。(这里是三天写完的Demo代码,有很多可以进一步优化的地方,有低效代码的存在,勿喷~)先来看随机生成元素:
/*
第一次生成的数组应当是:同行同列的情况是几乎不可以存在的
/
private function createGroup():void{
for(var i:uint = 0;i<7;i++){
for(var j:uint = 0;j<7;j++){
if(i == 0 && j < 2){//前两位 几乎不需要判断
var n:uint = Math.floor(Math.random()4);
arr[i][j] = n;
}else{
var t:uint = Math.floor(Math.random()4);
var fg:Boolean = checkGroup(t,i,j);
//这里有待优化,可能存在死循环
while(fg){
var m:uint = Math.floor(Math.random()4);
fg = checkGroup(m,i,j);
}
}
}
}
}
注意:checkGroup(m,i,j);是检测是否有可消除元素。这里确实存在风险,有待优化。然后再是控制元素:
private function sunClicked(e:MouseEvent):void {
if(!gameState){//游戏已经结束
statusTextField.text="游戏已经结束了~~~~~~";
return;
}
lastClickTime = repeat;
nowX = e.stageX/45;
nowY = e.stageY/45;
var sound:Sound = new Shoot();
sound.play();
if(oldX != -1 && oldY != -1){//不是第一次点击,但是需要做个判断:是否两个点击是相邻的
if(
((nowX == oldX && Math.abs(nowY - oldY)<= 1 )
|| ((Math.abs(nowX-oldX)<=1 && nowY == oldY))
)){
//trace(" 符合");
}else{
//选择不合规范
chooseC = 0;//重置选择
}
}
if(chooseC == 0){
chooseC = 1;
oldX = nowX;
oldY = nowY;
if(e.currentTarget.toString().search("yellow")!=-1){
var yel:yellow=e.currentTarget as yellow;
TransitionManager.start(yel, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}else if(e.currentTarget.toString().search("red")!=-1){
var re:red=e.currentTarget as red;
TransitionManager.start(re, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}else if(e.currentTarget.toString().search("blue")!=-1){
var bl:blue=e.currentTarget as blue;
TransitionManager.start(bl, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}else if(e.currentTarget.toString().search("blank")!=-1){
var bla:blank=e.currentTarget as blank;
TransitionManager.start(bla, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}
//trace("第一个");
}else if(chooseC ==1){
chooseC = 0;
changeView(nowX,nowY,oldX,oldY);
cleanItem();
}
}
//交换元素
private function changeView(currX:int,currY:int,lastX:int,lastY:int):void{
var temp:int = arr[currX][currY];
arr[currX][currY] = arr[lastX][lastY];
arr[lastX][lastY] = temp;
}
其中sunClicked是对点击元素的监听,changeView是将7*7的数组中元素交换。移除可消除的元素:
//移除掉 可以消除的元素
private function cleanItem():void{
var fg:Boolean = false;
var now:int = -1;
//这里将数组再复制一份,因为下面的判断内有修改数组的操作,如果第一次修改了数组,意味着第二次的比较会受影响,所以复制数组
for(var a:int = 0;a<7;a++){
for(var b:int = 0;b<7;b++){
tempArray[a][b] = arr[a][b];
}
}
for(var i:uint =0;i<7;i++){
for(var j:uint =0;j<7;j++){
now = arr[i][j];
if(i<=2){//判断5个元素
if(now == arr[i+1][j] && now == arr[i+2][j] && now == arr[i+3][j] && now == arr[i+4][j] && now !=4){
tempArray[i][j] =4;
tempArray[i+1][j] =4;
tempArray[i+2][j] =4;
tempArray[i+3][j] =4;
tempArray[i+4][j] =4;
makes = makes + 3;
fg=true;
}
}
if(i<=3){//判断4个元素
if(now == arr[i+1][j] && now == arr[i+2][j] && now == arr[i+3][j] && now !=4){
tempArray[i][j] =4;
tempArray[i+1][j] =4;
tempArray[i+2][j] =4;
tempArray[i+3][j] =4;
makes = makes + 2;
fg=true;
}
}
if(i<=4){//判断3个元素
if(now == arr[i+1][j] && now == arr[i+2][j] && now !=4){
tempArray[i][j] =4;
tempArray[i+1][j] =4;
tempArray[i+2][j] =4;
makes = makes + 1;
fg=true;
}
}
if(j<=2){//判断5个元素
if(now == arr[i][j+1] && now == arr[i][j+2] && now == arr[i][j+3] && now == arr[i][j+4] && now !=4){
tempArray[i][j] =4;
tempArray[i][j+1] =4;
tempArray[i][j+2] =4;
tempArray[i][j+3] =4;
tempArray[i][j+4] =4;
makes = makes + 3;
fg=true;
}
}
if(j<=3){//判断4个元素
if(now == arr[i][j+1] && now == arr[i][j+2] && now == arr[i][j+3] && now !=4){
tempArray[i][j] =4;
tempArray[i][j+1] =4;
tempArray[i][j+2] =4;
tempArray[i][j+3] =4;
makes = makes + 2;
fg=true;
}
}
if(j<=4){//判断3个元素
if(now == arr[i][j+1] && now == arr[i][j+2] && now !=4){
tempArray[i][j] =4;
tempArray[i][j+1] =4;
tempArray[i][j+2] =4;
makes = makes + 1;
fg=true;
}
}
}
}
//最后变化所得的数组都在 临时数组里面,所以将临时数组结果给最后的数组
for(var a:int = 0;a<7;a++){
for(var b:int = 0;b<7;b++){
arr[a][b] = tempArray[a][b];
}
}
if(fg==true){//有元素清除
//创建一个新的View,有闪动效果,可移除
//delays();
createAniView();
var delayStart:Timer = new Timer(500, 1);//为了让消除的时候可以有直接的消除效果,选择使用延迟的做法,让视觉上有个替换的过程
delayStart.addEventListener(TimerEvent.TIMER_COMPLETE, goPlay);
delayStart.start();
function goPlay(e:TimerEvent):void{
updateItem();
updateView();
//消除元素结束后,需要创建新的元素加到空位
reCreateGroup();
trace("是否可以继续消除 "+isCanClean());
while(isCanClean()){//还可以继续消除
cleanItem();
}
}
}
makeText.text = "当前积分:"+makes;
}
执行掉落的方法:
//更新,将数组移位,填满空的元素,上方元素掉落
private function updateItem():void{
for(var j:int = 0;j<7;j++){
var temps:Array = new Array();// 用来保存临时的结果
var num:int = 0;
for(var i:int=0;i<7;i++){
if(arr[j][i] != 4){
temps[num] = arr[j][i];
num++;
}
}
//trace(" j = "+j+" "+temps);
for(var k:int = 0;k<7;k++){
arr[j][k] = 4;
}
var n:int = 0;
for(var m:int=(7-temps.length);m <7;m ++){
arr[j][m] = temps[n];
//arr[m][j] = temps[n];
n++;
}
}
}
运行截图
源码:点击下载