By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

android

P: 59
i am trying to make a templet for making android games. this is what i have so far. two classes. one does the animation and other touch listener. If there is some thing better PLZ let me know so i cant edit my templet. thank you.

//first class = build and listeners
Expand|Select|Wrap|Line Numbers
  1. //imports etcc...
  2.  
  3. public class MainActivity extends Activity implements OnTouchListener{
  4.  
  5.     int delay = 50;
  6.  
  7.     /*** Object variables ***/
  8.     Animation animationObj;
  9.     WakeLock wL;
  10.  
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         // you phone wont lock when this app is running
  14.         PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE); 
  15.         // hidden step -- mainifest > permisions > add > users permision > ok > name:android.permission.WAKE_LOCK > enter
  16.         wL = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Whatever");
  17.  
  18.         super.onCreate(savedInstanceState);
  19.         super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //lock orientation
  20.         requestWindowFeature(Window.FEATURE_NO_TITLE);                            // remove title
  21.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  22.                             WindowManager.LayoutParams.FLAG_FULLSCREEN);          // remove icons top
  23.         wL.acquire();                                                             //start dont lock the screen
  24.         animationObj = new Animation(this);
  25.         animationObj.setOnTouchListener(this);
  26.         setContentView(animationObj);
  27.  
  28.  
  29.     }
  30.  
  31.     @Override
  32.      protected void onPause(){
  33.          super.onPause();
  34.          animationObj.pause();
  35.          wL.release();
  36.      }
  37.  
  38.  
  39.  
  40.      @Override
  41.      protected void onResume(){
  42.          super.onResume();
  43.          animationObj.resume();
  44.      }
  45.  
  46.  
  47.      @Override
  48.         public boolean onTouch(View v, MotionEvent event) {
  49.         //show down the speed so slow phone can run it
  50.             try{
  51.                 Thread.sleep(delay);
  52.             } catch(InterruptedException e){
  53.                 e.printStackTrace();
  54.             }
  55.  
  56.             if(event.getAction() == MotionEvent.ACTION_DOWN){ //user toch the screen
  57.  
  58.             }
  59.             else if(event.getAction() == MotionEvent.ACTION_UP){ //user let go of touch
  60.  
  61.             }
  62.  
  63.             //return false if you dont want it to drag
  64.             //return true if you want it to drag
  65.             return true;
  66.         }
  67.  
  68.  
  69.     @Override
  70.     public boolean onCreateOptionsMenu(Menu menu) {
  71.         // Inflate the menu; this adds items to the action bar if it is present.
  72.         getMenuInflater().inflate(R.menu.main, menu);
  73.         return true;
  74.     }
  75. }


//2nd class = animation and game loop
Expand|Select|Wrap|Line Numbers
  1. \\imports etc..
  2.  
  3. public class Animation extends SurfaceView implements Runnable {
  4.     /*** GAME VARIABLES ***/
  5.     boolean isRunning = false;
  6.  
  7.     /*** MENU VARIABLES ***/
  8.  
  9.     /*** OBJECT VARIABLES ***/
  10.     SurfaceHolder surfaceHolderObj;
  11.     Thread threadObj = null;
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.     //###########################################################################################################
  20.     public Animation(Context context) {
  21.         super(context);
  22.         surfaceHolderObj = getHolder();
  23.     }
  24.  
  25.  
  26.  
  27.  
  28.  
  29.     //###########################################################################################################
  30.     public void resume() {
  31.         isRunning = true;
  32.         threadObj = new Thread(this);
  33.         threadObj.start();
  34.     }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.     //###########################################################################################################
  41.     public void pause() 
  42.     {
  43.         isRunning = false;
  44.         while (true) {     // stop thread
  45.             try {
  46.                 threadObj.join();
  47.             } catch (InterruptedException e) {
  48.                 e.printStackTrace();
  49.             }
  50.             break;
  51.         }
  52.         threadObj = null;
  53.     }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.     //###########################################################################################################
  60.     // main game loop
  61.     @Override
  62.     public void run() 
  63.     {
  64.         while (isRunning) 
  65.         {
  66.             if (!surfaceHolderObj.getSurface().isValid()) { // check if surface is valid
  67.                 // when come to 'continue' word than go back begin of loop and dont run below if statment
  68.                 continue;
  69.             }
  70.  
  71.             Canvas canvas = surfaceHolderObj.lockCanvas();
  72.  
  73.             surfaceHolderObj.unlockCanvasAndPost(canvas);
  74.         }//end of while loop
  75.     }//End of RUN method
  76. }
Aug 9 '13 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
Without having analyzed your code to closely it looks ok; I would however have various input listeners as separate classes rather than implementing the OnTouchListener in your MainActivity. That way you can add more input devices (e.g. the accelerometer or bluetooth controllers) without changing the code too much. Also it makes debugging much easier if you don't have input control and game flow in the same classes.

The book Beginning Android Games (by the guy who created libGDX) gives a pretty good introduction into how to build a framework for Android games, if you can I'd recommend reading it.
Aug 9 '13 #2

Post your reply

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