视频:善知堂Android http://www.verycd.com/topics/2915940/
第十一集:弹球2
1.在实现游戏时很多时候用开源的游戏引擎。
比如说碰撞检测这些都会被封装。
2.一个线程控制一个小球,但是不建议大家这样写。
让ball 对象继承现成,可以是实现一个线程控制一个小球。
第十一集 背景
1.重新搭建程序
package wan.test; import android.R.color; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class RestartActivity extends Activity { class GameView extends SurfaceView implements Callback, Runnable { private SurfaceHolder sh = null; Paint paint = null; public GameView(Context context) { super(context); paint = new Paint(); paint.setColor(Color.GREEN); this.setFocusable(true);// 获得焦点 sh = this.getHolder();// 获得SurfaceView的“遥控器” sh.addCallback(this);// 注册了callback接口后,会自动执行surfaceCreated } @Override public void run() { while (true) { drawView(); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void drawView() { Canvas canvas = sh.lockCanvas(); canvas.drawColor(color.black);// 清屏 canvas.drawLine(0, 0, 100, 100, paint); sh.unlockCanvasAndPost(canvas);// 解锁了,然后会绘制在窗体上。 } @Override public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start();// 然后进入run方法 } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GameView my = new GameView(this); setContentView(my); } }
十一集的:09-41