简体   繁体   中英

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 = 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM