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

2017/5/20

How do I make an object move continuously?

alema173 alema173

2017/5/20

#
If I want a basketball to move continuously up once the spacebar has been pressed how would I do that? Because the isKeyDown method only lets the basketball move while the key is down and I need the basketball to move upwards without pause once the key has been pressed once...
FutureCoder FutureCoder

2017/5/20

#
create a variable and replace the line that makes the basket ball move up with variable = variable +1 then use if (variable > 0) { place code to move up here. }
alema173 alema173

2017/5/20

#
if(Greenfoot.isKeyDown("space")) { isKeyPressed = true; } if(isKeyPressed ) { int p = meter.getPower(); move(p); } This is my code right now and it won't work
alema173 alema173

2017/5/20

#
I'm still having problems getting my basketball in my game to move upwards continuously once the spacebar has been hit.
danpost danpost

2017/5/20

#
You are not showing all that you currently have. If what you are showing was all that you had, then it should move continuously once the space is pressed. The other possibility is the meter control. Show the class code for the 'meter' object.
Yehuda Yehuda

2017/5/21

#
danpost wrote...
Show the class code for the 'meter' object.
...Using code tags.
FutureCoder FutureCoder

2017/5/21

#
alema173 wrote...
if(Greenfoot.isKeyDown("space")) { isKeyPressed = true; } if(isKeyPressed ) { int p = meter.getPower(); move(p); } This is my code right now and it won't work
this won't work because isKeyPressed will only equal true, if the key is pressed, you have to make an integer and that will work
danpost danpost

2017/5/21

#
FutureCoder wrote...
< Quote Omitted >]this won't work because isKeyPressed will only equal true, if the key is pressed, you have to make an integer and that will work
Not true -- if the initial value of 'isKeyPressed' is false. The value is changed to true to indicate that the "space" key was pressed. By changing its value the second 'if' statement will begin to return true and the actor will start moving (provided that 'meter.getPower()' returns a non-zero value).
Yehuda Yehuda

2017/5/21

#
@FutureCoder Why would there be a difference in the type of variable used to indicate the key has been pressed, in the beginning the boolean starts off false (0), then when the space bar is pressed it becomes true (1). Since the only values of your int are 0 and 1 it makes more sense to use a boolean.
danpost danpost

2017/5/21

#
Yehudain wrote...
the beginning the boolean starts off false (0), then when the space bar is pressed it becomes true (1). Since the only values of your int are 0 and 1 it makes more sense to use a boolean.
Boolean values only have a true or a false value. The '1' and '0' referred to are internal computer bit-stored information and are not translated as actual values for a boolean field (just saying, so as not to confuse).
Yehuda Yehuda

2017/5/21

#
danpost wrote...
(just saying, so as not to confuse).
I won't say anything.
FutureCoder FutureCoder

2017/5/22

#
alema173 wrote...
if(Greenfoot.isKeyDown("space")) { isKeyPressed = true; } if(isKeyPressed ) { int p = meter.getPower(); move(p); } This is my code right now and it won't work
try this code to get the object to move up
1
2
3
int ypos = getY();
ypos = ypos - 4;
setLocation(getX(), ypos);
Yehuda Yehuda

2017/5/22

#
FutureCoder wrote...
try this code to get the object to move up <Code Omitted>
The following is the same code:
1
setLocation(getX(), getY() - 4);
I don't think the problem discussed here is moving, it seems to me that the object is supposed to move after the space bar is pressed (to which a solution has been given).
You need to login to post a reply.