感谢所有的建议和想法,我终于通过你的建议达到了它,这是一个简单的逻辑工作来解决问题。我想分享它,如果任何人想要创建自定义健康栏,但它不适合100 HP,因为那么你必须编写100条IF语句, 想法是创建简单的自定义健康栏并通过按下按钮减少它。 这个链接也帮助了我很多。 HealthBar 代码点击隐藏ImageView
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);
}
}
}
Hello Melq,通过删除else并使用“IF”,两个图像都隐藏在一次点击上,而不是当条件满足时 – Mohtashim
是的,因为你的变量是int,但是,如果你做数学,按下按钮时, healthBar'为0,所以两个条件都满足,因为0 <= 1且0 <= 0.9。首先,将你的变量改为'double',然后再次检查计算,例如。在play()中,如果你减少0.5,而不是5,按钮点击后,healthBar将为0.95,这将满足第一个条件,但不是第二个(这将在第二次按钮点击后满足)。 – Melquiades