In my project I have my main character body. I want it to move down on the y-axis as long as the screen is pressed. I tried it with
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
jetski.applyForce(new Vector2(0,-1000), jetski.getWorldCenter(), true);
return true;
}
but that does it only once.
I assume there is a main loop in your project since it looks like you are developing a game. It is typically done using flag(-s) to represent the user input. Let's say we have a flag isTouched
. So, when the user first touches the display, isTouched = true
. Then when the user no longer touches the display, isTouched = false
. Finally, in your main loop if (isTouched) applyForce();
. Android API or libgdx (judging from tags) should be able to provide listeners for both onTouch
and onTouchRelease
events. Mind you they may be called something different.
Try this:
boolean isTouched = false;
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
isTouched = true;
return true;
}
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
isTouched = false;
return true;
}
public void update(float delta) {
// update other stuff
if (isTouched) {
jetski.applyForce(new Vector2(0,-1000), jetski.getWorldCenter(), true);
}
// update other stuff
}
You might want to change -1000 to something lower, because it will add -1000 force every frame, and maybe add a limit. Also, judging by the force(-1000) I'm guessing you didn't scale the world, so the physics might not work as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.