마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

센서를 이용한 게임인데요, 물체를 받으면 사라지게 해주세요.

0 추천
package ac.wku.kr;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Game20100101Activity extends Activity  implements    SensorEventListener  {
 
    private SensorManager sensorManager;
    double ax,ay,az;
   
    private double cx,cy;
    ImageButton ab;
    Button player;
    Button enemy1;
    Button enemy2;
    Button enemy3;
    Button enemy4;
    Button enemy5;
    Button fruit1; // apple;
    Button fruit2; // banana;
    Button fruit3; //tomato;
    Button fruit4; //mango;
    Button fruit5; //grape;
    private boolean game_ended=false;
    int [] enemy_loc_x;
    int [] enemy_loc_y;
    int [] fruit_loc_x;
    int [] fruit_loc_y;
    int[] ab_x;
    int[] ab_y;
   

    protected void onStop(){
        super.onStop();

        if (sensorManager != null)  
              sensorManager.unregisterListener(this);
    }

   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_GAME );

        cx = 240;
        cy= 700;
       
        player = (Button) findViewById(R.id.player);
        enemy1 = (Button) findViewById(R.id.enemy1);
        enemy2 = (Button) findViewById(R.id.enemy2);
        enemy3 = (Button) findViewById(R.id.enemy3);
        enemy4 = (Button) findViewById(R.id.enemy4);
        enemy5 = (Button) findViewById(R.id.enemy5);
        fruit1 = (Button) findViewById(R.id.fruit1);
        fruit2 = (Button) findViewById(R.id.fruit2);
        fruit3 = (Button) findViewById(R.id.fruit3);
        fruit4 = (Button) findViewById(R.id.fruit4);
        fruit5 = (Button) findViewById(R.id.fruit5);
        ab=(ImageButton)findViewById(R.id.imageButton1);
       
       
       
       ab_x=new int[5];
       ab_y=new int[5];
        enemy_loc_x = new int[5];
        enemy_loc_y = new int[5];
        fruit_loc_x = new int[5];
        fruit_loc_y = new int[5];
       
        for (int i = 0; i < 5; i++) {
         enemy_loc_x[i] = (int) (Math.random() * 479);
         enemy_loc_y[i] = 0;
         ab_x[i] = (int) (Math.random() * 479);
         ab_y[i] = 0;

            fruit_loc_x[i] = (int) (Math.random() * 479);
            fruit_loc_y[i] = 0;
        }          

    }

 @Override
 public void onAccuracyChanged(Sensor arg0, int arg1) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onSensorChanged(SensorEvent event) {
  // TODO Auto-generated method stub
     if (game_ended==false && event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
              ax=event.values[0];
              ay=event.values[1];
              az=event.values[2];
            
              cx = cx-ax * 10;
              cy=cy+ay*10;
             
             
      
              if (cx > 680) cx = 680;
              if (cx < 50) cx = 50;
              if (cy > 1150) cy = 1150;
              if (cy < 50) cy = 50;
             
           
            player.layout((int) (cx-50), (int)(cy-50), (int) (cx+50), (int)(cy+50)) ;
        
            
           
            
            
     }
    
        for (int i = 0; i < 5; i++) {
         
         enemy_loc_y[i] += (int) (Math.random() * 10);
           
            fruit_loc_y[i] += (int) (Math.random() * 10);
            ab_y[i] += (int) (Math.random() * 10);
            if (enemy_loc_y[i] > 700) {
             enemy_loc_y[i] = 0;
             enemy_loc_x[i] = (int) (Math.random() * 479);
            }
            if (fruit_loc_y[i] > 700) {
             fruit_loc_y[i] = 0;
             fruit_loc_x[i] = (int) (Math.random() * 479);
            }
            if (ab_y[i] > 1150) {
             ab_y[i] = 0;
             ab_x[i] = (int) (Math.random() * 479);
            }
           
           
            if(cx-5<ab_x[0]+5 && cx-50>ab_x[0]-5  ){
             ab.setVisibility(View.INVISIBLE);
            }
           
        
         
        ab.layout(ab_x[0]-50, ab_y[0]-50, ab_x[0]+50, ab_y[0]+50);
        //enemy2.layout(enemy_loc_x[1]-5, enemy_loc_y[1], enemy_loc_x[1]+5, enemy_loc_y[1]+10);
        //enemy3.layout(enemy_loc_x[2]-5, enemy_loc_y[2], enemy_loc_x[2]+5, enemy_loc_y[2]+10);
        //enemy4.layout(enemy_loc_x[3]-5, enemy_loc_y[3], enemy_loc_x[3]+5, enemy_loc_y[3]+10);
      //enemy5.layout(enemy_loc_x[4]-5, enemy_loc_y[4], enemy_loc_x[4]+5, enemy_loc_y[4]+10);
     
        //fruit1.layout(fruit_loc_x[0]-10, fruit_loc_y[0], fruit_loc_x[0]+10, fruit_loc_y[0]+20);
        //fruit2.layout(fruit_loc_x[1]-10, fruit_loc_y[1], fruit_loc_x[1]+10, fruit_loc_y[1]+20);
        //fruit3.layout(fruit_loc_x[2]-10, fruit_loc_y[2], fruit_loc_x[2]+10, fruit_loc_y[2]+20);
        //fruit4.layout(fruit_loc_x[3]-10, fruit_loc_y[3], fruit_loc_x[3]+10, fruit_loc_y[3]+20);
        //fruit5.layout(fruit_loc_x[4]-10, fruit_loc_y[4], fruit_loc_x[4]+10, fruit_loc_y[4]+20);
      

 }
}
}

 

   if(cx-5<ab_x[0]+5 && cx-50>ab_x[0]-5 ){ab.setVisibility(View.INVISIBLE);

} 요부분을 건드리면 될 것 같은데요..

현제 ab란 이미지 버튼이 아래로 랜덤하게 떨어지는데

 ab.layout(ab_x[0]-50, ab_y[0]-50, ab_x[0]+50, ab_y[0]+50);

이부분이 이미지 버튼 레이어 아웃 부분입니다.

만약 ab 이미지 버튼과 player 버튼이 만나면 ab가 사라지게 하고 다시 랜덤하게 떨어지게 하게 해주세요
ldrbabo (220 포인트) 님이 2013년 6월 11일 질문
최근들어 해주세요 질문이 많이 올라오는걸보니
대학생들의 한학기가 종료되어 가나봅니다..
흠 그러게요 ㅎㅎ
왠지 동감가네요
아... 죄송합니다.바쁘군요,,한학기가 끝나가는 터라 과제폭탄요,

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...