2014-03-02 48 views
1

我試圖製作一個遊戲,屏幕上的一個方塊通過按鈕向上,向下,向左或向右移動,當方塊遇到另一個隨機放置的方塊時,方塊會移動。但是,我不知道如何測試兩個ImageView是否重疊。請保持答案簡單易懂。我是android新手。這裏是我的代碼,如果你需要它:如何測試對象是否在Android中重疊

package com.example.runaway; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.AttributeSet; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

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

    final ImageView box = (ImageView) findViewById(R.id.player); 
    Button pushMedown = (Button) findViewById(R.id.down); 



     pushMedown.setOnClickListener(new View.OnClickListener() { 

      int location = (int) box.getY(); 
      int math = 55; 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      location = (int) (math + box.getY()); 
      box.setY(location); 
      if (location > 400) {location = location - math;} 
      box.setY(location); 


     } 
    }); 
           // up 
     final ImageView box1 = box; 
     Button pushMeup = (Button) findViewById(R.id.up); 



     pushMeup.setOnClickListener(new View.OnClickListener() { 


      int location = (int) box.getY(); 
      int math = 55; 
    @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     location = (int) (box.getY() - math); 
     box.setY(location); 
     if (location < 10) {location = location + math;} 
     box.setY(location); 

    } 
     }); 

          // right 
      final ImageView box2 = box; 
      Button pushMeleft = (Button) findViewById(R.id.right); 



      pushMeleft.setOnClickListener(new View.OnClickListener() { 


       int location1 = (int) box.getX(); 
       int math = 55; 
      @Override 
       public void onClick(View v) { 
       // TODO Auto-generated method stub 

       location1 = (int) (math + box.getX()); 
       box.setX(location1); 
       if (location1 > 400) {location1 = location1 - math;} 
       box.setX(location1); 

       } 
      }); 
         // left 
      final ImageView box3 = box; 
      Button pushMeright = (Button) findViewById(R.id.left); 



      pushMeright.setOnClickListener(new View.OnClickListener() { 


        int location1 = (int) box.getX(); 
        int math = 55; 
       @Override 
        public void onClick(View v) { 
        // TODO Auto-generated method stub 

        location1 = (int) (box.getX() - math); 
        box.setX(location1); 
        if (location1 < 0) {location1 = location1 + math;} 
        box.setX(location1); 

       }});       

     } 

private ImageView findByViewId(int imageview1) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 
+0

remove'private ImageView findByViewId(int imageview1){'。不需要這個。雖然它可能無法解決你的其他問題 – Raghunandan

+0

@Raghunandan哇,你快速評論,謝謝,即使我仍然有問題! :3 – HorribleCode

回答

0

假設你的ImageViews被稱爲第一和第二。

if((first.getRight()>second.getLeft()&&first.getLeft()<second.getRight())&&first.getTop()>second.getBottom()&&first.getBottom()<second.getTop()) 
{ 
    //the imageViews are overlapping) 
} 
相關問題