how to make player in android

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    how to make player in android

    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.
    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
    }
    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.
    Code:
    public class Player1 {
         ....      public void playerKeys() {
                 //check if user get up screen from MainActicity
                if(up){
                y -= dy;
          }
    }
    Attached Files
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    OK, just making sure I understand what you're doing:
    • MainActivity has a GameLoop object
    • GameLoop has a Player1 object
    • Player1 needs to know the value of up, which is known in MainActivity

    The way to do this which will need the least code changes is probably to make up and down static and accessing them with MainActivity.up and MainActivity.do wn. However this is not the most elegant solution. What I would do is something like this:
    Code:
    public class Player1 {
       // ...
       private boolean up = false;
       private boolean down = false;
       // ...
       public void setUp(boolean up) {
          this.up = up;
       }
    
       public void setDown(boolean down) {
                this.down = down;
       }
    }
    Code:
    public class GameLoop extends SurfaceView implements Runnable {
       // ...
       private void setUp(boolean up) {
          // ...
          player1Obj.setUp(up);
       }
    
       private void setDown(boolean down) {
          // ...
          player1Obj.setDown(down);
       }
    }
    Code:
    public class MainActivity extends Activity implements OnTouchListener {
       // ...
       private void setUp(boolean up) {
          // ...
          gameLoopObj.setUp(up);
       }
    
       private void setDown(boolean down) {
          // ...
          gameLoopObj.setDown(down);
       }
    }
    An even better solution may be to implement a queue of direction events based on touch events, similar to the method used by the android touch sensor, and handing over those events. That would however require more code changes.

    Comment

    Working...