您好我正在编写一款游戏,您必须避免使用太空飞船的小行星。宇宙飞船是精灵。不知怎的,雪碧不动了?!在这里,您可以看到我的代码的很大一部分,以了解我想要做什么。
主类:
package com.me.mygdxgame;
import screen.MenuScreen;
import screen.ScreenManager;
public class MyGdxGame implements ApplicationListener {
SpriteBatch batch;
public static int WIDTH = 800 , HEIGHT = 480; // resolution
@Override
public void create() {
batch = new SpriteBatch();
ScreenManager.setScreen(new MenuScreen());
}
@Override
public void dispose() {
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().dispose();
}
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().update();
}
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().render(batch);
}
}
屏幕管理
package screen;
public class ScreenManager {
private static Screen currentScreen;
public static void setScreen(Screen screen){
if(currentScreen != null){
currentScreen.dispose();
}
currentScreen = screen;
currentScreen.create();
}
public static Screen getCurrentScreen() {
return currentScreen;
}
}
MenuScreen:
public class MenuScreen extends Screen {
private OrthoCamera cam;
private Spaceship spaceship;
@Override
public void create() {
// TODO Auto-generated method stub
cam = new OrthoCamera();
spaceship = new Spaceship();
}
@Override
public void render(SpriteBatch batch) {
// TODO Auto-generated method stub
batch.setProjectionMatrix(cam.combined);
batch.begin();
spaceship.render(batch);
batch.end();
}
@Override
public void update() {
// TODO Auto-generated method stub
cam.update();
spaceship.update();
}
实体类:
public abstract class Entity {
protected float x = 0,y = 0;
protected Vector2 pos, direction;
protected Texture texture;
/*public Entity(Vector2 pos, Vector2 direction){
this.pos = pos;
this.direction = direction;
} */
public abstract void update();
public abstract void render(SpriteBatch batch);
public Vector2 getPostion() {
return pos;
}
太空船类:
public class Spaceship extends Entity {
Texture texture;
Sprite sprite;
float width, height;
float x = 0f, y = 0f;
public Spaceship() {
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
texture = new Texture("spritesheet.png");
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear );
TextureRegion region = new TextureRegion(texture, 0, 312, 258, 144);
sprite = new Sprite(region);
sprite.setSize(sprite.getWidth(), sprite.getHeight());
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
sprite.setPosition( MyGdxGame.WIDTH/2, MyGdxGame.HEIGHT/2 );
}
public void update() {
if (Gdx.input.isTouched()) {
x = Gdx.input.getX() - width / 2;
y = -Gdx.input.getY() + height / 2;
}
}
@Override
public void render(SpriteBatch batch) {
// TODO Auto-generated method stub
sprite.draw(batch);
}
答案 0 :(得分:0)
在这部分代码中,您只将数据传递给x& y变量而不是精灵位置。
x = Gdx.input.getX() - width / 2;
y = -Gdx.input.getY() + height / 2;
你必须将它应用于你当前的精灵。
sprite.setPosition(x, y); // or sprite.setCenter(x, y) if you want it to be centered position.