这是我想出来的结果,但它很糟糕,我知道必须有更好的方法。我最终希望库存中的物品具有相应的纹理以显示它的图标。
ButtonSprite btn = new ButtonSprite(50, 280,
AssetLoaderUtil.yourTextureRegion,
mContext.getVertexBufferObjectManager()) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
super.onManagedUpdate(pSecondsElapsed);
}
@Override
public boolean onAreaTouched(final TouchEvent pAreaTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch (pAreaTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
// do action when button pressed down.
break;
case TouchEvent.ACTION_UP:
// do action when button pressed up.
//
// UserData.getInstance().unlockNextLevel();
//
if (inventory == false) {
// showInventory();
inventory = true;
inventoryRect = new Rectangle(10, 10, 460, 260,
mContext.getVertexBufferObjectManager());
inventoryRect.setColor(0, 0, 0, 0.9f);
hud.attachChild(inventoryRect);
inventoryRect.setVisible(true);
inventoryRect.setZIndex(11);
inventRect = true;
Text inventoryTitleText = new Text(5, 0,
AssetLoaderUtil.mFontMENU, "Inventory",
mContext.getVertexBufferObjectManager());
inventoryTitleText.setPosition(200, 20);
inventoryTitleText.setColor(Color.WHITE);
inventoryRect.attachChild(inventoryTitleText);
/*
* Text itemText = new Text(5, 0,
* AssetLoaderUtil.mFontMENU, "Nada...",
* mContext.getVertexBufferObjectManager());
* itemText.setPosition(200, 60);
* itemText.setColor(Color.WHITE);
*
*
* inventoryRect.attachChild(itemText);
*/
Text goldText = new Text(5, 0,
AssetLoaderUtil.mFontMENU, "Gold: "
+ UserData.getInstance()
.getPlayerGold(),
mContext.getVertexBufferObjectManager());
goldText.setPosition(50, 50);
goldText.setColor(Color.GREEN);
inventoryRect.attachChild(goldText);
if (UserData.getInstance().hasSword() == true) {
Text swordText = new Text(5, 0,
AssetLoaderUtil.mFontMENU, "Sword",
mContext.getVertexBufferObjectManager());
swordText.setPosition(50, 75);
swordText.setColor(Color.RED);
inventoryRect.attachChild(swordText);
}
if (UserData.getInstance().hasAxe() == true) {
Text axeText = new Text(5, 0,
AssetLoaderUtil.mFontMENU, "Axe",
mContext.getVertexBufferObjectManager());
axeText.setPosition(50, 95);
axeText.setColor(Color.RED);
inventoryRect.attachChild(axeText);
}
if (hasPotion == true) {
Text potionText = new Text(5, 0,
AssetLoaderUtil.mFontMENU, "Potion",
mContext.getVertexBufferObjectManager());
potionText.setPosition(50, 115);
potionText.setColor(Color.BLUE);
inventoryRect.attachChild(potionText);
}
} else {
inventory = false;
inventoryRect.setVisible(false);
// inventoryTitleText.setVisible(false);
// inventoryTitleText = null;
inventRect = false;
}
/*
* if (inventRect==false){
*
* inventoryRect = new Rectangle(15,15,800,440,
* mContext.getVertexBufferObjectManager());
*
* inventoryRect.setColor(1, 1, 1, 0.8f);
*
* hud.attachChild(inventoryRect);
* inventoryRect.setVisible(true);
*
* inventRect = true;
*/
// this.setVisible(false);
}
return super.onAreaTouched(pAreaTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
}
};
hud.registerTouchArea(btn);
hud.attachChild(btn);