Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android位图:冲突检测_Java_Android - Fatal编程技术网

Java Android位图:冲突检测

Java Android位图:冲突检测,java,android,Java,Android,我现在正在写一个Android游戏,我需要一些帮助来防止屏幕上的墙壁碰撞。当我在顶部和右侧拖动球时,它能够在墙中碰撞,但当我将它拖动得更快时,它能够在墙中重叠 public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) {

我现在正在写一个Android游戏,我需要一些帮助来防止屏幕上的墙壁碰撞。当我在顶部和右侧拖动球时,它能够在墙中碰撞,但当我将它拖动得更快时,它能够在墙中重叠

public boolean onTouchEvent(MotionEvent event) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            switch (event.getAction()) {
            // if the player moves
            case MotionEvent.ACTION_MOVE: {
                if (playerTouchRect.contains(x, y)) {
                    boolean left = false;
                    boolean right = false;
                    boolean up = false;
                    boolean down = false;
                    boolean canMove = false;
                    boolean foundFinish = false;
                    if (x != pLastXPos) {
                        if (x < pLastXPos) {
                            left = true;
                        } else {
                            right = true;
                        }
                        pLastXPos = x;
                    }
                    if (y != pLastYPos) {
                        if (y < pLastYPos) {
                            up = true;
                        } else {
                            down = true;
                        }
                        pLastYPos = y;
                    }

                    plCellRect = getRectFromPos(x, y);
                    newplRect.set(playerRect);
                    newplRect.left = x - (int) (playerRect.width() / 2);
                    newplRect.right = x + (int) (playerRect.width() / 2);
                    newplRect.top = y - (int) (playerRect.height() / 2);
                    newplRect.bottom = y + (int) (playerRect.height() / 2);
                    int currentRow = 0;
                    int currentCol = 0;
                    currentRow = getRowFromYPos(newplRect.top);
                    currentCol = getColFromXPos(newplRect.right);



                    if(!canMove){
                    canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] == Cell.wall;
                    canMove =true;
                    }
                    finishTest = mapManager.getCurrentTile().pMaze[currentRow][currentCol];
                    foundA = finishTest == Cell.valueOf(letterNotGet + "");



                    canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] != Cell.wall;
                    canMove = (finishTest == Cell.floor || finishTest == Cell.pl) && canMove;
                    if (canMove) {
                        invalidate();
                        setTitle();
                    }
                    if (foundA) {
                        mapManager.getCurrentTile().pMaze[currentRow][currentCol] = Cell.floor;
                        //  finishTest
                        letterGotten.add(letterNotGet);
                        playCurrentLetter();
                        /*sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);*/
                        foundS = letterNotGet == 's';
                        letterNotGet++;
                    }if(foundS){
                        AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
                        builder.setTitle(mainActivity.getText(R.string.finished_title));
                        LayoutInflater inflater = mainActivity.getLayoutInflater();
                        View view = inflater.inflate(R.layout.finish, null);
                        builder.setView(view);
                        View closeButton =view.findViewById(R.id.closeGame);
                        closeButton.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View clicked) {
                                if(clicked.getId() == R.id.closeGame) {
                                    mainActivity.finish();
                                }
                            }
                        });
                        AlertDialog finishDialog = builder.create();
                        finishDialog.show();
                    } 
                    else {
                        Log.d(TAG, "INFO: updated player position");
                        playerRect.set(newplRect);
                        setTouchZone();
                        updatePlayerCell();
                    }
                } // end of (CASE) if playerTouch
                break;
            } // end of (SWITCH) Case motion
            }//end of Switch
            return true;
        }//end of TouchEvent
        private void finish() {
            // TODO Auto-generated method stub
        }
        public int getColFromXPos(int xPos) {
            val = xPos / (pvWidth / mapManager.getCurrentTile().pCols);
            if (val == mapManager.getCurrentTile().pCols) {
                val = mapManager.getCurrentTile().pCols - 1;
            }
            return val;
        }
        /**
         * Given a y pixel position, return the row of the cell it is in This is
         * used when determining the type of adjacent Cells.
         * 
         * @param yPos
         *            y position in pixels
         * @return The cell this position is in
         */
        public int getRowFromYPos(int yPos) {
            val = yPos / (pvHeight / mapManager.getCurrentTile().pRows);
            if (val == mapManager.getCurrentTile().pRows) {
                val = mapManager.getCurrentTile().pRows - 1;
            }
            return val;
        }
        /**
         * When preserving the position we need to know which cell the player is in,
         * so calculate it from the centre on its Rect
         */
        public void updatePlayerCell() {
            plCell.x = (playerRect.left + (playerRect.width() / 2))
                    / (pvWidth / mapManager.getCurrentTile().pCols);
            plCell.y = (playerRect.top + (playerRect.height() / 2))
                    / (pvHeight / mapManager.getCurrentTile().pRows);
            if (mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] == Cell.floor) {
                for (int row = 0; row < mapManager.getCurrentTile().pRows; row++) {
                    for (int col = 0; col < mapManager.getCurrentTile().pCols; col++) {
                        if (mapManager.getCurrentTile().pMaze[row][col] == Cell.pl) {
                            mapManager.getCurrentTile().pMaze[row][col] = Cell.floor;
                            break;
                        }
                    }
                }
                mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] = Cell.pl;
            }
        }
        public Rect getRectFromPos(int x, int y) {
            calcCell.left = ((x / cellWidth) + 0) * cellWidth;
            calcCell.right = calcCell.left + cellWidth;
            calcCell.top = ((y / cellHeight) + 0) * cellHeight;
            calcCell.bottom = calcCell.top + cellHeight;
            Log.d(TAG, "Rect: " + calcCell + " Player: " + playerRect);
            return calcCell;
        }
        public void setPlayerRect(Rect newplRect) {
            playerRect.set(newplRect);
        }
        private void setTouchZone() {
            playerTouchRect.set(
                    playerRect.left - playerRect.width() / TOUCH_ZONE,

                    playerRect.top - playerRect.height() / TOUCH_ZONE,
                    playerRect.right + playerRect.width() / TOUCH_ZONE,
                    playerRect.bottom + playerRect.height() / TOUCH_ZONE);
        }
        public Rect getPlayerRect() {
            return playerRect;
        }
        public Point getPlayerCell() {
            return plCell;
        }
        public void setPlayerCell(Point cell) {
            plCell = cell;
        }
    }*
public boolean onTouchEvent(运动事件){
int x=(int)event.getX();
int y=(int)event.getY();
开关(event.getAction()){
//如果玩家移动
case MotionEvent.ACTION\u移动:{
if(playerTouchRect.包含(x,y)){
布尔左=假;
布尔右=假;
布尔向上=假;
布尔向下=假;
布尔值canMove=false;
布尔foundFinish=false;
如果(x!=pLastXPos){
if(x