SpriteBatch绘图不起作用

时间:2014-07-27 16:40:56

标签: java libgdx inventory

我正在为我的游戏制作一个库存系统,但它不起作用。 我有3个课程需要这个工作。 我有一个Slot类:

package com.mateo226.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;

public class Slots {
    Texture Slot1, Slot2, Slot3, Slot4, Slot5;


    public Slots(){
        Slot1 = new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png"));
        Slot2 = new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png"));
        Slot3 = new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png"));
        Slot4 = new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png"));
        Slot5 = new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png"));



    }


    public Texture getSlot1() {
        return Slot1;
    }


    public void setSlot1(Texture slot1) {
        Slot1 = slot1;
        this.Slot1 = slot1;
    }


    public Texture getSlot2() {
        return Slot2;
    }


    public void setSlot2(Texture slot2) {
        Slot2 = slot2;
    }


    public Texture getSlot3() {
        return Slot3;
    }


    public void setSlot3(Texture slot3) {
        Slot3 = slot3;
    }


    public Texture getSlot4() {
        return Slot4;
    }


    public void setSlot4(Texture slot4) {
        Slot4 = slot4;
    }


    public Texture getSlot5() {
        return Slot5;
    }


    public void setSlot5(Texture slot5) {
        Slot5 = slot5;
    }
}

我也有库存类:

package com.mateo226.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;

public class Inventory {

    String Slot1Item, Slot2Item, Slot3Item, Slot4Item, Slot5Item;
    String curItem;
    Slots slots = new Slots();
    Texture slotChosen = new Texture(Gdx.files.internal("Inventory/slots/InventorySlotChosen.png"));

    public void update(){
        if(Gdx.input.isKeyPressed(Keys.SPACE)){
            slots.setSlot1(slotChosen);
            curItem = Slot1Item;
            slots.setSlot2(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot3(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot4(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot5(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            System.out.println("Clicked 1");
        }
        if(Gdx.input.isKeyPressed(Keys.NUM_2)){
            slots.setSlot2(slotChosen);
            curItem = Slot2Item;
            slots.setSlot1(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot3(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot4(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot5(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
        }
        if(Gdx.input.isKeyPressed(Keys.NUM_3)){
            slots.setSlot3(slotChosen);
            curItem = Slot3Item;
            slots.setSlot2(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot1(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot4(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot5(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
        }
        if(Gdx.input.isKeyPressed(Keys.NUM_4)){
            slots.setSlot4(slotChosen);
            curItem = Slot4Item;
            slots.setSlot2(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot3(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot1(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot5(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
        }
        if(Gdx.input.isKeyPressed(Keys.NUM_5)){
            slots.setSlot5(slotChosen);
            curItem = Slot5Item;
            slots.setSlot2(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot3(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot4(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
            slots.setSlot1(new Texture(Gdx.files.internal("Inventory/slots/InventorySlot.png")));
        }
    }
}

然后在我的playcreen课程中,我有这部分代码:

SpriteBatch batch = new SpriteBatch();
Slots slots = new Slots();
    Inventory inv = new Inventory();

然后我有:

batch.begin();

        batch.draw(slots.getSlot1(), player.getPosition().x - (Gdx.graphics.getWidth() / 2), player.getPosition().y + (Gdx.graphics.getHeight() / 2) - 25);

        batch.draw(slots.getSlot2(), player.getPosition().x - (Gdx.graphics.getWidth() / 2) + 70, player.getPosition().y + (Gdx.graphics.getHeight() / 2) - 25);

        batch.draw(slots.getSlot3(), player.getPosition().x - (Gdx.graphics.getWidth() / 2) + 140, player.getPosition().y + (Gdx.graphics.getHeight() / 2) - 25);

        batch.draw(slots.getSlot4(), player.getPosition().x - (Gdx.graphics.getWidth() / 2) + 210, player.getPosition().y + (Gdx.graphics.getHeight() / 2) - 25);

        batch.draw(slots.getSlot5(), player.getPosition().x - (Gdx.graphics.getWidth() / 2) + 280, player.getPosition().y + (Gdx.graphics.getHeight() / 2) - 25);

        inv.update();
batch.end();

我能够绘制库存槽,但是当我点击像SPACE,2,3,4或5这样的按钮时 没有任何反应,除非我点击SPACE,我得到" Clicked 1"打印到控制台。 我不知道为什么我的纹理没有更新。如果我忘记了一些代码,请告诉我。谢谢!

0 个答案:

没有答案