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

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

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

    @Override
    public 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;
        }
    }

    @Override
    public int getMapIndex() {
        return mapIndex;
    }

这里实现了一个接口

package com.su.botanywarzombies.model;

public interface Plant {
    public int getMapIndex();
}


public class BaseModel implements Plant{
    // 卡片放置区域的 mapIndex
    @Override
    public 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. 效果

performance

4. demo 下载

https://github.com/sufadi/BotanyWarZombies

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值