日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

07 Android 植物人大战僵尸-修复放置卡片重叠Bug

發(fā)布時間:2023/12/20 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 07 Android 植物人大战僵尸-修复放置卡片重叠Bug 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 相同位置放置2個卡片的Bug

2. 思路

卡片放置區(qū)域的各個位置有且僅有一個 mapIndex ,若出現(xiàn)重復則不再放置

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;}

這里實現(xiàn)了一個接口

package com.su.botanywarzombies.model;public interface Plant {public int getMapIndex(); }public class BaseModel implements Plant{// 卡片放置區(qū)域的 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

總結(jié)

以上是生活随笔為你收集整理的07 Android 植物人大战僵尸-修复放置卡片重叠Bug的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。