Hey guys,
I'm trying to create a game. What I want to do first is have an airplane which is a image move around on the screen. I look all over the web and try some of the examples they have but I can't get mine to work. So basically I'm using the SensorManger to get the x,y,z values. Now with those values I want to move the image around. How would I do that? Or if there is a better way to do this please let me know. Thanks!
[/LEFT]
I'm trying to create a game. What I want to do first is have an airplane which is a image move around on the screen. I look all over the web and try some of the examples they have but I can't get mine to work. So basically I'm using the SensorManger to get the x,y,z values. Now with those values I want to move the image around. How would I do that? Or if there is a better way to do this please let me know. Thanks!
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ObitBlastActivity [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]implements[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]SensorEventListener {[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SensorManager [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sensorManager[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
[LEFT][SIZE=2]TextView [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]xCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// declare X axis object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]TextView [/LEFT]
[/LEFT]
[/SIZE][LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]yCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// declare Y axis object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[LEFT][/COLOR][/SIZE][SIZE=2]TextView [/LEFT]
[/LEFT]
[/SIZE][LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// declare Y axis object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[LEFT][/COLOR][/SIZE][SIZE=2]ImageView [/LEFT]
[/LEFT]
[/SIZE][LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myPlane[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#646464]
[/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState){[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]xCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=(TextView)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]xcoor[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]yCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=(TextView)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ycoor[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=(TextView)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zcoor[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myPlane[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (ImageView)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mainAirPlane[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sensorManager[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=(SensorManager)getSystemService([/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]SENSOR_SERVICE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// add listener. The listener will be HelloAndroid (this) class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sensorManager[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].registerListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2],[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sensorManager[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getDefaultSensor(Sensor.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TYPE_ACCELEROMETER[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]),[/SIZE]
[SIZE=2]SensorManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]SENSOR_DELAY_NORMAL[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onAccuracyChanged(Sensor sensor,[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] accuracy){[/SIZE][/LEFT]
[LEFT][SIZE=2]}[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onSensorChanged(SensorEvent event){[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// check sensor type[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](event.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sensor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getType()==Sensor.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TYPE_ACCELEROMETER[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]){[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// assign directions[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] x=event.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]values[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][0];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] y=event.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]values[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][1];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] z=event.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]values[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][2];[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]xCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"X: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+x);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]yCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Y: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+y);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zCoor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Z: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+z);[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Draw my plane here..........[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]