2016-09-25 21 views
0

我正在使用Java和XML開發AndroidStudio中的Space Invaders(沒有Libgdx,Gamemaker ...),我有一個問題...我希望你能幫助我。水平移動<ImageView>按<Button>

首先,這裏是我的app design. enter image description here

正如你所看到的,在RelativeLayout的底部,你可以找到在正確的飛船和兩個按鈕(一個在左邊,另一個按鈕船)。按鈕不可見。左側按鈕具有「control_izquierda」作爲id,右側按鈕「control_derecha」。

當你按下左鍵時,宇宙飛船向右移動,當你按下右鍵時,宇宙飛船向左移動(這個效果很好)。

我的問題這是關於按下其中一個按鈕時移動飛船(當我按住按鈕,飛船應移動)。我嘗試過不同的方法來嘗試這個,但我找不到正確的解決方案。

這裏是我的Java代碼

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void actualizaPosicion(View v) { 
     switch (v.getId()) { 
      case R.id.control_derecha: 
       findViewById(R.id.nave).setX(findViewById(R.id.nave).getX() - 30); 
       break; 
      case R.id.control_izquierda: 
       findViewById(R.id.nave).setX(findViewById(R.id.nave).getX() + 30); 
       break; 
     } 
    } 

} 

當其中一個按鈕被點擊,我的XML來電 「actualizaPosicion」 的方法。這種方法檢測是否點擊了右鍵或左鍵,然後上傳太空船的位置(該ID是「nave」),並且當按鈕按下時,宇宙飛船移動,並且當按鈕不再按時,飛船停下來。

我很感激幫助。

非常感謝!

回答

0

你需要的是一個動畫處理動畫Click here to view api docs

一旦你瞭解你將只需要創建具有不同值的XML文件(在你的情況翻譯)的基本流程。所以在你的按鈕上單擊java代碼,你可以簡單地啓動這個動畫xml文件。

//示例XML

<set android:shareInterpolator="false"> 
    <scale 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:fromXScale="1.0" 
        android:toXScale="1.4" 
        android:fromYScale="1.0" 
        android:toYScale="0.6" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:fillAfter="false" 
        android:duration="700" /> 
    <set android:interpolator="@android:anim/decelerate_interpolator"> 
        <scale 
           android:fromXScale="1.4" 
           android:toXScale="0.0" 
           android:fromYScale="0.6" 
           android:toYScale="0.0" 
           android:pivotX="50%" 
           android:pivotY="50%" 
           android:startOffset="700" 
           android:duration="400" 
           android:fillBefore="false" /> 
        <rotate 
           android:fromDegrees="0" 
           android:toDegrees="-45" 
           android:toYScale="0.0" 
           android:pivotX="50%" 
           android:pivotY="50%" 
           android:startOffset="700" 
           android:duration="400" /> 
    </set> 
</set> 

//例如Java進行使用

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); 
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 
spaceshipImage.startAnimation(hyperspaceJumpAnimation); 

希望這有助於代碼。請讓我知道,如果它改變了你的任何東西。

編輯:我從評論明白你正在尋找一個更復雜的解決方案,包括創建遊戲並不僅僅是簡單的動畫。

所以我想與你分享一個資源,它肯定會幫助你與這個和未來的項目。在這裏使用答案解釋遊戲開發的每一個方面是不可能的。

Click here for full detailed android space game tutorial

+0

嗨,Nishant。我認爲這種解決方案在這種情況下並不有用。我需要的是這樣的:當我按住左鍵時,當我不再按下按鈕時,飛船必須向下移動到右側,船將停止。我不能使用動畫,我需要將ImageView移動到另一個位置。如果我先按住左鍵,宇宙飛船移動30 dp,然後我想按住右鍵將飛船移動到左邊,那麼你的方式不是有用的。 –

+0

嘿,我明白你的要求。我已經相應地更新了答案。我相信這將解決您的所有當前和未來的問題與這個遊戲。 **這不僅僅是你需要的動畫。這是完全成熟的遊戲開發,這就是爲什麼我不能在答案中給出所有代碼示例**。如果編輯的鏈接變成您正在尋找的教程,請確認接受我的答案。 –