07 Android 植物人大战僵尸-修复放置卡片重叠Bug
生活随笔
收集整理的這篇文章主要介紹了
07 Android 植物人大战僵尸-修复放置卡片重叠Bug
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 相同位置放置2個卡片的Bug
2. 思路
卡片放置區域的各個位置有且僅有一個 mapIndex ,若出現重復則不再放置
2.1 卡片安放事件
package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {public void applay4Plant(int locationX, int locationY, EmplacePea emplacePea) {synchronized (mSurfaceHolder) {....switch (raceIndex) {case 0:gameLayout4plant0.add(new Pea(point.x, point.y, key));break;case 1:gameLayout4plant1.add(new Pea(point.x, point.y, key));break;case 2:gameLayout4plant2.add(new Pea(point.x, point.y, key));break;case 3:gameLayout4plant3.add(new Pea(point.x, point.y, key));break;case 4:gameLayout4plant4.add(new Pea(point.x, point.y, key));break;2.2 初始化 Pea 對象尤其是 mapIndex
package com.su.botanywarzombies.entity;public class Pea extends BaseModel {public class Pea extends BaseModel {private int farmeIndex = 0;// 禁止一個位置放2個動畫卡片private int mapIndex;public Pea(int locationX, int locationY, int mapIndex) {this.locationX = locationX;this.locationY = locationY;isLive = true;this.mapIndex = mapIndex;}@Overridepublic void drawSelf(Canvas canvas, Paint paint) {super.drawSelf(canvas, paint);if (isLive) {canvas.drawBitmap(Config.peaFlames[farmeIndex], locationX, locationY, paint);/* farmeIndex ++;if (farmeIndex == Config.peaFlames.length -1) {farmeIndex = 0;}*/farmeIndex = (++farmeIndex) % 8;}}@Overridepublic int getMapIndex() {return mapIndex;}這里實現了一個接口
package com.su.botanywarzombies.model;public interface Plant {public int getMapIndex(); }public class BaseModel implements Plant{// 卡片放置區域的 mapIndex@Overridepublic int getMapIndex() {return 0;} }2.3 下次放置卡片的去重復判斷
public void applay4Plant(int locationX, int locationY, EmplacePea emplacePea) {synchronized (mSurfaceHolder) {....if (isExist(key, raceIndex)) {return;}private boolean isExist(int key, int raceIndex) {switch (raceIndex) {case 0:return hasMapIndex(key, gameLayout4plant0);case 1:return hasMapIndex(key, gameLayout4plant1);case 2:return hasMapIndex(key, gameLayout4plant2);case 3:return hasMapIndex(key, gameLayout4plant3);case 4:return hasMapIndex(key, gameLayout4plant4);default:break;}return false;}private boolean hasMapIndex(int key, ArrayList<BaseModel> list) {for (BaseModel model : list) {if (model instanceof Plant) {if (key == model.getMapIndex()) {return true;}}}return false;}3. 效果
4. demo 下載
https://github.com/sufadi/BotanyWarZombies
總結
以上是生活随笔為你收集整理的07 Android 植物人大战僵尸-修复放置卡片重叠Bug的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: workbench如何截图_戴尔键盘电脑
- 下一篇: 部分有关 广告联盟作弊 与反作弊资料收集