我是这个框架的新手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
}
}
答案 0 :(得分:0)
您的Sprite类只是动画的参考教程。如果你想播放可以与舞台一起使用的动画,你可以在GameView类中使用Stage。所以删除你的Sprite类。只保留扩展Game类的GameView类。在create方法中,创建一个AnimationActor对象并添加到stage。
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();
}
}
对于gamePlay的背景,或者您可以通过传递所需背景的纹理并添加到舞台来说明屏幕包装图像对象。在将任何其他Actor添加到舞台之前,需要首先添加背景图像。