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

2013/4/15

problem using Greenfoot.mouseClicked(this)

jainmanish jainmanish

2013/4/15

#
i am trying to make a chess game but when i click on the object it doesnt get selected , i have attached the code of a black pawn , but it is malfunctioning , i cant find the problem
 int a;
    MouseInfo mouse;
   public void act() 
    {
       if(((board)getWorld()).blackturn)
       {
           if(Greenfoot.mouseClicked(this))
           {
               move();
           }
       }
    }  
    public void move()
    {   
        mouse = Greenfoot.getMouseInfo();
        if(mouse!=null)
       { 
            a = Greenfoot.getMouseInfo().getButton();
                if(mouse.getY()==getY()-1 && mouse.getX()==getX() && b1==1)
                {
                    if(!checkblack(mouse.getX(), mouse.getY()))
                    {
                    setLocation(mouse.getX(), mouse.getY());
                    a=0;
                    
                    //((board)getWorld()).changeTurn();
                    }
                }
                if(getY()-2==4 && mouse.getY()==4 && mouse.getX()==getX() && b1==1)
                {
                    if(!checkblack(mouse.getX(), mouse.getY()))
                    {
                     setLocation(mouse.getX(), mouse.getY());
                     a=0;
                     //((board)getWorld()).changeTurn();
                    }
                }
                if(mouse.getX()==getX()+1 || mouse.getX()==getX()-1 && mouse.getY()==getY()-1)
                {
                    if(checkwhite(mouse.getX(), mouse.getY()))
                    {
                        setLocation(mouse.getX(), mouse.getY());
                        a=0;
                        killWhite();
                        //((board)getWorld()).changeTurn();
                    }
                }
       } 
    }
    
     public boolean checkblack(int x,int y)
    {
       List ab = getWorld().getObjectsAt(x, y, black.class);
       if(ab!=null)
       return true;
       else
       return false;          
    }
    public boolean checkwhite(int x,int y)
    {
       List ab = getWorld().getObjectsAt(x, y, white.class);
       if(ab!=null)
       return true;
       else
       return false;          
    }
    public void killWhite()
    {
        List ab = getWorld().getObjectsAt(getX(), getY(), white.class);
        getWorld().removeObjects(ab);
    }
erdelf erdelf

2013/4/15

#
what is the worlds cell size?
jainmanish jainmanish

2013/4/17

#
its 9 X 9 and the cell size is 50
davmac davmac

2013/4/17

#
You only call the move() method if the mouse was clicked on this piece, but then you check the mouse co-ordinates (eg on line 19 and 29) and see if they are not equal to the current actor coordinates. If the mouse was clicked on the actor, then the mouse co-ordinates will always be the same as the actor coordinates. So, the conditions in the 'if' statements on lines 19 and 29 will never be 'true'. It's not clear exactly how you expect it / want it to work, perhaps if you explained some more?
jainmanish jainmanish

2013/4/17

#
what i want to do is , when i click on the object , the x(will make a new one) variable becomes true, Then when i click on another location on the board , it should perform the necessary checks for that position, how do i check a click on the world class
danpost danpost

2013/4/17

#
You can check for a click anywhere by using 'Greenfoot.mouseClicked(null)'. If it returns 'true', then use the Greenfoot class' 'getMouseInfo' method to get the mouse information and use the MouseInfo methods 'getX' and 'getY' to retrieve the location where the mouse was clicked (it will use your grid coordinates; not pixels, but cells, from top and left).
if (Greenfoot.mouseClicked(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    int mouseX = mouse.getX();
    int mouseY = mouse.getY();
    // etc.
jainmanish jainmanish

2013/4/18

#
Greenfoot.mouseClicked(null) worked thank you very much @danpost
You need to login to post a reply.