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

2017/10/15

How to return a method with boolean type?

ML31 ML31

2017/10/15

#
Hello, very new with Greenfoot, please help me with this code! After the if statement in the moveEatRight method, I can't figure out how to return type boolean whether that's true or false. I think it might be an issue with how I am using the parenthesis but for now, my method is not working, i.e. the Chess block on the right is not being removed, only the return statement can be seen which is whatever I write before the end of the method. The code requires me to eliminate the chess piece on the right and return true.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Chess here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Chess extends Actor
{
    private boolean isFlipped; //chess flipped or not
    private int chessID; //0 (Black) 1 (Red)
    
    
    public static final int ID_BLACK_GENERAL= 0;
    public static final int ID_RED_GENERAL =1;
   
    public boolean moveAndEatRight(){
       
            
        int curX= getX();
        int curY= getY();
        Chess rightChess = (Chess)GWorld.getOneObjectAt( curX+1, curY, "Chess");
    
        if (rightChess!=null){
            if (rightChess.isFlipped){
                GWorld.removeOneObject(rightChess);
                setLocation (curX+1, curY);
               return true;
                }
              
    
            }
           
       return false;
 
}
   


    public Chess (int id){
        isFlipped= false;
        chessID= id;
    }
    public void flip(){
        if(isFlipped==false){
            if(chessID==0){
                setImage("Chess-General-Black.gif");
                
        }
        else if(chessID==1){
            setImage("Chess-General-Red.gif");
        }
        else{
        }
    }}
public void Act(){
}
}

            
       
      
   
  
       

danpost danpost

2017/10/15

#
I think the problem is that 'isFlipped' is never true -- nowhere is it being set to true.
You need to login to post a reply.