我正在学习libgdx开发,而我在编辑演示时遇到了麻烦:
我正在编辑一个正常的飞鸟演示,我删除了岩石,我添加了一些子弹(从右到左)这里是我的代码:
public class FGame implements Screen {
// General constants
private static final float PLANE_JUMP_IMPULSE = 350;
private static final float GRAVITY = -20;
private static final float PLANE_VELOCITY_X = 200;
private static final float PLANE_START_Y = 240;
private static final float PLANE_START_X = 50;
private static final float BULLET_SPEED_X = 5;
// Timer constants
private static final float TIME_BULLET_REPEAT = 2;
private static final float TIME_FUEL_REPEAT = 5;
private static final float TIME_FUEL_DECREASE = 5;
// Render && Batch
ShapeRenderer shapeRenderer;
SpriteBatch batch;
// Camera
OrthographicCamera camera;
OrthographicCamera uiCamera;
// Texture from Background && Ceiling
Texture background;
TextureRegion ground;
float groundOffsetX = 0;
TextureRegion ceiling;
// Plane animation && position && speed && gravity
Animation plane;
Vector2 planePosition = new Vector2();
Vector2 planeVelocity = new Vector2();
float planeStateTime = 0;
Vector2 gravity = new Vector2();
// Ready && Game Over signs
TextureRegion ready;
TextureRegion gameOver;
// Font used for Fuel% && Score
BitmapFont font;
// Bullet texture && Sprite && ArrayList && timer from bullets
Texture bulletTexture;
Sprite bulletSprite;
ArrayList<Bullet> bulletList;
float bulletTime;
// Fuel texture && Sprite && ArrayList && Timer
Texture fuelTexture;
Sprite fuelSprite;
ArrayList<Fuel> fuelList;
float fuelTime;
// GameState && Rectangles
GameState gameState = GameState.Start;
int score = 0;
Rectangle rect1 = new Rectangle();
// Music and effects
Music music;
Sound explode;
private void resetWorld() { // Used in the first time and in GameOver
score = 0;
groundOffsetX = 0;
planePosition.set(PLANE_START_X, PLANE_START_Y);
planeVelocity.set(0, 0);
gravity.set(0, GRAVITY);
camera.position.x = 400;
if (!bulletList.isEmpty())
bulletList.removeAll(bulletList); // Remove objects every new Game
if (!fuelList.isEmpty())
fuelList.removeAll(fuelList);
}
private void updateWorld() { // Used every frame update
float deltaTime = Gdx.graphics.getDeltaTime();
planeStateTime += deltaTime;
bulletTime += deltaTime;
fuelTime += deltaTime;
if (Gdx.input.justTouched()) {
if (gameState == GameState.Start) {
gameState = GameState.Running;
}
if (gameState == GameState.Running) {
planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE);
}
if (gameState == GameState.GameOver) {
gameState = GameState.Start;
resetWorld();
}
}
if (gameState != GameState.Start) {
planeVelocity.add(gravity);
// Timer functions
if (bulletTime >= TIME_BULLET_REPEAT) { // Create bullets over time.
bulletTime -= TIME_BULLET_REPEAT;
createBullet();
}
if (fuelTime >= TIME_FUEL_REPEAT) { // Create fuel over time
fuelTime -= TIME_FUEL_REPEAT;
createFuel();
}
if (!bulletList.isEmpty()) { // bullets movement.
for (Bullet bullet : bulletList) {
bullet.bulletSprite.setX(bullet.bulletSprite.getX()
- BULLET_SPEED_X);
}
}
}
planePosition.mulAdd(planeVelocity, deltaTime);
camera.position.x = planePosition.x + 350;
if (camera.position.x - groundOffsetX > ground.getRegionWidth() + 400) {
groundOffsetX += ground.getRegionWidth();
}
rect1.set(planePosition.x + 20, planePosition.y,
plane.getKeyFrames()[0].getRegionWidth() - 20,
plane.getKeyFrames()[0].getRegionHeight());
if (planePosition.y < ground.getRegionHeight() - 20
|| planePosition.y + plane.getKeyFrames()[0].getRegionHeight() > 480 - ground
.getRegionHeight() + 20) {
if (gameState != GameState.GameOver)
explode.play();
gameState = GameState.GameOver;
planeVelocity.x = 0;
}
}
private void createBullet() {
bulletList.add(new Bullet(Gdx.graphics.getWidth(), bulletSprite));
for (Bullet bullet : bulletList) {
if (!bullet.isSet()) {
bullet.bulletSprite.setBounds(planePosition.x + 1000,
bullet.getY(), 100, 100);
bullet.Set();
score++;
}
}
}
private void createFuel() {
fuelList.add(new Fuel(Gdx.graphics.getWidth(), fuelSprite));
for (Fuel fuel : fuelList) {
if (!fuel.isSet()) {
fuel.fuelSprite.setBounds(planePosition.x + 1000,
fuel.getY(), 50, 50);
fuel.Set();
score++;
}
}
}
private void drawWorld() {
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(background, camera.position.x - background.getWidth() / 2, 0);
batch.draw(ground, groundOffsetX, 0);
batch.draw(ground, groundOffsetX + ground.getRegionWidth(), 0);
batch.draw(ceiling, groundOffsetX, 480 - ceiling.getRegionHeight());
batch.draw(ceiling, groundOffsetX + ceiling.getRegionWidth(),
480 - ceiling.getRegionHeight());
batch.draw(plane.getKeyFrame(planeStateTime), planePosition.x,
planePosition.y);
if (!bulletList.isEmpty()) { // Draw Bullets
for (Bullet bullet : bulletList) {
bullet.bulletSprite.draw(batch);
}
} // End Draw Bullets
if (!bulletList.isEmpty()) { // Draw Fuel
for (Fuel fuel : fuelList) {
fuel.fuelSprite.draw(batch);
}
} // End Draw Fuel
batch.end();
batch.setProjectionMatrix(uiCamera.combined);
batch.begin();
if (gameState == GameState.Start) {
batch.draw(ready,
Gdx.graphics.getWidth() / 2 - ready.getRegionWidth() / 2,
Gdx.graphics.getHeight() / 2 - ready.getRegionHeight() / 2);
}
if (gameState == GameState.GameOver) {
batch.draw(
gameOver,
Gdx.graphics.getWidth() / 2 - gameOver.getRegionWidth() / 2,
Gdx.graphics.getHeight() / 2 - gameOver.getRegionHeight()
/ 2);
}
if (gameState == GameState.GameOver || gameState == GameState.Running) {
font.draw(batch, "" + score, Gdx.graphics.getWidth() / 2,
Gdx.graphics.getHeight() - 60);
}
batch.end();
}
static enum GameState { // Enum for gameStates
Start, Running, GameOver
}
@Override
public void show() { // Method called instead of create()
// Loading shapeRenderer && batch
shapeRenderer = new ShapeRenderer();
batch = new SpriteBatch();
// Loading camera
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
uiCamera = new OrthographicCamera();
uiCamera.setToOrtho(false, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
uiCamera.update();
// Loading Font
font = new BitmapFont(Gdx.files.internal("arial.fnt"));
// Loading Background && Ceiling
background = new Texture("background.png");
ground = new TextureRegion(new Texture("ground.png"));
ceiling = new TextureRegion(ground);
ceiling.flip(true, true);
// Plane frames && animation
Texture frame1 = new Texture("plane1.png");
frame1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
Texture frame2 = new Texture("plane2.png");
Texture frame3 = new Texture("plane3.png");
plane = new Animation(0.05f, new TextureRegion(frame1),
new TextureRegion(frame2), new TextureRegion(frame3),
new TextureRegion(frame2));
plane.setPlayMode(PlayMode.LOOP);
// Bullet Textures && List
bulletTexture = new Texture("img/bullet.png");
bulletSprite = new Sprite(bulletTexture);
bulletList = new ArrayList<Bullet>();
// Fuel Textures && List
fuelTexture = new Texture("img/fuel.png");
fuelSprite = new Sprite(fuelTexture);
fuelList = new ArrayList<Fuel>();
// Loading Ready && gameOver Signs
ready = new TextureRegion(new Texture("ready.png"));
gameOver = new TextureRegion(new Texture("gameover.png"));
// Music load && play
music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
music.setLooping(true);
music.play();
// Effect load
explode = Gdx.audio.newSound(Gdx.files.internal("explode.wav"));
// Call resetWorld for the first time.
resetWorld();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
updateWorld();
drawWorld();
}
子弹被正确绘制但由于某种原因,每次创建子弹时x的速度都会增加。
这是我编辑项目符号的x移动的地方:
if (!bulletList.isEmpty()) { // bullets movement.
for (Bullet bullet : bulletList) {
bullet.bulletSprite.setX(bullet.bulletSprite.getX()
- BULLET_SPEED_X);
}
这是我的子弹课程
public class Bullet {
private float y;
private float x;
private boolean bulletSet = false;
public Sprite bulletSprite;
public Bullet(float x, Sprite bulletSprite) {
this.x = x;
this.bulletSprite = bulletSprite;
y = MathUtils.random(50, 450);
}
public boolean isSet() {
return bulletSet;
}
public void Set() {
bulletSet = true;
}
public float getY () {
return y;
}
public float getX () {
return x;
}
你能找到问题吗?
答案 0 :(得分:2)
问题出在这里,在你的Bullet构造函数中:
this.bulletSprite = bulletSprite;
所有项目符号实例都引用相同的精灵,因此您反复更改相同的精灵。相反,在将精灵分配给新子弹时,应该复制它。将其更改为
this.bulletSprite = new Sprite(bulletSprite);
顺便说一句,看起来您对if (!bulletList.isEmpty())
的所有来电都是多余的,可以删除。 (只是为了简化你的代码)