我有一个6x6的精灵网格。我试图创建以下行为,但我似乎有问题,selectedCell没有重置。它似乎在交换后随机保留一个选定的单元格,但选择和取消选择相同的单元格似乎有效。
我实现此功能的方式是否有任何明显错误,或者有更简单的方法吗?
单元格具有以下OnTouchEventMethod:
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch(pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
this.setScale(1.2f);
this.setZIndex(999);
this.gGrid.sortChildren();
Log.d("TOUCH", "ACTION_DOWN");
break;
case TouchEvent.ACTION_UP:
this.setScale(1.0f);
this.setZIndex(1);
this.gGrid.sortChildren();
Log.d("TOUCH", "ACTION_UP");
gGrid.cellTouchEvent(this);
break;
case TouchEvent.ACTION_MOVE:
break;
}
return false;
}
此代码将调用网格对象
中的cellTouchEvent()方法 /**
* called when a touch event is registered on a Grid Cell
*/
public void cellTouchEvent(GameGridCell cell) {
if(cell.isGGCellVisible()) {
if(this.selectedCell==null) {
this.setSelectedCell(cell);//sets the selected cell as one is not set
Log.d("TEST", "SET SELECTED CELL");
} else {
if(this.selectedCell == cell) {
Log.d("TEST", "UNSET SELECTED CELLS");
this.setSelectedCellToNull(); //Set selected cell to null as it was pressed twice
} else if (this.selectedCell != cell) {
this.swapCells(this.selectedCell, cell); //Swap the two cells
this.setSelectedCellToNull(); //Ensure selected cell is set to null after swap
}
}
} else if(!cell.isGGCellVisible()) {
cell.showCellContents(); //Show the contents of the cell as it is not visible
}
}
最后,这是进行实际单元格交换的代码
/**
* Swaps the two cells passed in as parameters
*/
public void swapCells(GameGridCell cellA, GameGridCell cellB) {
//First swap the locations in the gridItemsArray
this.gridItemsArray.setValueAt(cellA.getGGCellID(), cellB);
this.gridItemsArray.setValueAt(cellB.getGGCellID(), cellA);
//Save cell A variables
float x = cellA.getX();
float y = cellA.getY();
int id = cellA.getGGCellID();
int type = cellA.getGGCellType();
boolean visible = cellA.isVisible();
//Set cell A to cells B variables
cellA.setX(cellB.getX());
cellA.setY(cellB.getY());
cellA.setGGCellID(cellB.getGGCellID());
cellA.setGGCellType(cellB.getGGCellType());
cellA.setVisible(cellB.isVisible());
//set cell B to saved variables
cellB.setX(x);
cellB.setY(y);
cellB.setGGCellID(id);
cellB.setGGCellType(type);
cellB.setVisible(visible);
//Update the cell ID text
cellA.updateCellIDText();
cellB.updateCellIDText();
this.sortChildren(); //Finally sort the children
}
这里的评论是设置所选单元格的方法
/**
* @return the selectedCell
*/
public GameGridCell getSelectedCell() {
return selectedCell;
}
/**
* @param selectedCell the selectedCell to set
*/
public void setSelectedCell(GameGridCell selectedCell) {
if(selectedCell==null) {
this.selectedCell=null;
} else {
this.selectedCell = selectedCell;
}
this.gameLevel.updateCellIDText();
}
/**
* @param selectedCell the selectedCell to set
*/
public void setSelectedCellToNull() {
this.selectedCell=null;
this.gameLevel.updateCellIDText();
}
答案 0 :(得分:0)
在触摸事件中执行操作,而不是使用break,返回true:
case TouchEvent.ACTION_UP:
this.setScale(1.0f);
this.setZIndex(1);
this.gGrid.sortChildren();
Log.d("TOUCH", "ACTION_UP");
gGrid.cellTouchEvent(this);
//don't use break
//break;
//instead return true, which means you've handled touch
return true;
为您处理的所有操作执行此操作。