感谢您的所有建议和想法,我终于通过您的建议实现了它,这是解决问题的简单逻辑工作。如果有人要创建自定义的Health Bar,我想共享它,但是它不适用于100 HP,因为那样的话您必须编写100 IF语句,
想法是创建简单的自定义Healthbar并通过单击按钮来减少它。
此链接也对我有很大帮助。
HealthBar
码
private TextView Message,result;
private Button play;
private float maxHP = 10;
private float currentHP = maxHP;
//private float percentHP = (float) (currentHP/maxHP);
private int user_shield;
private float healthBar;
private ImageView shieldusb1,shieldusb2,shieldusb3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_one);
Initialize();
}
private void Initialize(){
user_shield = R.raw.shield_blue;
shieldusb2 = (ImageView) findViewById(R.id.shield_b2);
shieldusb1 = (ImageView) findViewById(R.id.shield_b1);
shieldusb3 = (ImageView) findViewById(R.id.shield_b3);
result = (TextView) findViewById(R.id.result);
play = (Button) findViewById(R.id.playButton);
play.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
play();
}
});
}
public void play(){
{
healthBar=0;
currentHP = (float) (currentHP - 0.5);
//currentHP -= 1;
if(currentHP <= 0) //if the player died
{
currentHP = 0; //set his HP to zero, (just in case)
Message.setText("You died! :P"); //add any extra death-code here
//update the healthBar
result.setText ("WIN POINTS");
}
updateHealthBar();
}
}
private void updateHealthBar() {
// percentHP = currentHP / maxHP;
healthBar = currentHP;
if(healthBar<9.6){
shieldusb1.setVisibility(View.GONE);
}
{
if (healthBar<=9){
shieldusb2.setVisibility(View.GONE);
}
if (healthBar<=8.6){
shieldusb3.setVisibility(View.GONE);
}
}
}
最佳答案
您的healthBar
是int类型的,因此对0.9的检查与对1的检查相同。
另外,根据您的条件,您首先要检查:
if (healthBar <= 1)
如果满足此条件,则您将隐藏shieldusb1,并且由于使用
else
,您将永远不会达到第二条件。您可以做的是在第二个条件前面删除else
,但是如上所述,您的healthBar是int,因此第二个条件基本上与第一个条件相同。将healthBar更改为double。关于java - 在点击时隐藏ImageView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21729233/