zoukankan      html  css  js  c++  java
  • Android游戏开发:物理游戏之重力系统开发--圆形自由落体Demo

        本节为大家提供有关物理游戏的知识,讲解了一个简单的圆形自由落体Demo的编写。。
    Java代码
    package com.himi;   
    import java.util.Random;   
    import java.util.Vector;   
    import android.content.Context;   
    import android.graphics.Canvas;   
    import android.graphics.Color;   
    import android.graphics.Paint;   
    import android.util.Log;   
    import android.view.KeyEvent;   
    import android.view.SurfaceHolder;   
    import android.view.SurfaceView;   
    import android.view.SurfaceHolder.Callback;   
    public class MySurfaceViee extends SurfaceView implements Callback, Runnable {   
        private Thread th;   
        private SurfaceHolder sfh;   
        private Canvas canvas;   
        private Paint paint;   
        private boolean flag;   
        public static int screenW, screenH;   
        private Vector<MyArc> vc;//这里定义装我们自定义圆形的容器   
        private Random ran;//随即库   
        public MySurfaceViee(Context context) {   
            super(context);   
            this.setKeepScreenOn(true);   
            vc = new Vector<MyArc>();   
            ran = new Random();//备注1   
            sfh = this.getHolder();   
            sfh.addCallback(this);   
            paint = new Paint();   
            paint.setAntiAlias(true);   
            setFocusable(true);   
        }   
        public void surfaceCreated(SurfaceHolder holder) {   
            flag = true;//这里都是上一篇刚讲过的。。。   
            th = new Thread(this);   
            screenW = this.getWidth();   
            screenH = this.getHeight();   
            th.start();   
        }   
        public void draw() {   
            try {   
                canvas = sfh.lockCanvas();   
                canvas.drawColor(Color.BLACK);   
                if (vc != null) {//当容器不为空,遍历容器中所有圆形画方法   
                    for (int i = 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 void logic() {//主逻辑   
            if (vc != null) {//当容器不为空,遍历容器中所有圆形逻辑   
                for (int i = 0; i < vc.size(); i++) {   
                    vc.elementAt(i).logic();   
                }   
            }   
        }   
        @Override  
        public boolean onKeyDown(int keyCode, KeyEvent event) {   
            //当按键事件响应,我们往容器中仍个我们的圆形实例   
            vc.addElement(new MyArc(ran.nextInt(this.getWidth()), ran.nextInt(100), ran.nextInt(50)));   
            return true;   
        }   
        public void run() {   
            // TODO Auto-generated method stub   
            while (flag) {   
                logic();   
                draw();   
                try {   
                    Thread.sleep(100);   
                } catch (Exception ex) {   
                }   
            }   
        }   
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {   
            Log.v("Himi", "surfaceChanged");   
        }   
        public void surfaceDestroyed(SurfaceHolder holder) {   
            flag = false;   
        }   
    }  
           OK,代码都很简单,也很清晰! 稍微说一句:像MyArc里面也有类似MysurfaceView中一样的方法 logic() 以及draw(),这样能更好的管理我们的代码结构,思路清晰,各尽其责,避免混乱。
  • 相关阅读:
    MySQL状态变量Aborted_connects与Aborted_clients浅析
    ERROR 1044 (42000): Access denied for user 'root'@'localhost'
    ERROR 3009 (HY000): Column count of mysql.user is wrong…..
    服务器CPU繁忙或内存压力引起网络掉包的浅析与总结
    ORACLE如何找到引起账号锁定的IP的一点思考与总结
    记一次坑爹的ORA-01152&ORA-01110错误
    RMAN restore fails with ORA-01180: can not create datafile 1
    SQL Server的优化器会缓存标量子查询结果集吗
    Snapshot origin volumes can be resized only while inactive: try lvchange -an
    MySQL InnoDB下关于MVCC的一个问题的分析
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/3706290.html
Copyright © 2011-2022 走看看