Crash issues with drag-n-drop puzzle

I am working on an Android game similar to the Rush Hour / Traffic Jam / Blocked puzzle. The board is a square containing rectangular shapes. Long pieces can only move horizontally, and tall pieces can only move vertically. The goal is to free the red piece and take it out of the board. This game is just my second programming project in any language, so any advice or recommendations will be appreciated along with your answer.

I have a class for fragments of the Pieces game that describes how they are scaled and drawn on the screen, gives them a drag and drop function, and detects and handles conflicts.

Then I have an activity class called GameView that creates my layout and creates Pieces to add to a RelativeLayout called Board. I considered making my own class, but not yet needed.

This is what my work looks like:
screen_cap1

:
, . , , , , , (- ), . : screencapgif1
: , , , . , .
:

    @Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //if next to another piece, 
                //do not allow to move any further towards said piece
                if(eventX<x&&(x==piece.right+1)){
                    return false;
                }else if(eventX>x&&(x==piece.x-width-1)){
                    return false;
                }
                //move normally if no collision
                //if collision, do not allow to move through other piece
                if(collides(this,piece)==false){
                    x = (eventX-(width/2));
                }else if(collidesLeft(this,piece)){
                    x = piece.right+1;
                    break;
                }else if(collidesRight(this,piece)){
                    x = piece.x-width-1;
                    break;
                }
            }
            break;
        }else if(height>width){
            for (Pieces piece : aPieces) {
                if(piece==this){
                    continue;
                }else if(collides(this,piece)==false){
                    y = (eventY-(height/2));
                }else if(collidesUp(this,piece)){
                    y = piece.bottom+1;
                    break;
                }else if(collidesDown(this,piece)){
                    y = piece.y-height-1;
                    break;
                }
            }
        }
        invalidate();
        break;

    case MotionEvent.ACTION_UP:
        // end move
        if(this.moves()){
        GameView.counter++;
        }
        initialX=x;
        initialY=y;
        break;
        }
    // parse puzzle
    invalidate();
    return true;
    }

onDraw:

width = sizedBitmap.getWidth();
height = sizedBitmap.getHeight();
right = x+width;
bottom = y+height;

, :

    private boolean collidesDown(Pieces piece1, Pieces piece2){
    float x1 = piece1.x;
    float y1 = piece1.y;
    float r1 = piece1.right;
    float b1 = piece1.bottom;
    float x2 = piece2.x;
    float y2 = piece2.y;
    float r2 = piece2.right;
    float b2 = piece2.bottom;

    if((y1<y2)&&(y1<b2)&&(b1>=y2)&&(b1<b2)&&((x1>=x2&&x1<=r2)||(r1>=x2&&x1<=r2))){
        return true;
    }else{
        return false;
    }
}

private boolean collides(Pieces piece1, Pieces piece2){
    if(collidesLeft(piece1,piece2)){
        return true;
    }else if(collidesRight(piece1,piece2)){
        return true;
    }else if(collidesUp(piece1,piece2)){
        return true;
    }else if(collidesDown(piece1,piece2)){
        return true;
    }else{
        return false;
    }
}

x, y, right, bottom, width, height ints float, ? , , , ! , !

Update:
( ):

@Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                        }else{
                            x = piece.right+1;
                        }
                    }
                    //check for and handle collisions while moving right
                    if(this.isLeftOf(piece)){
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                        }else{
                            x = piece.x-width-1;
                        }
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }

, ( ). , , . , , , , , , . ?

+3
5

, , , , . , , , .

, :

            //if next to another piece, 
            //do not allow to move any further towards said piece
            if(eventX<x&&(x==piece.right+1)){
                return false;
            }else if(eventX>x&&(x==piece.x-width-1)){
                return false;
            }

(==) - , , , . " ". ) ints float. ) (>= ..). (a), () ..

, :

    //check if touch is on piece
    if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height))
+1

:)

, , , , , .

, 1 , , 2 , . 1 , 2 - .

// Horizontal:
if( piece1.isMovingLeft )
    piece1.x += Math.max( 0, piece2.right - piece1.x );
else if( piece1.isMovingRight )
    piece1.x -= Math.max( 0, piece1.right - piece2.x );

// Vertical:
if( piece1.isMovingUp )
    piece1.y -= Math.max( 0, piece2.bottom - piece1.y );
else if( piece1.isMovingDown )
    piece1.y += Math.max( 0, piece1.bottom - piece2.y )

:

  • , () , () . .
  • , , . ( .)
  • Math.max(0, ) , 0, .. .
  • , , 1 2. ( .)

, .

, !

+1

:

, ( ). , , .

break, , .;)

0

, . :

case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                            continue;
                        }else{
                            x = piece.right+1;
                        }
                    }else if(this.isLeftOf(piece)){ //check for and handle collisions while moving right
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                            continue;
                        }else{
                            x = piece.x-width-1;
                        }
                    }else{
                        continue;
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }
            }
        }
        if(height>width){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a vertical collision
                if(this.isAllignedVerticallyWith(piece)){
                    //check for and handle collisions while moving up
                    if(this.isBelow(piece)){
                        if(eventY>piece.bottom+(height/2)){
                            y = (int)(eventY-(height/2)); //move normally
                            continue;
                        }else{
                            y = piece.bottom+1;
                        }
                    }else if(this.isAbove(piece)){ //check for and handle collisions while moving down
                        if(eventY<piece.y-(height/2)){
                            y = (int)(eventY-(height/2));
                            continue;
                        }else{
                            y = piece.y-height-1;
                        }
                    }else{
                        continue;
                    }
                    break;
                }else{
                    y = (int)(eventY-(height/2));
                }
            }
        }
        invalidate();
        break;

:

private boolean isLeftOf(Pieces piece) {
    int r1 = this.right;
    int x2 = piece.initialX;
    boolean bool = false;
    if (r1<=x2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedHorizontallyWith(piece2) && r1<=piece2.initialX){
                if (piece.x>piece2.x){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isRightOf(Pieces piece) {
    //True means that "this" is right of "piece" and "piece" is farther right than other possible pieces
    int x1 = this.initialX;
    int r2 = piece.right;
    boolean bool = false;

    if (x1>=r2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedHorizontallyWith(piece2) && x1>=piece2.right){
                if (piece.x<piece2.x){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isBelow(Pieces piece){
    int y1 = this.initialY;
    int b2 = piece.bottom;
    boolean bool = false;
    if (y1>=b2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedVerticallyWith(piece2) && y1>=piece2.bottom){
                if (piece.y<piece2.y){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isAbove(Pieces piece){
    int y2 = piece.initialY;
    int b1 = this.bottom;
    boolean bool = false;
    if (b1<=y2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedVerticallyWith(piece2) && b1<=piece2.y){
                if (piece.y>piece2.y){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isAllignedHorizontallyWith(Pieces piece) {

    int y1 = this.y;
    int b1 = this.bottom;
    int y2 = piece.y;
    int b2 = piece.bottom;

    if((y1>=y2&&y1<=b2)||(b1>=y2&&b1<=b2)){
        return true;
    }else{
        return false;   
    }
}

private boolean isAllignedVerticallyWith(Pieces piece) {

    int x1 = this.x;
    int r1 = this.right;
    int x2 = piece.x;
    int r2 = piece.right;

    if((x1>=x2&&x1<=r2)||(r1>=x2&&r1<=r2)){
        return true;
    }else{
        return false;   
    }
}

, , , .

0
source

Nothing, just getting your image s Left,right,top,bottom, and intersect with current x and y and find this solution more about this go to this link itwill also help, I hope you got the right answer from this link.

/

/ technical / game-programming collision detection-algorithm-r754 "> http://www.gamedev.net/page/resources//technical/game-programming/collision-detection-algorithm-r754>

0
source

All Articles