android

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

    android

    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
    Code:
    //imports etcc...
    
    public class MainActivity extends Activity implements OnTouchListener{
    
    	int delay = 50;
    	
    	/*** Object variables ***/
    	Animation animationObj;
    	WakeLock wL;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// you phone wont lock when this app is running
    		PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    		// hidden step -- mainifest > permisions > add > users permision > ok > name:android.permission.WAKE_LOCK > enter
    		wL = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Whatever");
    		
    		super.onCreate(savedInstanceState);
    		super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //lock orientation
    		requestWindowFeature(Window.FEATURE_NO_TITLE);                            // remove title
    		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    							WindowManager.LayoutParams.FLAG_FULLSCREEN);          // remove icons top
    		wL.acquire();                                                             //start dont lock the screen
    		animationObj = new Animation(this);
    		animationObj.setOnTouchListener(this);
    		setContentView(animationObj);
    		
    		
    	}
    
    	@Override
    	 protected void onPause(){
    		 super.onPause();
    		 animationObj.pause();
    		 wL.release();
    	 }
    	 
    	 
    	 
    	 @Override
    	 protected void onResume(){
    		 super.onResume();
    		 animationObj.resume();
    	 }
    	 
    	 
    	 @Override
    		public boolean onTouch(View v, MotionEvent event) {
    		//show down the speed so slow phone can run it
    			try{
    				Thread.sleep(delay);
    			} catch(InterruptedException e){
    				e.printStackTrace();
    			}
    			
    			if(event.getAction() == MotionEvent.ACTION_DOWN){ //user toch the screen
    				
    			}
    			else if(event.getAction() == MotionEvent.ACTION_UP){ //user let go of touch
    				
    			}
    			
    			//return false if you dont want it to drag
    			//return true if you want it to drag
    			return true;
    		}
    
    	
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    }


    //2nd class = animation and game loop
    Code:
    \\imports etc..
    
    public class Animation extends SurfaceView implements Runnable {
    	/*** GAME VARIABLES ***/
    	boolean isRunning = false;
    
    	/*** MENU VARIABLES ***/
    
    	/*** OBJECT VARIABLES ***/
    	SurfaceHolder surfaceHolderObj;
    	Thread threadObj = null;
    
    	
    	
    	
    	
    	
    	
    	//###########################################################################################################
    	public Animation(Context context) {
    		super(context);
    		surfaceHolderObj = getHolder();
    	}
    
    	
    	
    	
    	
    	//###########################################################################################################
    	public void resume() {
    		isRunning = true;
    		threadObj = new Thread(this);
    		threadObj.start();
    	}
    
    	
    	
    
    	
    	//###########################################################################################################
    	public void pause() 
    	{
    		isRunning = false;
    		while (true) { 	// stop thread
    			try {
    				threadObj.join();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			break;
    		}
    		threadObj = null;
    	}
    
    	
    	
    	
    	
    	//###########################################################################################################
    	// main game loop
    	@Override
    	public void run() 
    	{
    		while (isRunning) 
    		{
    			if (!surfaceHolderObj.getSurface().isValid()) { // check if surface is valid
    				// when come to 'continue' word than go back begin of loop and dont run below if statment
    				continue;
    			}
    			
    			Canvas canvas = surfaceHolderObj.lockCanvas();
    
    			surfaceHolderObj.unlockCanvasAndPost(canvas);
    		}//end of while loop
    	}//End of RUN method
    }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    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.

    Comment

    Working...