基本上,我正在开发使用libGDX
的游戏。我有一个character
,它会自动前进。以及用户在屏幕上点击/单击的内容,我希望character
的速度降低。我似乎无法弄清楚该如何做。任何帮助都会很棒。
我的ScrollHandler
:
package com.kilobolt.GameObjects;
import com.kilobolt.GameWorld.GameWorld;
import com.kilobolt.ZBHelpers.AssetLoader;
public class ScrollHandler {
private Grass frontGrass, backGrass;
private Pipe pipe1, pipe2, pipe3;
public static final int SCROLL_SPEED = -1000;
public static final int PIPE_GAP = 49;
private GameWorld gameWorld;
public ScrollHandler(GameWorld gameWorld, float yPos) {
this.gameWorld = gameWorld;
frontGrass = new Grass(0, yPos, 143, 11, SCROLL_SPEED);
backGrass = new Grass(frontGrass.getTailX(), yPos, 143, 11,
SCROLL_SPEED);
pipe1 = new Pipe(210, 0, 22, 60, SCROLL_SPEED, yPos);
pipe2 = new Pipe(pipe1.getTailX() + PIPE_GAP, 0, 22, 70, SCROLL_SPEED,
yPos);
pipe3 = new Pipe(pipe2.getTailX() + PIPE_GAP, 0, 22, 60, SCROLL_SPEED,
yPos);
}
public void update(float delta) {
// Update our objects
frontGrass.update(delta);
backGrass.update(delta);
pipe1.update(delta);
pipe2.update(delta);
pipe3.update(delta);
// Check if any of the pipes are scrolled left,
// and reset accordingly
if (pipe1.isScrolledLeft()) {
pipe1.reset(pipe3.getTailX() + PIPE_GAP);
} else if (pipe2.isScrolledLeft()) {
pipe2.reset(pipe1.getTailX() + PIPE_GAP);
} else if (pipe3.isScrolledLeft()) {
pipe3.reset(pipe2.getTailX() + PIPE_GAP);
}
// Same with grass
if (frontGrass.isScrolledLeft()) {
frontGrass.reset(backGrass.getTailX());
} else if (backGrass.isScrolledLeft()) {
backGrass.reset(frontGrass.getTailX());
}
}
public void stop() {
frontGrass.stop();
backGrass.stop();
pipe1.stop();
pipe2.stop();
pipe3.stop();
}
public boolean collides(Bird bird) {
if (!pipe1.isScored()
&& pipe1.getX() + (pipe1.getWidth() / 2) < bird.getX()
+ bird.getWidth()) {
addScore(1);
pipe1.setScored(true);
AssetLoader.coin.play();
} else if (!pipe2.isScored()
&& pipe2.getX() + (pipe2.getWidth() / 2) < bird.getX()
+ bird.getWidth()) {
addScore(1);
pipe2.setScored(true);
AssetLoader.coin.play();
} else if (!pipe3.isScored()
&& pipe3.getX() + (pipe3.getWidth() / 2) < bird.getX()
+ bird.getWidth()) {
addScore(1);
pipe3.setScored(true);
AssetLoader.coin.play();
}
return (pipe1.collides(bird) || pipe2.collides(bird) || pipe3
.collides(bird));
}
private void addScore(int increment) {
gameWorld.addScore(increment);
}
public Grass getFrontGrass() {
return frontGrass;
}
public Grass getBackGrass() {
return backGrass;
}
public Pipe getPipe1() {
return pipe1;
}
public Pipe getPipe2() {
return pipe2;
}
public Pipe getPipe3() {
return pipe3;
}
}
我的
Bird
:package com.kilobolt.GameObjects;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Vector2;
import com.kilobolt.ZBHelpers.AssetLoader;
public class Bird {
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
private float rotation;
private int width;
private int height;
private boolean isAlive;
private Circle boundingCircle;
public Bird(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(0, 0);
acceleration = new Vector2(0, 460);
boundingCircle = new Circle();
isAlive = true;
}
public void update(float delta) {
velocity.add(acceleration.cpy().scl(delta));
if (velocity.y > 200) {
velocity.y = 200;
}
position.add(velocity.cpy().scl(delta));
// Set the circle's center to be (9, 6) with respect to the bird.
// Set the circle's radius to be 6.5f;
boundingCircle.set(position.x + 9, position.y + 6, 6.5f);
}
public boolean isFalling() {
return velocity.y > 110;
}
public boolean shouldntFlap() {
return velocity.y > 70 || !isAlive;
}
public void onClick() {
if (isAlive) {
AssetLoader.flap.play();
velocity.y = -140;
}
}
public void die() {
isAlive = false;
velocity.y = 0;
}
public void decelerate() {
acceleration.y = 0;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public float getRotation() {
return rotation;
}
public Circle getBoundingCircle() {
return boundingCircle;
}
public boolean isAlive() {
return isAlive;
}
}
最佳答案
这是我的建议:
每当MouseListener
检测到点击时,请使其沿playerSpeed -=
或playerSpeed = playerSpeed - *some other number*
的方向运行。这样,无论何时发生点击,播放器都会放慢速度。
现在,如果您想拥有它,以便在发生单击时播放器减速,而在再次发生单击时播放器再次加速,请使用boolean
在发生单击时切换慢速和快速。