take a look at this image. i am trying to create simple ping pong in android. but i cant seem to under how to make my paddle1(player1 ) move. so if user touch red area of screen than paddle1(player1 ) will go up. if user touch green area of screen than paddle1(player1 ) will go down.
i have 3 class:
MainActivity.ja va 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.ja va 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.
GameLopp will just update and draw stuff on screen.
Player1 will move paddle up if "up" from MainActicity is set to true. PROBLEM is that i dont now how to get value of "up". i have set of getter and setter in MainActivity.
i have 3 class:
MainActivity.ja va 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.ja va 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.
Code:
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.
Code:
public class GameLoop extends SurfaceView implements Runnable { Player1 player1Obj = new Player1(); .... will have run method (game loop).. etc }
Code:
public class Player1 { .... public void playerKeys() { //check if user get up screen from MainActicity if(up){ y -= dy; } }
Comment