2014-03-02 6 views
1

버튼을 사용하여 화면의 사각형을 위, 아래, 왼쪽 또는 오른쪽으로 움직이는 게임을 만들려고했는데 사각형이 다른 임의로 배치 된 사각형을 만났을 때 사각형은 움직임. 그러나 두 개의 ImageView가 겹치는 지 테스트하는 방법을 모르겠습니다. 답변을 간단하고 이해하기 쉽게 유지하십시오. 나는 안드로이드를 처음 접한다. 여기 당신이 그것을 필요로하는 경우에 내 코드입니다 :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

'private ImageView findByViewId (int imageview1) {'을 (를) 제거하십시오. 이거 필요 없어. 비록 당신의 다른 문제를 해결하지 못할 수도 있습니다. – Raghunandan

+0

@Raghunandan 와우, 당신은 빨리, 고맙다, 내가 여전히 문제가 있다고해도! :삼 – HorribleCode

답변

0

하면 ImageViews는 제 1 및 제 2라고 가정하면.

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