i have 3 class:
MainActivity.java will create every thing and has touch lisnerters
GameLoop.java will have game loop and draw on screen
Paddle1.java will move paddle
problem is that i dont know how can i get information from MainActivity.java to Player1.java.
MainActivity will have ontouch method which will get width, height of screen than it will get x, y postion of touch.
if user hit on red area than set "up" to true.
Expand|Select|Wrap|Line Numbers
- public class MainActivity extends Activity implements OnTouchListener {
- boolean up = false;
- boolean down = false;
- GameLoop gameLoopObj;
- ....
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- DisplayMetrics metrics = this.getResources().getDisplayMetrics();
- int screenWidth = metrics.widthPixels;
- int screenHeight = metrics.heightPixels;
- //get postion of user touch
- int tX = (int)event.getX();
- int tY = (int)event.getY();
- // user touch the screen
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- if(tX >= 0 && tX <= tX+screenWidth/3){
- if(tY >= 0 && tY <= tY+screenHeight/2){
- up = true;
- }
- }
- }
- return true;
- }}
GameLopp will just update and draw stuff on screen.
Expand|Select|Wrap|Line Numbers
- public class GameLoop extends SurfaceView implements Runnable {
- Player1 player1Obj = new Player1(); ....
- will have run method (game loop).. etc
- }
Expand|Select|Wrap|Line Numbers
- public class Player1 {
- .... public void playerKeys() {
- //check if user get up screen from MainActicity
- if(up){
- y -= dy;
- }
- }