Thread: Basic animation

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Basic animation


    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
    }
    Last edited by game2d; August 9th, 2013 at 12:28 AM.

IMN logo majestic logo threadwatch logo seochat tools logo