Java 在网格中交换项目

Java 在网格中交换项目,java,android,touch,andengine,game-engine,Java,Android,Touch,Andengine,Game Engine,我有一个6x6的精灵网格。我正在尝试创建以下行为,但我似乎遇到了selectedCell未重置的问题。它似乎在交换后随机保留选定的单元格,但选择和取消选择同一单元格似乎有效 当用户点击一个单元格时,它应该成为选中的单元格 当用户再次点击单元格时,所选单元格应重置为空(或无) 当用户点击一个单元格,然后点击另一个不同的单元格时,这些单元格应该被交换。(通过交换对象属性。在此之后,应重置所选单元格。) 我实现此功能的方式中是否存在任何明显的错误,或者是否有更简单的方法来实现此功能 这些单元格具有以下

我有一个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();
    }
    

    在操作的触摸事件中,不要使用“中断”,而是返回“真”:

    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;
    

    对您处理的所有操作都执行此操作。

    如果您对所选单元格未重置有问题,请同时发布重置方法的代码。我已将这些方法添加到问题中,请澄清这句话:“似乎在交换后随机保留所选单元格”。你什么意思?随机?注意,它是真的随机的还是有一个模式…我真的看不到任何模式。有时它不会将所选单元格重置为null,当我不断点击每个单元格时,它将与上一个单元格切换。但是,如果我双击一个单元格,它将始终重置。我尝试将方法调用更改为ACTION_DOWN事件-同样的问题。然而,我确实注意到,似乎发生了双重事件——我的意思是双重动作,这可能解释了为什么所选电池可能会重置然后设置。太好了——这似乎解决了我的触摸事件问题!