检测连续触摸的细胞以通过碰撞或位置获胜



我试着为学校的学生设计(bee_cell_competition(。一切都在我的代码中工作。我有7*7(49(个细胞作为电影剪辑。学生们随机选择一个单元格,试图使连续触摸的单元格获胜(垂直(,如图所示。当用户触摸到一个新单元格(mc_47(时,我该如何获取,从而让用户获胜。我有这个代码,但它无法检测。请参阅此图像描述

var i =0;
var j =0;
var sellected_bee_cell;
var enemy1;
var enemy2;
var touched_cell_arr:Array = new Array(panel_bee.cell_0,panel_bee.cell_00);
var mc_bee_cells:Array = new Array(panel_bee.cell_1,panel_bee.cell_2,panel_bee.cell_3,panel_bee.cell_4,panel_bee.cell_5,panel_bee.cell_6,panel_bee.cell_7,panel_bee.cell_8,panel_bee.cell_9,panel_bee.cell_10,panel_bee.cell_11,panel_bee.cell_12,panel_bee.cell_13,panel_bee.cell_14,panel_bee.cell_15,panel_bee.cell_16,panel_bee.cell_17,panel_bee.cell_18,panel_bee.cell_19,panel_bee.cell_20,panel_bee.cell_21,panel_bee.cell_22,panel_bee.cell_23,panel_bee.cell_24,panel_bee.cell_25,panel_bee.cell_26,panel_bee.cell_27,panel_bee.cell_28,panel_bee.cell_29,panel_bee.cell_30,panel_bee.cell_31,panel_bee.cell_32,panel_bee.cell_33,panel_bee.cell_34,panel_bee.cell_35,panel_bee.cell_36,panel_bee.cell_37,panel_bee.cell_38,panel_bee.cell_39,panel_bee.cell_40,panel_bee.cell_41,panel_bee.cell_42,panel_bee.cell_43,panel_bee.cell_44,panel_bee.cell_45,panel_bee.cell_46,panel_bee.cell_47,panel_bee.cell_48,panel_bee.cell_49);
//====================================================================================
for (i = 0 ; i < 49 ; ++i)
{
mc_bee_cells[i].buttonMode = true;
mc_bee_cells[i].mouseChildren = false;
}
for (i = 0 ; i < 49 ; ++i)
{
mc_bee_cells[i].addEventListener(MouseEvent.CLICK, run_the_next_q_4bee);
function run_the_next_q_4bee(e:Event):void
{
sellected_bee_cell = e.currentTarget ;
sellected_bee_cell.gotoAndStop("green");
touched_cell_arr.push(sellected_bee_cell);
check_win(null);
}
}
//===========================================================
function check_win(e:Event):void
{
for (var i:int = 0; i < touched_cell_arr.length - 1; i++)
{
var enemy1 = mc_bee_cells[i];
for(var j:int = i+1; j< touched_cell_arr.length; j++)
{
var enemy2 = mc_bee_cells[j];
if(enemy1.hitTestObject(enemy2))
{
trace("yes");
// now i need how to check that user completed continuous touched cells vertically or horizontally
}
}
}
}
//===========================================================

您应该保存被触摸的单元格链。您必须为此目的进行重构:

1( 创建一个类Cell,为您的细胞扩展MovieClip:

public class Cell extends MovieClip {
public var prevCell:Cell = null;
public var nextCell:Cell = null;
public var index:uint = 0;
public var isSelected = false;
public function Cell() {}
}

2( 重写run_the_next_q_4bee处理程序:

var neighbourSelectedCellIndex:Array = [];
function run_the_next_q_4bee(e:Event):void {
sellected_bee_cell = e.currentTarget ;
sellected_bee_cell.gotoAndStop("green");
touched_cell_arr.push(sellected_bee_cell);
// Check neighbour cells
var neighbourSelectedCellIndex = 
checkNeighbourCells(sellected_bee_cell.index)
if (neighbourSelectedCellIndex == -1) {
touchedCellsChains.push(sellected_bee_cell)
} else {
var neighbourCell = mc_bee_cells[neighbourSelectedCellIndex];
neighbourCell.nextCell = sellected_bee_cell;
sellected_bee_cell.prevCell = prevCell;
}
check_win(null);
}
function checkNeighbourCells(selectedCellIndex:uint):int {
// Check neighbour cells. Are there any selected adjoining cell?
// If there is any selected cell return its index
return <selected cell index>;
// If there is no selected cell return -1
return -1;
}

3( 重写check_win方法:

// You make call `hitTestObject` method    
var hasTop = false;
var hasBottom = false;
var prevCell = enemy1;
do {
if (prevCell.index < 7) {
hasTop = true;
} else if (prevCell.index > 42) {
hasBottom = true;
}
prevCell = prevCell.prevCell;
} while (!prevCell)
var nextCell = enemy1;
do {
if (nextCell.index < 7) {
hasTop = true;
} else if (nextCell.index > 42) {
hasBottom = true;
}
nextCell = nextCell.nextCell;
} while (!nextCell)
if (hasTop && hasBottom) {
trace("You win!");
}

最新更新