创建多个矩形java libgdx

时间:2017-05-06 13:08:41

标签: java android libgdx

您好我是libgdx和java的新手,我在屏幕上创建多个对象时遇到问题。当我点击SPACE时,我希望在Pocisk类的屏幕矩形上有一个新对象,但是当我点击它时,只需按下它。 / p>

游戏课

public class Gra extends ApplicationAdapter {
    SpriteBatch batch;
    private Texture Statektekstura,Pocisktekstura;
    private Statek statek;
    private Pocisk pocisk;

    @Override
    public void create () {
        zaladujdane();
        init();
    }

    private void init() {
        batch=new SpriteBatch();
        statek=new Statek(Statektekstura);
        pocisk=new Pocisk(Pocisktekstura);
    }

    private void zaladujdane() {
        //tekstura statku pomniejszana
        Pixmap pixmap2 = new Pixmap(Gdx.files.internal("statek.jpg"));
        Pixmap pixmap1 = new Pixmap(100, 100, pixmap2.getFormat());
        pixmap1.drawPixmap(pixmap2,
                0, 0, pixmap2.getWidth(), pixmap2.getHeight(),
                0, 0, pixmap1.getWidth(), pixmap1.getHeight()
        );
        Statektekstura=new Texture(pixmap1);

        //tekstura pocisk pomniejszona
        Pixmap pixmap20 = new Pixmap(Gdx.files.internal("pocisk.jpg"));
        Pixmap pixmap10 = new Pixmap(35, 35, pixmap20.getFormat());
        pixmap10.drawPixmap(pixmap20,
                0, 0, pixmap20.getWidth(), pixmap20.getHeight(),
                0, 0, pixmap10.getWidth(), pixmap10.getHeight()
        );

        Pocisktekstura=new Texture(pixmap10);   
    }

    @Override
    public void render () {
        udpdate();
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        statek.draw(batch);
        pocisk.draw(batch);

        batch.end();
    }

    private void udpdate() {
        przesuwanie();
        pocisk.y=pocisk.y+pocisk.pociskpredkosc*Gdx.graphics.getDeltaTime();        
    }

    private void przesuwanie() {
        if(Gdx.input.isKeyPressed(Keys.A))
        {
            statek.x=statek.x-250*Gdx.graphics.getDeltaTime();
        }
        if(Gdx.input.isKeyPressed(Keys.D))
        {
            statek.x=statek.x+250*Gdx.graphics.getDeltaTime();
        }
        if(Gdx.input.isKeyPressed(Keys.SPACE))
        {

            pocisk=new Pocisk(Pocisktekstura);
            pocisk.strzal();    
        }       
    }

    @Override
    public void dispose () {
        //batch.dispose();  
    }
}

类Pocisk代码:

public class Pocisk extends Rectangle {

    private Texture texture;
    public float pociskpredkosc=0;

    Pocisk(Texture texture)
    {
        this.texture=texture;
        this.height=texture.getHeight();
        this.width=texture.getWidth();  
    }

    public void draw(SpriteBatch batch)
    {
        batch.draw(texture, x, y);
    }

    public void strzal()
    {
        pociskpredkosc=pociskpredkosc+80;
    }   
}

我认为我在课堂上没有失败,而不是在Pocisk课程中,当我点击空格时,我应该改变什么来制作一个新的Rectangle?

0 个答案:

没有答案