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

2020/11/14

Make an object follow the cursor

Mousekip Mousekip

2020/11/14

#
I wanted the object of this class to start following the cursor when the you click with the mouse anywhere and stop when you release it. Though, for some reason the loop doesn´t end when released and also only puts the object where the cursor was when clicked and never follows it. If anyone knows, what the problem is, please respond. Thank you in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act() {
if (Greenfoot.mousePressed(null)) {
            World world = (World) getWorld();
            MouseInfo mouse = Greenfoot.getMouseInfo();
             
            while (!Greenfoot.mouseClicked(null)) {
                 
                setLocation(mouse.getX(), mouse.getY());
                world.repaint();
                 
            }
        }
  }
Super_Hippo Super_Hippo

2020/11/14

#
Change mousePressed to mouseDragged. Change while to if. You can remove lines 3 and 9.
danpost danpost

2020/11/15

#
mousePressed is only true when a mouse button changes state from up to down. It is not true when the button is held down. mouseDragged is only true when a button is down AND the mouse has moved. It is not true if the mouse has not moved. Using while as in line 6 will freeze the scenario until the mouse button is released. The following is if I understand what you are wanting correctly. What you need is a boolean to track the mouse button state; so, you can tell when it is being held down:
1
private boolean mseBtnDown;
You will also need to retain the last known position of the mouse (for when a button is down but the mouse doesn't move):
1
private int mseX, mseY;
Then you can have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MouseInfo mouse = Greenfoot.getMouseInfo();
// updatting button state
if (!mseBtnDown && Greenfoot.mousePressed(null)) mseBtnDown = true;
if (mseBtnDown && Greenfoot.mouseClicked(null)) mseBtnDown = false;
// for button down state
if (mseBtnDown)
{
    // updating mouse position
    if (mouseInfo != null)
    {
        mseX = mouse.getX();
        mseY = mouse.getY();
    }
    // moving
    turnTowards(mseX, mseY);
    move(3);
    setRotation(0);
}
Mousekip Mousekip

2020/11/15

#
Well, I changed the code, but it still only works when I click the mouse. If the mouse is not released, nothing will happen... It basically has the same effect as the following code, but I want it to move even when held down.
1
2
3
4
5
6
7
8
9
public void act() {
if (Greenfoot.mousePressed(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
 
            turnTowards(mouse.getX(), mouse.getY());
            move(3);
            setRotation(0);
        }
}
danpost danpost

2020/11/15

#
Mousekip wrote...
Well, I changed the code, but it still only works when I click the mouse. If the mouse is not released, nothing will happen... It basically has the same effect as the following code, but I want it to move even when held down.
I tested the code given. Found one error -- line 9 should be:
1
if (mouse != null)
Otherwise, it seems to work okay.
danpost danpost

2020/11/15

#
Mousekip wrote...
I changed the code, but it still only works when I click the mouse.
Please provide the entire class with changed code for review.
Mousekip Mousekip

2020/11/15

#
danpost wrote...
Please provide the entire class with changed code for review.
At the time, when I wrote the comment, I already changed it to mouse and it still didnt work. As requested here´s the code of the entire class (please excuse other flaws in the code, i´m clearly not that experienced with programming / the changed code begins in line 123):
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    int strenght = 5;
    int str = 50;
    int xachse = 70;
    int yachse = 98;
    int time = 140;
    int delay = 65;
    private boolean mseBtnDown;
    private int mseX, mseY;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        buttons();
        //fallen();
        jump();
    }   
 
    public void fallen() {
        if (getY() != 440) {
            Greenfoot.setSpeed(50);
            int i = 0;
            int y = 0;
            int x = 0;
            int initialY = getY();
            while (getY() <= 440 && getY() != 440) {
                x = x * x;
                x = -x;
 
                y = x;
                x = i;
 
                x++;
                i++;
 
                setLocation(getX(), initialY - y);
                if (getY() > 440 && getY() != 440) {
                    setLocation(getX(), 440);
                }
                Greenfoot.delay(1);
            }
            setLocation(getX(), 440);
            Greenfoot.setSpeed(65);
        }
    }
 
    public void buttons() {
        if (Greenfoot.isKeyDown("e")) {
            World world = (World) getWorld();
            while (Greenfoot.isKeyDown("e")) {
            }
            world.showText("Wähle Sprungweite 1-9 (aktuell: " + strenght + " )", 600, 50);
            world.repaint();
            while (!Greenfoot.isKeyDown("1") && !Greenfoot.isKeyDown("2") && !Greenfoot.isKeyDown("3")  && !Greenfoot.isKeyDown("4") && !Greenfoot.isKeyDown("5") && !Greenfoot.isKeyDown("6") && !Greenfoot.isKeyDown("7") && !Greenfoot.isKeyDown("8") && !Greenfoot.isKeyDown("9") && !Greenfoot.isKeyDown("escape")) {
            }
            while (Greenfoot.isKeyDown("1") || Greenfoot.isKeyDown("2") || Greenfoot.isKeyDown("3")  || Greenfoot.isKeyDown("4") || Greenfoot.isKeyDown("5") || Greenfoot.isKeyDown("6") || Greenfoot.isKeyDown("7") || Greenfoot.isKeyDown("8") || Greenfoot.isKeyDown("9") || Greenfoot.isKeyDown("escape")) {
            }
            strenght = Integer.parseInt(Greenfoot.getKey());
            world.showText(null, 600, 50);
            world.repaint();
            str = strenght * 10;
 
            if (str == 10) {
                xachse = 32;
                yachse = 102;
                time = 64;
                delay = 58;
            }else if (str == 20) {
                xachse = 45;
                yachse = 101;
                time = 90;
                delay = 60;
            }else if (str == 30) {
                xachse = 54;
                yachse = 97;
                time = 108;
                delay = 62;
            }else if (str == 40) {
                xachse = 63;
                yachse = 99;
                time = 126;
                delay = 64;
            }else if (str == 50) {
                xachse = 70;
                yachse = 98;
                time = 140;
                delay = 65;
            }else if (str == 60) {
                xachse = 77;
                yachse = 99;
                time = 154;
                delay = 65;
            }else if (str == 70) {
                xachse = 84;
                yachse = 101;
                time = 168;
                delay = 66;
            }else if (str == 80) {
                xachse = 89;
                yachse = 99;
                time = 178;
                delay = 66;
            }else if (str == 90) {
                xachse = 95;
                yachse = 100;
                time = 190;
                delay = 67;
            }else {
                System.out.println("wtf ru doing?");
            }
        }
        if (Greenfoot.mousePressed(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            // updatting button state
            if (!mseBtnDown && Greenfoot.mousePressed(null)) mseBtnDown = true;
            if (mseBtnDown && Greenfoot.mouseClicked(null)) mseBtnDown = false;
            // for button down state
            if (mseBtnDown)
            {
                // updating mouse position
                if (mouse != null)
                {
                    mseX = mouse.getX();
                    mseY = mouse.getY();
                }
                // moving
                turnTowards(mseX, mseY);
                move(3);
                setRotation(0);
            }
        }
        if (!Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d")) {
            setLocation(getX() - 1, getY());
            setRotation(getRotation() - 1);
            Greenfoot.delay(1); 
        }
        if (!Greenfoot.isKeyDown("space") && !Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("d")) {
            setLocation(getX() + 1, getY());
            setRotation(getRotation() + 1);
            Greenfoot.delay(1);           
        }
    }
 
    public int stärke (int stärke) {
 
        if (stärke < 10 && stärke > 0) {
            return stärke;
        }else {
            return 5;
        }
 
    }
 
    public void jump() {
        if (Greenfoot.isKeyDown("space")) {
            int height = 0;
            int speed = 0;
 
            int y;
            int x = 0;
            int initialX = getX();
            int initialY = getY();
 
            if (Greenfoot.isKeyDown("space") && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d")) {
                Greenfoot.setSpeed(49);
                for (int i = 0; i <= 20; i++) {
 
                    x = x - 10;
                    x = x * x;
                    x = -x;
                    x = x + 100;
 
                    y = x;
                    x = i;
 
                    x = x + 1;
 
                    setLocation(getX(), initialY - y);
 
                    Greenfoot.delay(1);
 
                }
                Greenfoot.setSpeed(65);
            }else if (Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d")) {
                Greenfoot.setSpeed(delay);
                for (int i = 0; i <= time; i++) {
 
                    x = x - xachse;
                    x = x * x;
                    x = -x / str;
                    x = x + yachse;
 
                    y = x;
                    x = i;
 
                    x = x + 1;
 
                    setLocation(initialX - x, initialY - y);
                    setRotation(getRotation() - 1);
                    Greenfoot.delay(1);
                }
                Greenfoot.setSpeed(65);
            }else if (Greenfoot.isKeyDown("space") && !Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("d")) {
                Greenfoot.setSpeed(delay);
                for (int i = 0; i <= time; i++) {
 
                    x = x - xachse;
                    x = x * x;
                    x = -x / str;
                    x = x + yachse;
 
                    y = x;
                    x = i;
 
                    x = x + 1;
 
                    setLocation(initialX + x, initialY - y);
                    setRotation(getRotation() + 1);
                    Greenfoot.delay(1);
                }
                Greenfoot.setSpeed(65);
            }else if (Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("d")) {
                Greenfoot.setSpeed(50);
                for (int i = 0; i <= 20; i++) {
 
                    x = x - 10;
                    x = x * x;
                    x = -x;
                    x = x + 100;
 
                    y = x;
                    x = i;
 
                    x = x + 1;
 
                    setLocation(getX(), initialY - y);
                    Greenfoot.delay(1);
                }
                Greenfoot.setSpeed(65);
            }
        }   
    }
 
    public void funktion() {
        int y = 100;
        int x = 10;
        int initialY = getY();
        for (int i = 10; i <= 20; i++) {
            x = x - 10;
            x = x * x;
            x = -x;
            x = x + 100;
 
            y = x;
            x = i;
 
            x++;
 
            System.out.println("X = " + x);
            System.out.println("Y = " + y);
        }
    }
}
danpost danpost

2020/11/15

#
Mousekip wrote...
At the time, when I wrote the comment, I already changed it to mouse and it still didnt work. As requested here´s the code of the entire class
Remove lines 123 and 141.
Mousekip Mousekip

2020/11/15

#
danpost wrote...
Mousekip wrote...
At the time, when I wrote the comment, I already changed it to mouse and it still didnt work. As requested here´s the code of the entire class
Remove lines 123 and 141.
It now works perfectly! I should have noticed that when you wrote about what mousePressed means in detail. Thank you very much!
You need to login to post a reply.