05 Android 植物人大战僵尸-安放豌豆射手到图层
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                05 Android 植物人大战僵尸-安放豌豆射手到图层
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                1. 效果
2.思路
點擊豌豆射手卡片,生成卡片,并將觸摸事件傳遞給卡片,以便卡片能移動
2.1 第1觸摸事件
Activity 的觸摸事件
package com.su.botanywarzombies;public class MainActivity extends Activity {private GameView mGameView;@Overridepublic boolean onTouchEvent(MotionEvent event) {return mGameView.onTouchEvent(event);}2.2 第2觸摸事件
mGameView 中的卡片選卡觸摸事件
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {@Overridepublic boolean onTouchEvent(MotionEvent event) {// 第一圖層,背景圖和放置狀態(tài)欄的卡片for (BaseModel model : gameLayout1) {if (model instanceof TouchAble) {if (((TouchAble) model).onTouch(event)) {// true 表示觸摸事件到此為止不再響應return true;}}}2.3 第3觸摸事件
豌豆射手卡片被點擊事件
public class SeedPea extends BaseModel implements TouchAble {@Overridepublic boolean onTouch(MotionEvent event) {int x = (int) event.getX();int y = (int) event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 判斷是否點擊了卡片,根據(jù)是否焦點在觸摸區(qū)域范圍內(nèi)if (touchArea.contains(x, y)) {Log.d("sufadi", "touch seed pea");applyEmplacePea();return true;}default:break;}return false;}2.4 第4觸摸事件
申請一張卡片,并且獲取觸摸事件,已方便位移操作
2.4.1 請求安放一個卡片
public class SeedPea extends BaseModel implements TouchAble {// 請求安放一個卡片private void applyEmplacePea() {// 向 GameView圖層加GameView.getInstance().applyEmplacePea(locationX, locationY);}2.4.3 GameView 新增該卡片
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {public void applyEmplacePea(int locationX, int locationY) {synchronized (mSurfaceHolder) {// 頂層加入被安放狀態(tài)的植物Log.d("sufadi", "applyEmplacePea add EmplacePea");gameLayout1.add(new EmplacePea(locationX, locationY));}}2.4.3 GameView 繪制該卡片,見圖層 gameLayout1
注意下面的圖層是有先后順序的
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {private void onDrawing(Canvas mCanvas) {for (BaseModel model : gameLayout2) {model.drawSelf(mCanvas, mPaint);}// 后畫的會覆蓋先畫的,故位置在gameLayout2下面if (gameLayout1 != null && !gameLayout1.isEmpty()) {for (BaseModel model : gameLayout1) {model.drawSelf(mCanvas, mPaint);}}}2.4.4 GameView 傳遞觸摸事件
注意下面的圖層是有先后順序的
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {@Overridepublic boolean onTouchEvent(MotionEvent event) {// 第一圖層,背景圖和放置狀態(tài)欄的卡片for (BaseModel model : gameLayout1) {if (model instanceof TouchAble) {if (((TouchAble) model).onTouch(event)) {// true 表示觸摸事件到此為止不再響應return true;}}}// 第二圖層,安放卡片for (BaseModel model : gameLayout2) {if (model instanceof TouchAble) {if (((TouchAble) model).onTouch(event)) {// true 表示觸摸事件到此為止不再響應return true;}}}return false;}2.4.5 卡片的移動事件
public class EmplacePea extends BaseModel implements TouchAble {// 觸摸區(qū)域private Rect touchArea;@Overridepublic boolean onTouch(MotionEvent event) {int x = (int) event.getX();int y = (int) event.getY();if (touchArea.contains(x, y)) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_MOVE:// 卡片位置更新locationX = x - Config.peaFlames[0].getWidth() / 2;locationY = y - Config.peaFlames[0].getHeight() / 2;// 觸摸點跟隨,重新移到新的位置touchArea.offsetTo(locationX, locationY);break;case MotionEvent.ACTION_UP:break;default:break;}}return false;}2.4.6 上述步驟每60秒不斷繪制,故圖片順利移動
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {@Overridepublic void run() {while (gameRunFlag) {// 這里需要考慮線程同步synchronized (mSurfaceHolder) {try {// 鎖住畫布才能繪圖mCanvas = mSurfaceHolder.lockCanvas();mCanvas.drawBitmap(Config.gameBg, 0, 0, mPaint);// 放置卡片的起始 X 坐標是 (界面寬度-卡片寬度) / 2mCanvas.drawBitmap(Config.seekBank, (Config.screenWidth - Config.seekBank.getWidth()) / 2, 0, mPaint);onDrawing(mCanvas);} catch (Exception e) {// e.printStackTrace();} finally {// 解鎖并提交mSurfaceHolder.unlockCanvasAndPost(mCanvas);}}try {Thread.sleep(60);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3. Demo 下載
https://github.com/sufadi/BotanyWarZombies
總結(jié)
以上是生活随笔為你收集整理的05 Android 植物人大战僵尸-安放豌豆射手到图层的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: English语法_副词 - ago /
- 下一篇: PP-LCNet-YoloV5
