Android Game development: Physics Game Gravity system development--round freefall Demo

Source: Internet
Author: User

This section provides you with a knowledge of the physics game and explains the writing of a simple round freefall demo. Java Code PackageCom.himi; ImportJava.util.Random; ImportJava.util.Vector; ImportAndroid.content.Context; ImportAndroid.graphics.Canvas; ImportAndroid.graphics.Color; ImportAndroid.graphics.Paint; ImportAndroid.util.Log; Importandroid.view.KeyEvent; ImportAndroid.view.SurfaceHolder; ImportAndroid.view.SurfaceView; ImportAndroid.view.SurfaceHolder.Callback;  Public classMysurfacevieeextendsSurfaceviewImplementsCallback, Runnable {PrivateThread th; PrivateSurfaceholder SFH; PrivateCanvas Canvas; PrivatePaint paint; Private BooleanFlag;  Public Static intScreenw, Screenh; PrivateVector<myarc> VC;//this defines the container for our custom circular    PrivateRandom ran;//The library is immediately     PublicMysurfaceviee (Context context) {Super(context);  This. Setkeepscreenon (true); VC=NewVector<myarc>(); Ran=NewRandom ();//Remark 1SFH = This. Getholder (); Sfh.addcallback ( This); Paint=NewPaint (); Paint.setantialias (true); Setfocusable (true); }        Public voidsurfacecreated (Surfaceholder holder) {flag=true;//here is the last one just said ... th =NewThread ( This); Screenw= This. getwidth (); Screenh= This. GetHeight ();       Th.start (); }        Public voidDraw () {Try{Canvas=Sfh.lockcanvas ();               Canvas.drawcolor (Color.Black); if(VC! =NULL) {//when the container is not empty, iterate through all the circular drawing methods in the container                 for(inti = 0; I < vc.size (); i++) {Vc.elementat (i). Drawmyarc (canvas, paint); }               }           } Catch(Exception e) {//Todo:handle Exception}finally {               Try {                   if(Canvas! =NULL) sfh.unlockcanvasandpost (canvas); } Catch(Exception E2) {}}} Private voidLogic () {//Master Logic        if(VC! =NULL) {//when the container is not empty, traverse all the circular logic in the container             for(inti = 0; I < vc.size (); i++) {Vc.elementat (i). logic (); } }} @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {           //when the key event responds, we still have our circle instance in the containerVc.addelement (NewMyarc (Ran.nextint ( This. GetWidth ()), Ran.nextint (+), Ran.nextint (50))); return true; }        Public voidrun () {//TODO auto-generated Method Stub         while(flag) {logic ();               Draw (); Try{Thread.Sleep (100); } Catch(Exception ex) {}}}  Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {LOG.V ("Himi", "surfacechanged"); }        Public voidsurfacedestroyed (Surfaceholder holder) {flag=false; }} OK, the code is very simple, but also very clear! A little bit: like Myarc inside there are similar to Mysurfaceview in the same way logic () and draw (), so that we can better manage our code structure, clear thinking, do their duty, to avoid confusion. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.