This site requires JavaScript, please enable it in your browser!
Greenfoot back
Programmer1998
Programmer1998 wrote ...

2015/10/12

MouseInfo Question

Programmer1998 Programmer1998

2015/10/12

#
Hi! I'm creating a checkers game and I'm trying to move the checkers on the board using the MouseInfo, however it's not working. Here is code from my world and methods of my checkers positions.
public CheckersLogic(int rows, int columns)
    {
        row_ = rows;
        column_ = columns; 
        currentPlayer_ = red_; //the starting player is red
        position_ = new int[row_][column_]; //position relevent to the row and columns
        
        position_[0][1] = red_;
        position_[0][3] = red_;
        position_[0][5] = red_;
        position_[0][7] = red_;
        position_[1][0] = red_;
        position_[1][2] = red_;
        position_[1][4] = red_;
        position_[1][6] = red_;
        position_[2][1] = red_;
        position_[2][3] = red_;
        position_[2][5] = red_;
        position_[2][7] = red_;
        
        position_[5][0] = white_;
        position_[5][2] = white_;
        position_[5][4] = white_;
        position_[5][6] = white_;
        position_[6][1] = white_;
        position_[6][3] = white_;
        position_[6][5] = white_;
        position_[6][7] = white_;
        position_[7][0] = white_;
        position_[7][2] = white_;
        position_[7][4] = white_;
        position_[7][6] = white_;
    }

public int getPosition(int row, int column)
    {
        //check if the space is within the boundaries
        if(row < 0 || row > row_ || column < 0 || column > column_)
        {
            return invalid_; //this spot is not on the board
        }
        else //if the position is valid
        {
            return position_[row][column]; //return the position
        }
    }
public void movePieces()
    {
        int r = logic_.getRows();
        int c = logic_.getColumns();

        //need to track the piece being moved
        //setLocation the checker piece being moved
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int initialX = 0;
        int initialY = 0;
        int finalX = 0;
        int finalY = 0;
        Red red = new Red();
        White white = new White();
        for(int y = 0; y < r; y++)
        {
            for(int x = 0; x < c; x++)
            {
                int position = logic_.getPosition(y, x); //will get the position on the board
                if(logic_.currentPlayer_ == logic_.red_)
                {
                    //if the position is red and the current player is the red player
                    if(isSelected_ && Greenfoot.mousePressed(red))
                    {
                        isSelected_ = true;
                        //removeObjects(getObjectsAt(y, x, Red.class));
                        initialX = red.getX(); //gets the intial coordinates
                        initialY = red.getY();
                    }
                    else if(isSelected_ == true && Greenfoot.mouseDragged(red))
                    {
                        red.setLocation(mouse.getX(), mouse.getY());
                    }
                    else if(isSelected_ == true && Greenfoot.mouseDragEnded(red))
                    {
                        isSelected_ = false; //is no longer selected
                        finalX = mouse.getX();
                        finalY = mouse.getY();
                        red.setLocation(finalX, finalY); //set piece where mouse is 
                    }
                }
                else if(logic_.currentPlayer_ == logic_.white_)
                {
                    //if the position is white and the current player is the white player
                    if(isSelected_ && Greenfoot.mousePressed(white))
                    {
                        isSelected_ = true;
                        initialX = white.getX(); //gets the intial coordinates
                        initialY = white.getY();
                    }
                    else if(isSelected_ == true && Greenfoot.mouseDragged(white))
                    {
                        white.setLocation(mouse.getX(), mouse.getY());
                    }
                    else if(isSelected_ == true && Greenfoot.mouseDragEnded(white))
                    {
                        isSelected_ = false; //is no longer selected
                        finalX = mouse.getX();
                        finalY = mouse.getY();
                        white.setLocation(finalX, finalY);
                    }
                }
            }
        }
    }
danpost danpost

2015/10/12

#
One major issue is that you are creating new Red and White objects on lines 13 and 14. These objects are not any of the Red or White objects you may already have in the world and themselves are not in your world. Therefore, the conditions on lines 23 and 45 where you check for mouse presses on these objects will always be false. Better would be to check for any mouse press with:
if (Greenfoot.mousePressed(null))
and then, if it becomes 'true', get the Actor object, if any, the mouse was pressed on:
MouseInfo mouse = Greenfoot.getMouseInfo();
Actor mouseActor = null;
if (mouse != null) mouseActor = mouse.getActor();
At this point 'mouseActor' could still be 'null' or contain an Actor object, which must be checked using 'instanceof' to see if it is of a valid class for moving at that time.
You need to login to post a reply.