1. 效果图

2.思路

仿照这豌豆射手

2.1 点击状态栏的向日葵

package com.su.botanywarzombies.entity;public class SeedFlower extends BaseModel implements TouchAble {@Overridepublic boolean onTouch(MotionEvent event) {int x = (int) event.getX();int y = (int) event.getY();// x,y坐标是否在触摸区域if (touchArea.contains(x, y)) {Log.d("sufadi", "touch seed flower");apply4EmplaceFlower();return true;}return false;}private void apply4EmplaceFlower() {GameView.getInstance().applyEmplacePlant(locationX, locationY, this);}

2.2 可拖动的向日葵图片

package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {public void applyEmplacePlant(int locationX, int locationY, BaseModel mBaseModel) {synchronized (mSurfaceHolder) {// 顶层加入被安放状态的植物// gameLayout1 表示正在安放植物的集合// 安放状态下一次只能放一个植物if (gameLayout1.size() < 1) {if (mBaseModel instanceof SeedPea) {gameLayout1.add(new EmplacePea(locationX, locationY));} else if (mBaseModel instanceof SeedFlower) {gameLayout1.add(new EmplaceFlower(locationX, locationY));}}}}

2.3 松开可拖动的向日葵卡片

MotionEvent.ACTION_UP 事件

package com.su.botanywarzombies.entity;public class EmplaceFlower extends BaseModel implements TouchAble {@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, locationY)的变化locationX = x - Config.peaFlames[0].getWidth() / 2;locationY = y - Config.peaFlames[0].getHeight() / 2;// 触摸点跟随,重新移到新的位置touchArea.offsetTo(locationX, locationY);break;case MotionEvent.ACTION_UP:// 对象标志位失效,即死亡对象isLive = false;GameView.getInstance().applay4Plant(locationX, locationY, Config.TYPE_FLOWER);break;default:break;}}return false;}

2.4 可安放区域自定定位邻近位置

package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {public void applay4Plant(int locationX, int locationY, int type) {synchronized (mSurfaceHolder) {// 当前卡片中心坐标与可安放集合最近的坐标点for (Integer key : Config.plantPoint.keySet()) {// 1. 两个卡片中心点距离是一个单元格宽度,这里我们取两个卡片直接画中心线// 2. 可放置卡片偏向那,自动放置哪里final int TOTAL_ROW = 11;final int TOTAL_LINE = 6;Point point = Config.plantPoint.get(key);if ((Math.abs(locationX - point.x) < Config.screenWidth / TOTAL_ROW / 2) && (Math.abs(locationY - point.y) < Config.screenHeight / TOTAL_LINE / 2)) {int raceIndex = TOTAL_LINE;for (int i = 0; i < Config.racWayYpoint.length; i++) {if (point.y == Config.racWayYpoint[i]) {raceIndex = i;}}if (isExist(key, raceIndex)) {return;}switch (raceIndex) {case 0:gameLayout4plant0.add(getPlant(point.x, point.y, key, type));break;case 1:gameLayout4plant1.add(getPlant(point.x, point.y, key, type));break;case 2:gameLayout4plant2.add(getPlant(point.x, point.y, key, type));break;case 3:gameLayout4plant3.add(getPlant(point.x, point.y, key, type));break;case 4:gameLayout4plant4.add(getPlant(point.x, point.y, key, type));break;default:break;}}}}}private BaseModel getPlant(int locationX, int locationY, int key, int type) {switch (type) {case Config.TYPE_PEA:return new Pea(locationX, locationY, key);case Config.TYPE_FLOWER:return new Flower(locationX, locationY, key);default:break;}return null;}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;}

2.5 一个不断点头的向日葵

package com.su.botanywarzombies.entity;import android.graphics.Canvas;
import android.graphics.Paint;import com.su.botanywarzombies.constant.Config;
import com.su.botanywarzombies.model.BaseModel;public class Flower extends BaseModel {private int farmeIndex = 0;// 禁止一个位置放2个动画卡片private int mapIndex;public Flower(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.flowerFlames[farmeIndex], locationX, locationY, paint);/** farmeIndex ++; if (farmeIndex == Config.peaFlames.length -1) {* farmeIndex = 0; }*/farmeIndex = (++farmeIndex) % 8;}}@Overridepublic int getMapIndex() {return mapIndex;}
}

3. Demo 下载

https://github.com/sufadi/BotanyWarZombies

08 Android 植物人大战僵尸-添加向日葵卡片相关推荐

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

    1. 相同位置放置2个卡片的Bug 2. 思路 卡片放置区域的各个位置有且仅有一个 mapIndex ,若出现重复则不再放置 2.1 卡片安放事件 package com.su.botanywarzo ...

  2. 10 Android 植物人大战僵尸-矩形的碰撞监测

    0. 学习来源 没想到教学视频就只讲到这里,视频来源是传智播客之植物大战僵尸Android开发教程+课件+源码 作者是侯哥,非常nice,视频是很早的,当时还是用Eclipse编写的,虽然现在是And ...

  3. 02 Android 植物人大战僵尸-太阳花和豌豆射手卡片

    1.放置太阳花和豌豆射手卡片 2.基本思路 太阳花卡片的起始X位置 = 根据状态栏的X坐标 + 1个图片宽度 豌豆射手卡片的起始X位置 = 根据状态栏的X坐标 + 2个图片宽度 // 状态栏位置 + ...

  4. 04 Android 植物人大战僵尸-卡片的触摸事件

    1. 第一层触摸事件-Activty public class MainActivity extends Activity {@Overridepublic boolean onTouchEvent( ...

  5. 10 Android 植物人大战僵尸-生成僵尸

    1. 效果 2. 需求 每隔一定的时间在5个跑道中随机生成僵尸,并且从右往左移动 3. 开发 定义一个僵尸生成管理者,负责定时生成僵尸,这里定义的是每隔15秒生成僵尸 package com.su.b ...

  6. 09 Android 植物人大战僵尸-生成小太阳

    1. 效果 2. 需求 安置向日葵完成,等待 10 秒 产生一个太阳 太阳 5 秒钟如果没有被收集,则自动消失 太阳如果被点击,即收集,则执行位移操作,回到卡片状态栏的太阳卡槽里 3. 思路 向日葵的 ...

  7. 02 Android 植物人大战僵尸-背景图层布置

    1.背景图层布置效果 该图层主要是2张图片过程,草地和放置卡片的状态图层过程,属于静态图片范畴 2. 背景图片的屏幕适配 这里主要根据图片的缩放比对原始图片进行重新绘制,达到适配屏幕的效果 缩放比宽 ...

  8. 01 Android 植物人大战僵尸-画个方块

    1. SurfaceView 更新的速度特别快,可以直接从内存或者DMA等硬件接口中取得图像数据的绘图容器 可以在主线程之外的线程中向屏幕绘图上,可以避免绘图任务繁重导致主线程阻塞,从而提高程序的反应 ...

  9. 05 Android 植物人大战僵尸-安放豌豆射手到图层

    1. 效果 2.思路 点击豌豆射手卡片,生成卡片,并将触摸事件传递给卡片,以便卡片能移动 2.1 第1触摸事件 Activity 的触摸事件 package com.su.botanywarzombi ...

最新文章

  1. c++ smart pointer 趣谈
  2. Android如何把 内容复制到剪贴板
  3. Windows Media Center .MCL文件代码执行漏洞(MS16-059)
  4. nhinx php 调优,高流量站点NGINX与PHP-fpm配置优化
  5. batchnorm and relu_日本AND荷重传感器
  6. pthread_cancel()与pthread_cancel
  7. Android Java编写布局
  8. Unix/Linux环境C编程新手教程(40) 初识文件操作
  9. HTML5写的简单登录页面
  10. latex表格自动换行
  11. ida android so 断点,IDA Pro 7.0+调试Android so飘云整理(基于Android5.1.1)
  12. C++学习(一八一)android的NDK、SDK版本的对应关系
  13. docker容器获取宿主ip地址
  14. volatile与Synchronized的异同
  15. “七段数码管绘制”实例详解
  16. Linux---笔记总结
  17. pandorabox php7,飞鱼星G7免拆刷入pandorabox
  18. 玩客云刷上Armbian的体验
  19. 青龙羊毛——酷狗滴滴
  20. 企业发放的奖金根据利润提成。

热门文章

  1. 入门案例理解 —— 命令模式(Command Pattern)
  2. Unity3D项目十:简单坦克大战
  3. 【网络设备配置与管理实验二】路由器口令的配置
  4. 【GAN实战项目:DCGAN in Tensorflow生成动漫人物头像】代码学习
  5. [商业][思想]《免费 -- 商业的未来 Free The Future of a Radical Price》 -- 克里斯·安德森(美)...
  6. github python抢票_实测两款 GitHub 开源抢票插件,所有坑都帮你踩过了
  7. 解密新华三云密码之S12516X-AF核心交换机一览众山小
  8. 数学与Python有机结合及统计学、微积分、线性代数相关资源、图形软件
  9. java支付宝网页授权登录界面_支付宝开发平台之第三方授权登录与获取用户信息...
  10. 看10篇博文写点感受,不积跬步,无以至千里、不积细流,无以成江海