2014-02-12 176 views
0

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

    } 

回答

0

healthBarINT型的,所以檢查,對0.9相同反對1.

而且檢查,在你的條件,你先檢查:

if (healthBar <= 1) 

如果這個條件成立,你隱藏了shieldusb1,並且你永遠不會達到第二個條件,因爲你已經使用了else。你可以做的是在第二個條件前刪除else,但如上所述,你的healthBar是int,所以第二個條件基本上與第一個條件相同。將healthBar更改爲雙倍。

+0

Hello Melq,通過刪除else並使用「IF」,兩個圖像都隱藏在一次點擊上,而不是當條件滿足時 – Mohtashim

+0

是的,因爲你的變量是int,但是,如果你做數學,按下按鈕時, healthBar'爲0,所以兩個條件都滿足,因爲0 <= 1且0 <= 0.9。首先,將你的變量改爲'double',然後再次檢查計算,例如。在play()中,如果你減少0.5,而不是5,按鈕點擊後,healthBar將爲0.95,這將滿足第一個條件,但不是第二個(這將在第二次按鈕點擊後滿足)。 – Melquiades

0

你需要

if(healthBar<=1) 
... 
else if (healthBar<=0.9) 

即使healthBar<= 0.9判斷爲真來調整這些條件,healthBar<=1也是如此,因此總是會進入第一if

1

我的猜測,是否需要刪除否則如果,並且只是如果,因爲如果你執行第一個語句,其中< = 1,否則如果永遠不會執行。

private void updateHealthBar() { 
    percentHP = currentHP/maxHP;/calculating points 
    healthBar = percentHP; 


    if(healthBar<=1){   // Hiding first Image View 
     shieldusb1.setVisibility(View.GONE);     
    } 
    if (healthBar<=0.9){ 
     shieldusb2.setVisibility(View.GONE); // How to Hide this Image View after button click 
    } 
} 
1

您是否healthBar是< = 1,如果是< = 0.9 ......但這種情況從來沒有核實,造成0.9後< 1,所以它總是選擇第一個選項! 更改您的第一個如果檢查值介於1和0.9之間,它將起作用!

0

這是唯一合乎邏輯的問題。如果if/if更改if/else if block。

if(healthBar<=1) 
    shieldusb1.setVisibility(View.GONE);     
if (healthBar<=0.9) 
    shieldusb2.setVisibility(View.GONE); 
+0

什麼邏輯問題。告訴他。 – Piyush