I've got a few years experience in Java and Slick2D, and I'm attempting to port a game over to libgdx due to the fact that it is a much better library. However, I'm having a simple issue with moving a sprite on the screen. There must be some paradigm I don't understand with this API. I've distilled my code into the following. It recognizes input and runs it through my entire entity and networking systems both to and from the server, and yet the sprite stays at the same location. Any help would be wonderful.
My create() method:
public void create() {
//Check to see if we are using external server or localhost
ClientNetworkManager.setLocalhostAsHost(Constant.useLocalhost, this);
//Begin application setup
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1280,900);
batch = new SpriteBatch();
//Load assets into memory
texture = new Texture(Gdx.files.internal("Sprites/Ash.png"));
//texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite = new Sprite(texture);
sprite.setSize(256,256);
sprite.setPosition(44, 666);
//Begin external network setup
setPlayerPointer(null);
this.connection = new ClientConnectionThread(this);
getClientConnectionThread().setCurrentPing(0);
}
My render() method:
public void render() {
//Render logic
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if (connection.isActive() && !Constant.performEssentialTasksOnly) {
InputManager.handlePlayerInput(this);
batch.setProjectionMatrix(camera.combined);
camera.update();
batch.begin();
batch.draw(sprite,playerPointer.getCurrentX(),playerPointer.getCurrentY());
System.out.println(playerPointer.getCurrentX());
batch.end();
}
}
The values for the getCurrentX/Y methods are all reporting correctly, so that's not the issue. I feel it must be something obvious.
You can call
sprite.setX(xpos);
to change the actual position.
If you are going to use a tiledmap, don't forget to push in the right SpriteBatch
, which should be returned by the renderer.getSpriteBatch()
method.
You aren't using the Sprite position to render it, you are actually using it like if it was a regular TextureRegion .
batch.draw(sprite,playerPointer.getCurrentX(),playerPointer.getCurrentY());
System.out.println(playerPointer.getCurrentX());
Where are you changing this playerPointer?
Do it more like this:
sprite.setPosition(playerPointer.getCurrentX(),playerPointer.getCurrentY())
sprite.draw(batch);
But again, the problem is that playerPointer doesnt have the correct values (they aren't changing).
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.