[英]Why is my Sprite not moving? LIbgdx/Android
Hi I'm currently programming a game where you have to avoid asteroids with a Spaceship. 嗨,我目前正在编写一款游戏,您必须避免使用太空飞船的小行星。 The spaceship is a sprite.
太空飞船是一个精灵。 Somehow the Sprite does not move?!
雪碧不动不动?! Here you can see a big part of my code to understand what I am trying to do.
在这里,您可以看到我的代码的很大一部分,以了解我要执行的操作。
Main Class: 主类:
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);
}
}
ScreenManager 屏幕管理器
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();
}
Entity Class: 实体类别:
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;
}
Spaceship Class: 飞船舱
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);
}
In this part of code, you only passing data to x & y variable and not to sprite position. 在这部分代码中,您仅将数据传递到x&y变量,而不传递到sprite位置。
x = Gdx.input.getX() - width / 2;
y = -Gdx.input.getY() + height / 2;
You must apply this to your current sprite. 您必须将其应用于当前的精灵。
sprite.setPosition(x, y); // or sprite.setCenter(x, y) if you want it to be centered position.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.