简体   繁体   中英

LibGDX - Not all sprites being rendered

I'm attempting to make a simple game, where multiple, thin platforms are created. I believe everything is set up correctly, and through debugging, I've found that the platforms are created properly (with random offsets). The issue is, only the last platform created is rendered, not all of them.

I use a class to manage the platforms, which contains an inner private class that holds platform information (position, sprite):

private class Platform{
    private Vector2 mVelocity;
    private Vector2 mPosition;

    private Sprite mTexture;

    public Platform(int x, int y, int w, int h){
        mPosition   = new Vector2(x, y);
        mVelocity   = new Vector2(0, 0);
        mTexture    = AssetLoader.mPlatformTexture;
        mTexture.setSize(w, h);
    }

    public void Update(float delta){
        mTexture.setPosition(mPosition.x, mPosition.y);
    }

    public Sprite GetTexture(){
        return mTexture;
    }
}

The platforms are created recursively and added to an array list, using random offsets:

public Platforms(){
    mPlatforms = new ArrayList<Platform>();
    AddPole(50);
}

private void AddPole(int x){
    if(x < Gdx.graphics.getWidth()){
        mPlatforms.add(new Platform(x, RandomInRange(-240, -400), 20, 480));
        AddPole(x + RandomInRange(100, 200));
    }
}

Finally, the platforms are updated and rendered:

@Override
public void render(float frameTime) {
    mRunTime += frameTime;

    mPlatforms.Update(frameTime);
    mCharacter.Update(frameTime);
    mCamera.update();

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    mBatcher.setProjectionMatrix(mCamera.combined);

    mBatcher.begin();
    mPlatforms.Render(mBatcher);
    mCharacter.Render(mBatcher);
    mBatcher.end();
}

...

public void Update(float delta){
    for(int i = 0; i < mPlatforms.size(); i++){
        mPlatforms.get(i).Update(delta);
    }
}

public void Render(SpriteBatch batcher){
    for(int i = 0; i < mPlatforms.size(); i++){
        mPlatforms.get(i).GetTexture().draw(batcher);
    }
}

But as can be seen, only one platform is drawn (the last one to be added).

在此处输入图片说明

Am I doing something a bit silly that is immediately obvious? I just started messing around with LibGDX so any help would be appreciated.

I don't know libgdx, but I think I know the solution to your problem.

Every platform has the same texture, namely:

mTexture    = AssetLoader.mPlatformTexture;

So now you update the positions of the platforms, and call:

mTexture.setPosition(mPosition.x, mPosition.y);

Updating the same texture for each platform. Meaning that after updating all platforms, you've put the texture to the position of the last platform. Then you draw, and you draw the platform at that position a bunch of times. One on top of the other. You can solve this in two ways, either make a seperate texture instance for each platform (I can't show you how to do that, since I don't know the texture class), or update and draw at the same time, like so:

public void UpdateAndDraw(float delta){
    for(int i = 0; i < mPlatforms.size(); i++){
        mPlatforms.get(i).Update(delta);
        mPlatforms.get(i).GetTexture().draw(batcher);
    }
}

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