[英]Slow down on tap?
基本上,我正在開發一個使用libGDX
。 我有一個character
,該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
來在發生單擊時切換慢速和快速。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.