i want to move a peice after selected it , then clicking on the position i want it to move,
i am first checking if the path is empty or not
but as soon as i click on it the variable(selected) changes from false to true , but changes back again within that 1 click , what do i do ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import greenfoot.*; import java.util.List; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class brook here. * * @author (your name) * @version (a version number or a date) */ public class brook extends black { /** * Act - do whatever the brook wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ boolean selected = false ; MouseInfo mouse; int mousex; int mousey; boolean test1= false ; boolean test2= false ; boolean test3= false ; boolean test4= false ; public void act() { if (((board)getWorld()).blackturn) { if (Greenfoot.mousePressed( this )) { if (!selected) selected = true ; else selected = false ; test3= true ; } if (selected) { if (Greenfoot.mouseClicked( null )) { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse!= null ) { mousex = mouse.getX(); mousey = mouse.getY(); move(); test4= true ; } } } } } public void move() { boolean clear= true ; if (mousex>getX() && mousey==getY()) { for ( int i=getX()+ 1 ;i<mousex;i++) { if (checkblack(i, 0 ) || checkwhite(i, 0 )) { clear= false ; test1= true ; } } } else { selected= false ; test2= true ; } if (clear && selected) { setLocation(mousex, mousey); selected= false ; if (checkwhite( 0 , 0 )) { killWhite(); } ((board)getWorld()).changeTurn(); } } public boolean checkblack( int x, int y) { Actor ab = getOneObjectAtOffset(x, y, black. class ); if (ab!= null ) return true ; else return false ; } public boolean checkwhite( int x, int y) { Actor ab = getOneObjectAtOffset(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); } } |