游戏中的LibGDX暂停图像

时间:2016-08-20 07:36:59

标签: java android libgdx

我是libgdx的新手,我尝试了几种添加暂停功能和图像的方法。任何人都可以纠正我的错误吗?我被困了

package com.badlogic.drop;

import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class GameScreen implements Screen {
final Drop game;

Texture dropImage;
Texture bucketImage;
Texture pauseImage;
Sound dropSound;
Music rainMusic;
OrthographicCamera camera;
Rectangle bucket;
Rectangle pause;
Array<Rectangle> raindrops;
long lastDropTime;
int dropsGathered;

public GameScreen(final Drop gam) {
    this.game = gam;

    // load the images for the droplet and the bucket, 64x64 pixels each
    dropImage = new Texture(Gdx.files.internal("droplet.png"));
    bucketImage = new Texture(Gdx.files.internal("bucket.png"));
    pauseImage = new Texture(Gdx.files.internal("pause.png"));
    // load the drop sound effect and the rain background "music"
    dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
    rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
    rainMusic.setLooping(true);

    // create the camera and the SpriteBatch
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 800, 480);

    // create a Rectangle to logically represent the bucket
    bucket = new Rectangle();
    bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
    bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
    // the bottom screen edge
    bucket.width = 64;
    bucket.height = 64;

    pause = new Rectangle();
    pause.x =700;
    pause.y =380;
    pause.width = 64;
    pause.height = 64;

    // create the raindrops array and spawn the first raindrop
    raindrops = new Array<Rectangle>();
    spawnRaindrop();
}

private void spawnRaindrop() {
    Rectangle raindrop = new Rectangle();
    raindrop.x = MathUtils.random(0, 800 - 64);
    raindrop.y = 480;
    raindrop.width = 64;
    raindrop.height = 64;
    raindrops.add(raindrop);
    lastDropTime = TimeUtils.nanoTime();
}

public enum State{Running,Paused}
State state = State.Running;
@Override
public void render(float delta) {
    switch(state) {
        case Running:
            update();
            break;
        case Paused:
            Gdx.graphics.setContinuousRendering(false);
            break;
    }
    draw();
}
public void update(){
    Gdx.graphics.requestRendering();
}
public void draw(){// clear the screen with a dark blue color. The
    // arguments to glClearColor are the red, green
    // blue and alpha component in the range [0,1]
    // of the color to be used to clear the screen.
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // tell the camera to update its matrices.
    camera.update();

    // tell the SpriteBatch to render in the
    // coordinate system specified by the camera.
    game.batch.setProjectionMatrix(camera.combined);

    // begin a new batch and draw the bucket and
    // all drops
    game.batch.begin();
    game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
    game.batch.draw(bucketImage, bucket.x, bucket.y);
    game.batch.draw(pauseImage, pause.x, pause.y);
    for (Rectangle raindrop : raindrops) {
        game.batch.draw(dropImage, raindrop.x, raindrop.y);
    }
    game.batch.end();

如何将按钮编码为游戏“

    // process user input
    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);
        bucket.x = touchPos.x - 64 / 2;
    }
    if (Gdx.input.isKeyPressed(Keys.LEFT))
        bucket.x -= 200 * Gdx.graphics.getDeltaTime();
    if (Gdx.input.isKeyPressed(Keys.RIGHT))
        bucket.x += 200 * Gdx.graphics.getDeltaTime();

    // make sure the bucket stays within the screen bounds
    if (bucket.x < 0)
        bucket.x = 0;
    if (bucket.x > 800 - 64)
        bucket.x = 800 - 64;

    // check if we need to create a new raindrop
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
        spawnRaindrop();

    // move the raindrops, remove any that are beneath the bottom edge of
    // the screen or that hit the bucket. In the later case we play back
    // a sound effect as well.
    Iterator<Rectangle> iter = raindrops.iterator();
    while (iter.hasNext()) {
        Rectangle raindrop = iter.next();
        raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
        if (raindrop.y + 64 < 0)
            iter.remove();
        if (raindrop.overlaps(bucket)) {
            dropsGathered++;
            dropSound.play();
            iter.remove();
        }
    }}   

@Override
public void hide() {
}

@Override
public void pause() {
}

@Override
public void resume() {
}  

CRY

1 个答案:

答案 0 :(得分:0)

有几项改进。

暂停游戏

通常的做法是创建一个名为update的新方法,您可以在其中转储所有逻辑(如移动纹理)。您可以从render循环调用此方法。 如果游戏暂停,你不会调用此方法。

我看到你已经做到了这一点。您无需调用任何setContinuousRendering方法。你的状态机应该工作。您也可以选择仅使用布尔值(例如isPaused)。

我还没有看到将状态设置为暂停的方法,所以我不知道你想如何测试你的暂停系统。 您必须按下按钮或使用控制台输入或立即暂停以进行测试。

精灵

我建议您使用Sprites而不是手动创建带纹理的矩形,这会让您的生活更轻松。 精灵基本上是纹理+矩形和一些渲染代码。

效果

我会调用Gdx.graphics.getDeltaTime()一次,然后将返回值保存在变量中。这样,该方法就不会被调用所需的所有值。

您使用TimeUtils.nanoTime()。我个人使用Java System.currentTimeMillis(),因为它的价值高出100倍。这样您就不必键入所有这些零。

由于您正在使用Scene2D,因此会自动调用render方法。它有一个名为delta的参数。您可以使用此变量而不是手动创建一个。 只需将update()方法更改为update(float delta)并传递变量。

draw方法中,您更新相机。如果你不移动相机,这是多余的。

祝你好运!

修改:我误解了你的问题。您可以使用Scene2D创建按钮。此按钮将触发可用于设置暂停状态的事件。 您正在屏幕内创建游戏,您应该使用舞台。查看有关Scene2D的更多信息。

编辑2 :格式化