473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to make player in android

59
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.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
  1. public class MainActivity extends Activity implements OnTouchListener {
  2.           boolean up = false;
  3.           boolean down = false;
  4.          GameLoop gameLoopObj;
  5.           ....
  6.        @Override
  7.       public boolean onTouch(View v, MotionEvent event) {
  8.                    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
  9.                   int screenWidth = metrics.widthPixels;
  10.                   int screenHeight = metrics.heightPixels;
  11.  
  12.                    //get postion of user touch
  13.                    int tX = (int)event.getX();
  14.                   int tY = (int)event.getY();
  15.  
  16.                 // user touch the screen
  17.                 if (event.getAction() == MotionEvent.ACTION_DOWN) { 
  18.                       if(tX >= 0 && tX <= tX+screenWidth/3){
  19.                           if(tY >= 0 && tY <= tY+screenHeight/2){
  20.                                  up = true;
  21.                             }
  22.                      }
  23.                }  
  24.        return true;
  25.         }}

GameLopp will just update and draw stuff on screen.
Expand|Select|Wrap|Line Numbers
  1. public class GameLoop extends SurfaceView implements Runnable {
  2.        Player1 player1Obj = new Player1();       ....
  3.         will have run method (game loop).. etc
  4. }
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.
Expand|Select|Wrap|Line Numbers
  1. public class Player1 {
  2.      ....      public void playerKeys() {
  3.              //check if user get up screen from MainActicity
  4.             if(up){
  5.             y -= dy;
  6.       }
  7. }
  8.  
Attached Images
File Type: jpg ping.jpg (10.1 KB, 129 views)
Aug 12 '13 #1
1 1326
Nepomuk
3,112 Expert 2GB
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.down. However this is not the most elegant solution. What I would do is something like this:
Expand|Select|Wrap|Line Numbers
  1. public class Player1 {
  2.    // ...
  3.    private boolean up = false;
  4.    private boolean down = false;
  5.    // ...
  6.    public void setUp(boolean up) {
  7.       this.up = up;
  8.    }
  9.  
  10.    public void setDown(boolean down) {
  11.             this.down = down;
  12.    }
  13. }
Expand|Select|Wrap|Line Numbers
  1. public class GameLoop extends SurfaceView implements Runnable {
  2.    // ...
  3.    private void setUp(boolean up) {
  4.       // ...
  5.       player1Obj.setUp(up);
  6.    }
  7.  
  8.    private void setDown(boolean down) {
  9.       // ...
  10.       player1Obj.setDown(down);
  11.    }
  12. }
Expand|Select|Wrap|Line Numbers
  1. public class MainActivity extends Activity implements OnTouchListener {
  2.    // ...
  3.    private void setUp(boolean up) {
  4.       // ...
  5.       gameLoopObj.setUp(up);
  6.    }
  7.  
  8.    private void setDown(boolean down) {
  9.       // ...
  10.       gameLoopObj.setDown(down);
  11.    }
  12. }
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.
Aug 12 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alan Zhong | last post by:
i am a beginner of database design, could anyone please help me to figure out how to make these two tables work. 1) a "players" table, with columns "name", "age" 2) a "teams" table, which can...
2
by: Vanga Sasidhar | last post by:
Already posted the same message but the date and time of my machine was set back. and it was listed under the old date. Thats why I am posting the same message again. Please accept this. ...
2
by: ALi Shaikh | last post by:
I already did the game once and it works but now I need to change it to add a function that will make the computer take player 1 turn and always win. heres the original #include<iostream.h> ...
0
by: npm | last post by:
I'm trying to find out why the android browser won't load and/or display my XML data. It works fine on computer browsers (FF, IE, Safari, Chrome, Opera) and even in iPhone's Safari, but when I view...
0
rahuljaiswal1987
by: rahuljaiswal1987 | last post by:
Hi all, I have a web service hosted in my network which is SOAP SECURED. I want to access it from my Android client application. I am using ksoap2 api to make android client to consume the...
1
by: npm | last post by:
I'm trying to find out why the android browser won't load and/or display my XML data. This javascript works fine on computer browsers (FF, IE, Safari, Chrome, Opera) and even in iPhone's Safari, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.