画出时不会显示精灵

时间:2016-08-21 16:52:00

标签: java android libgdx

我正在尝试使用LibGDX构建一个Android游戏。这是我的第一个Android应用程序,请原谅我,如果我要求的是简单的。我想画一个精灵,但似乎没有任何东西出现。我在想,也许精灵是在相机空间之外画出来的,但即使我拿回相机(显示出游戏区域),我也看不到精灵。我花了很长时间试图找到问题,但我无法做到。这是我的Playstate类:

public class PlayState extends State{
    private Man man;
    private Cannon cannon;
    private Texture cannontop;
    private Texture bg;
    private SpriteBatch sb;

    public PlayState(GameStateManager gsm) {
        super(gsm);
        cannon = new Cannon();
        cannontop = new Texture("Cannon.png");
        man = new Man(0, (Gdx.graphics.getHeight() / 4));
        bg = new Texture("background.png");
        cam.setToOrtho(false, 2160, 3840);

    }

    @Override
    protected void handleInput() {
       // finish later

    }

    @Override
    public void update(float dt) {
        man.update(dt);
        //cannon.update();
    }

    public void create(){
        sb = new SpriteBatch();
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setProjectionMatrix(cam.combined);
        sb.begin();
        sb.draw(bg, cam.position.x - (cam.viewportWidth / 2), 0);
        sb.draw(man.getTexture(), man.getPosition().x, man.getPosition().y);
        //sb.draw(cannon.getTexture(), cannon.getPositionx(), cannon.getPositiony());
        cannon.getCannon().draw(sb);
        sb.draw(cannontop, (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2) - 23, (bg.getHeight() / 2) + (bg.getHeight() / 5));
        //sb.draw(cannon.getTexture(), (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannontop.getWidth() / 2) + 8);
        sb.end();

    }

    @Override
    public void dispose() {
        bg.dispose();
        man.dispose();
        cannon.dispose();
    }
}

我想要绘制的精灵是在加农炮类中。这是我的大炮班:

public class Cannon {
    private Texture cannend;
    private Texture bg;
    private Texture cannonmain;
    private SpriteBatch batch;
    private Sprite cannon;


    public Cannon(){
        cannonmain = new Texture("Cannon.png");
        bg = new Texture ("background.png");
        cannend = new Texture("cannonend.png");
        cannon = new Sprite(cannend, (bg.getWidth()/ 2) - (cannend.getWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannonmain.getWidth() / 2) + 8, (cannend.getWidth()), (cannend.getHeight()));
        cannon.setPosition((bg.getWidth()/ 2) - (cannend.getWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannonmain.getWidth() / 2) + 8);
        cannon.setRotation(70);
        cannon.setCenter((bg.getWidth()/ 2) , (bg.getHeight() / 2) + (bg.getHeight() / 5) + 3*(cannend.getHeight() / 4));
    }
    //public update(dt){
    //    stuffff
    // }


    public Sprite getCannon(){
        return cannon;
    }

    public float getPositionx(){
        return cannon.getX();
    }

    public float getPositiony(){
        return cannon.getY();
    }

    public void dispose(){
        cannonmain.dispose();
        cannend.dispose();
        bg.dispose();
    }

    public Texture getTexture(){
        return cannend;
    }

    public float getTextureWidth(){
        return cannend.getWidth();
    }
}
我道歉是精灵/纹理名称令人困惑。请忽略这些评论,因为它们适用于我尝试过的未来/替代事项。我想知道如何让Sprite出现。此外,精灵旋转后我会旋转吗?任何帮助都非常受欢迎。 感谢。

州级:

public abstract class State {
    protected OrthographicCamera cam;
    protected Vector3 mouse;
    protected GameStateManager gsm;

    protected State(GameStateManager gsm) {
        this.gsm = gsm;
        cam = new OrthographicCamera();
        mouse = new Vector3();

    }


    protected abstract void handleInput();
    public abstract void update(float dt);
    public abstract void render(SpriteBatch sb);
    public abstract void dispose();


}

3 个答案:

答案 0 :(得分:1)

您应该使用sprite.draw(spritebatch)而不是spritebatch.draw(sprite)来绘制精灵。

另外,国家级的用途是什么? Playstate应该是一个屏幕吗?然后你应该在你的课程中实现Screen界面,然后使用你想要的方法,比如'渲染',' dispose',...

答案 1 :(得分:0)

从render方法中删除SpriteBatch sb参数 只需使用

@Override 
public void render()

答案 2 :(得分:0)

可能是libgdx无法绘制精灵,因为重复任何SpriteBatch.begin();
并复制SpriteBatch.end(); 在一堆电话中