【问题标题】:How to insert background image on sprite class in libGDX?如何在 libGDX 的精灵类上插入背景图像?
【发布时间】:2017-02-20 07:27:16
【问题描述】:

我是这个框架 libGDX 的新手,我将它用于我的自上而下的游戏。我已经搜索过,但我的代码不起作用。如何在精灵类中插入背景图像,或者如何在另一个活动中导入精灵类? 这是我的代码。谢谢并提前:)

Sprite.java

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Sprite implements ApplicationListener {
    // Constant rows and columns of the sprite sheet
    private static final int FRAME_COLS = 5, FRAME_ROWS = 1;

    // Objects used
    Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
    Texture walkSheet;
    SpriteBatch spriteBatch;

    // A variable for tracking elapsed time for the animation
    float stateTime;

@Override
public void create() {

    // Load the sprite sheet as a
    walkSheet = new Texture(Gdx.files.internal("cat2.png"));
    // Use the split utility method to create a 2D array of TextureRegions. This is
    // possible because this sprite sheet contains frames of equal size and they are
    // all aligned.
    TextureRegion[][] tmp = TextureRegion.split(walkSheet,
            walkSheet.getWidth() / FRAME_COLS,
            walkSheet.getHeight() / FRAME_ROWS);

    // Place the regions into a 1D array in the correct order, starting from the top
    // left, going across first. The Animation constructor requires a 1D array.

    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
    int index = 0;
    for (int i = 0; i < FRAME_ROWS; i++) {
        for (int j = 0; j < FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
        }
    }
    // Initialize the Animation with the frame interval and array of frames
    walkAnimation = new Animation<TextureRegion>(0.075f, walkFrames);

    // Instantiate a SpriteBatch for drawing and reset the elapsed animation
    // time to 0
    spriteBatch = new SpriteBatch();
    stateTime = 0f;
}

@Override
public void resize(int width, int height) {

}

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time

    // Get current frame of animation for the current stateTime
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    spriteBatch.begin();
    spriteBatch.draw(currentFrame, 50, 50); // Draw current frame at (50, 50)
    spriteBatch.end();
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() { // SpriteBatches and Textures must always be disposed
    spriteBatch.dispose();
    walkSheet.dispose();
  }
}

Gameview.java

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public class Gameview extends Game {

private Stage stage;
private Texture myTexture;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton button;

@Override
public void create()
{
    myTexture = new Texture(Gdx.files.internal("floor.png"));
    myTextureRegion = new TextureRegion(myTexture);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    button = new ImageButton(myTexRegionDrawable); //Set the button up

    stage = new Stage(new ScreenViewport()); //Set up a stage for the ui
    stage.addActor(button); //Add the button to the stage to perform rendering and take input.
    Gdx.input.setInputProcessor(stage); //Start taking input from the ui
}

@Override
public void render()
{
    //Clear the screen, set the clear color, yada, yada
    stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
    stage.draw(); //Draw the ui
}
}

【问题讨论】:

  • Sprite 是 Libgdx 中的预定义类,为什么不用另一个名字。
  • 你能给我@AbhishekAryan 先生一个样本或教程吗?我有 2 个类,一个是游戏视图和精灵类。我如何在我的游戏视图类中渲染精灵类?
  • 为什么你的 Sprite 类实现 ApplicationListener ?
  • 在你的游戏中选择任意一个Game/ApplicationAdapter/ApplicationListener
  • 我只是按照他们使用 ApplicationListener (github.com/libgdx/libgdx/wiki/2D-Animation) 的教程进行操作

标签: android libgdx sprite


【解决方案1】:

您的 Sprite 类只是动画的参考教程。如果你想播放可以与舞台一起使用的动画,你在 GameView 类中使用舞台。所以删除你的 Sprite 类。只保留扩展 Game 类的 GameView 类。在 create 方法中创建 AnimationActor 的对象并添加到舞台。

public class AnimationActor extends Actor {

    Texture walkSheet;
    float stateTime;
    private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
    Animation<TextureRegion> walkAnimation;
    TextureRegion reg;

    public AnimationActor() {

        // Load the sprite sheet as a
        walkSheet = new Texture(Gdx.files.internal("cat2.png"));
        // Use the split utility method to create a 2D array of TextureRegions. This is
        // possible because this sprite sheet contains frames of equal size and they are
        // all aligned.
        TextureRegion[][] tmp = TextureRegion.split(walkSheet,
                walkSheet.getWidth() / FRAME_COLS,
                walkSheet.getHeight() / FRAME_ROWS);

        // Place the regions into a 1D array in the correct order, starting from the top
        // left, going across first. The Animation constructor requires a 1D array.

        TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }
        // Initialize the Animation with the frame interval and array of frames
        walkAnimation = new Animation<TextureRegion>(0.075f, walkFrames);
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        stateTime += delta;
        reg = walkAnimation.getKeyFrame(stateTime);
        if (walkAnimation.getPlayMode() == Animation.PlayMode.NORMAL && walkAnimation.isAnimationFinished(stateTime))
            end();
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(reg, getX(), getY(), getWidth() / 2, getHeight() / 2, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
    }

    public void setPosition(Position origin) {
        this.setPosition(getOriginX() - getWidth() / 2f, getOriginY() - getHeight() / 2f);
    }

    public void end() {
        if (getParent() != null)
            remove();
    }
}

对于您的游戏背景,或者您可以通过传递所需背景的纹理来说屏幕箱图像对象并添加到您的舞台。在将任何其他 Actor 添加到您的舞台之前,需要先添加背景图像。

【讨论】:

  • 感谢您的努力,但 this.setOrigin(origin.x, origin.y);错误:(11, 36) 错误: com.sun.xml.internal.ws.dump 包不存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多