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.widget.Button;
public class Game20100101Activity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
double ax,ay,az;
private double cx,cy;
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;
int [] enemy_loc_x;
int [] enemy_loc_y;
int [] fruit_loc_x;
int [] fruit_loc_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= 1000;
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);
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;
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 (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;
player.layout((int) (cx-50), 700, (int) (cx+50), 740) ;
}
for (int i = 0; i < 5; i++) {
enemy_loc_y[i] += (int) (Math.random() * 10);
fruit_loc_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);
}
}
enemy1.layout(enemy_loc_x[0]-5, enemy_loc_y[0], enemy_loc_x[0]+5, enemy_loc_y[0]+10);
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);
}
}
소스는 이렇고요..
player.layout((int) (cx-50), 700, (int) (cx+50), 740) ; 이부분이 좌우로 움직이게 해주는 것 같은데 도저히 무엇을 바꿔야
상하로 움직일까요?