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

2013/12/12

getOneObjectAtOffset not working?

VincentG VincentG

2013/12/12

#
Trying to do a checkers game, and checking for move legality. As you can see in the 3rd if statement, trying to check if the tile I'm moving to is null or not. It ALWAYS return null, every time I move a piece to another tile that has an opponent piece or my own piece, it just overlaps and there's 2 piece in 1 tile. I have a CheckerPiece actor class that inherits Checker, in CheckerPiece it just creates the objects, both red and blue pieces.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class Checker extends Actor
{
    boolean isSelected = false;
    int mouseX = 0;
    int mouseY = 0;
    int fromX = 0;
    int fromY = 0;
    int toX = 0;
    int toY = 0;
    
    
    public void act(){
       Board board = (Board) getWorld();
   
        
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (isTurn()){ //Checks if the piece clicked is the owned by the player who is currently taking their turn
                if (!isSelected && Greenfoot.mousePressed(this)) //If a piece is not already selected and the mouse is clicked on a checker piece
                {
                    isSelected = true; //Select this piece
                    fromX = getX(); //store the starting location x value
                    fromY = getY(); //store the starting location y value
                    toX = 0; //reset the to X value to 0
                    toY = 0; //reset the to X value to 0

                }
                if (isSelected && Greenfoot.mouseDragged(this)) //as the mouse is dragged around the screen make the piece move along with the mouse
                { 
                    setLocation(mouse.getX(), mouse.getY()); 
                    return; 
                }
                if (isSelected && Greenfoot.mouseDragEnded(this)) //when the user releases the mouse button and stops dragging, check if the move was legal.
                { 
                    isSelected = false; 
                    toX = mouse.getX();
                    toY = mouse.getY();
                 
                    if (!(toX == fromX-1 && toY == fromY+1) && !(toX == fromX+1 && toY == fromY+1) 
                        && !(toX == fromX-1 && toY == fromY-1) && !(toX == fromX+1 && toY == fromY-1)
                        &&  getOneObjectAtOffset( toX, toY, Checker.class )==  ) //If illegal move back to starting position.
                    { 
                        setLocation(fromX, fromY); 
                        return; 
                    }
                    
                    //  If legal move, then move the selected piece
                    setLocation(toX, toY);
                    return;
                }
            }  
        }
    
    public boolean isTurn() 
    { 
            return true;
    }
}
   
    
    
VincentG VincentG

2013/12/12

#
Sorry, I meant 5th if statement and getOneObjectAtOffset( toX, toY, CheckerPiece.class )==null
danpost danpost

2013/12/12

#
I believe that the incomplete statement was a mis-copy (line 43) where 'null' is missing. However, should not the comparison be '!=' (not equal), as if there is a piece located there, you want to move the dragged piece back.
VincentG VincentG

2013/12/12

#
I figured out what was happening, I miss understood getOneObjectAtOffset method. Thanks for the reply too :)
You need to login to post a reply.