简体   繁体   English

在LibGDX中移动精灵

[英]Moving a sprite in LibGDX

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. 我在Java和Slick2D方面有几年的经验,由于它是一个更好的库,因此我试图将游戏移植到libgdx上。 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. 这个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. 它可以识别输入,并通过我的整个实体和网络系统(往返于服务器)运行输入,但sprite仍位于同一位置。 Any help would be wonderful. 任何帮助都会很棒。

My create() method: 我的create()方法:

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. getCurrentX / Y方法的值均正确报告,因此这不是问题。 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. 如果要使用tiledmap,请不要忘记推入正确的SpriteBatch ,它应该由renderer.getSpriteBatch()方法返回。

You aren't using the Sprite position to render it, you are actually using it like if it was a regular TextureRegion . 您没有使用Sprite位置来渲染它,实际上是像在使用常规TextureRegion一样使用它。

batch.draw(sprite,playerPointer.getCurrentX(),playerPointer.getCurrentY());
System.out.println(playerPointer.getCurrentX());

Where are you changing this playerPointer? 您在哪里更改此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). 但是同样,问题是playerPointer没有正确的值(它们没有改变)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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