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가 사라지게 하고 다시 랜덤하게 떨어지게 하게 해주세요