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

2013/4/19

Greenfoor.mousePressed() & Greenfoot.mouseClicked() , happening in one go

jainmanish jainmanish

2013/4/19

#
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);
    }
 
}
Gevater_Tod4711 Gevater_Tod4711

2013/4/19

#
Probably the problem is that the method Greenfoot.mousePressed() doesn't work like you want it to. This often happens if you use the mouseInfo methods or anything that has to do with the mouse. You should try to add a boolean that shows if the mouse is currently down or not:
1
2
3
4
5
6
if(Greenfoot.mousePressed(this)) {
    mouseDown = true;
}
else if(Greenfoot.mouseClicked(null)) {
    mouseDown = false;
}
And then you try to use this variable instead of Greenfoot.mousePressed to check whether the mouse is down or not. (only if the value changes from false to true the mouse has been pressed).
danpost danpost

2013/4/19

#
What you probably want is something more like the following:
1
2
3
4
5
6
7
8
if (Greenfoot.mouseClicked(this)) selected = !selected;
if (selected && Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    mousex = mouse.getX(); 
    mousey = mouse.getY(); 
    move();
}
although, lines 4 through 6 can be done in the 'move' method and you can then remove 'mousex' and 'mousey' as fields in the class.
davmac davmac

2013/4/19

#
I'd also suggest that you want to 'return' at line 35 since otherwise the next 'if' block will always be processed if a click causes the piece to be selected, i.e: - line 31 sets selected=true - line 38 - in "if(selected)" - the condition is now true. - line 49 - in "if(Greenfoot.mouseClicked(null))" - the condition is also true.
You need to login to post a reply.