感謝所有的建議和想法,我終於通過你的建議達到了它,這是一個簡單的邏輯工作來解決問題。我想分享它,如果任何人想要創建自定義健康欄,但它不適合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