当前位置:   article > 正文

植物大战僵尸经典开发步骤

开发植物大战僵尸中僵尸进场的思路

植物大战僵尸一直是一个很受欢迎的经典的小游戏,我主要用cocos2d-android做了一个类似的小demo,在这里主要介绍一下我做给这个小demo。

开发前各种准备工作

做一个小游戏我们首先要有一个地图吧,所以我用tiled这个软件来制作地图,安装和使用都挺简单了,画好后用notepad++打开看一下图片路径对不对,然后把图片、字体文件、地图文件.ttf放到工程的assets目录下,然后我们就可以在后面使用这些资源了。
当然我,我们先来了解一下一些其他相关知识点

加载地图

  1. CCTMXTiledMap map=CCTMXTiledMap.tiledMap("map.tmx");
  2. this.addChild(map);
  • 1
  • 2
  • 3

解析地图
//解析地图

  1. private void parseMap() {
  2. roadPoints=new ArrayList<CGPoint>();
  3. CCTMXObjectGroup objectGroupNamed=map.objectGroupNamed("road");
  4. ArrayList<HashMap<String,String>> objects=objectGroupNamed.objects;
  5. for(HashMap<String,String> hashMap:objects){
  6. int x=Integer.parseInt(hashMap.get("x"));
  7. int y=Integer.parseInt(hashMap.get("y"));
  8. CGPoint cgPoint=ccp(x,y);
  9. roadPoints.add(cgPoint);
  10. }
  11. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

//展示僵尸

  1. int position=3;
  2. private void loadZombies() {
  3. CCSprite sprite=CCSprite.sprite("z_1_01.png");
  4. sprite.setPosition(roadPoints.get(position));
  5. sprite.setAnchorPoint(0.5f,0);
  6. sprite.setScale(0.65f);
  7. this.addChild(sprite);
  8. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

粒子系统

eg:飘雪:CCParticleSnow

  1. private void loadParticle() {
  2. system = CCParticleSnow.node();
  3. // 设置雪花的样式
  4. system.setTexture(CCTextureCache.sharedTextureCache().addImage("f.png"));
  5. this.addChild(system, 1);
  6. }
  7. system.stopSystem();// 停止粒子系统
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

自定义效果,后缀.plis


声音引擎

  1. SoundEngine engine=SoundEngine.sharedEngine();
  2. // 1 上下文 2. 音乐资源的id 3 是否循环播放
  3. engine.playSound(CCDirector.theApp, R.raw.psy, true);
  • 1
  • 2
  • 3
  • 4

暂停和继续

1.onExit();
2.onEnter();

  1. @Override
  2. public boolean ccTouchesBegan(MotionEvent event) {
  3. this.onExit(); // 暂停
  4. this.getParent().addChild(new PauseLayer());// 让场景添加新的图层
  5. return super.ccTouchesBegan(event);
  6. }
  7. // 专门用来暂停的图层
  8. private class PauseLayer extends CCLayer{
  9. private CCSprite heart;
  10. public PauseLayer(){
  11. this.setIsTouchEnabled(true);// 打开触摸事件的开关
  12. heart = CCSprite.sprite("heart.png");
  13. // 获取屏幕的尺寸
  14. CGSize winSize = CCDirector.sharedDirector().getWinSize();
  15. heart.setPosition(winSize.width/2, winSize.height/2);// 让图片再屏幕的中间
  16. this.addChild(heart);
  17. }
  18. // 当点击PauseLayer的时候
  19. @Override
  20. public boolean ccTouchesBegan(MotionEvent event) {
  21. CGRect boundingBox = heart.getBoundingBox();
  22. // 把Android坐标系中的点 转换成Cocos2d坐标系中的点
  23. CGPoint convertTouchToNodeSpace = this.convertTouchToNodeSpace(event);
  24. if(CGRect.containsPoint(boundingBox, convertTouchToNodeSpace)){// 确实点击了心
  25. this.removeSelf();// 回收当前图层
  26. DemoLayer.this.onEnter();//游戏继续
  27. }
  28. return super.ccTouchesBegan(event);
  29. }
  30. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

项目正式开始

首先要有一个logo,logo下面有一个背景图片,需要加载一个进度条,一步步跳转就可以了,说到这个进度条,其实就是一个帧动画
用下面这段代码来看一下,当然这里之后我抽取了一个CommonUtils工具类,
这里写图片描述

  1. private void loading() {
  2. CCSprite loading=CCSprite.sprite("image/loading/loading_01.png");
  3. loading.setPosition(winSize.width/2, 30);
  4. this.addChild(loading);
  5. CCAction animate = CommonUtils.getAnimate("image/loading/loading_%02d.png", 9, false);
  6. loading.runAction(animate);
  7. start = CCSprite.sprite("image/loading/loading_start.png");
  8. start.setPosition(winSize.width/2, 30);
  9. start.setVisible(false);// 暂时不可见
  10. this.addChild(start);
  11. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

当然我们可以来看一下这个工具类,这在之后的开发中有很多的实用价值:

  1. `public class CommonUtils {
  2. /**
  3. * 切换图层
  4. * @param newLayer 新进入的图层
  5. */
  6. public static void changeLayer(CCLayer newLayer){
  7. CCScene scene=CCScene.node();
  8. scene.addChild(newLayer);
  9. CCFlipXTransition transition=CCFlipXTransition.transition(2, scene, 0);
  10. CCDirector.sharedDirector().replaceScene(transition);//切换场景 ,参数 新的场景
  11. }
  12. /**
  13. * 解析地图上 对象的所有点
  14. * @param map 地图
  15. * @param name 对象的名字
  16. * @return
  17. */
  18. public static List<CGPoint> getMapPoints(CCTMXTiledMap map,String name){
  19. List<CGPoint> points = new ArrayList<CGPoint>();
  20. // 解析地图
  21. CCTMXObjectGroup objectGroupNamed = map.objectGroupNamed(name);
  22. ArrayList<HashMap<String, String>> objects = objectGroupNamed.objects;
  23. for (HashMap<String, String> hashMap : objects) {
  24. int x = Integer.parseInt(hashMap.get("x"));
  25. int y = Integer.parseInt(hashMap.get("y"));
  26. CGPoint cgPoint = CCNode.ccp(x, y);
  27. points.add(cgPoint);
  28. }
  29. return points;
  30. }
  31. /**
  32. * 创建了序列帧的动作
  33. * @param format 格式化的路径
  34. * @param num 帧数
  35. * @param isForerver 是否永不停止的循环
  36. * @return
  37. */
  38. public static CCAction getAnimate(String format,int num,boolean isForerver){
  39. ArrayList<CCSpriteFrame> frames=new ArrayList<CCSpriteFrame>();
  40. //String format="image/loading/loading_%02d.png";
  41. for(int i=1;i<=num;i++){
  42. CCSpriteFrame spriteFrame = CCSprite.sprite(String.format(format, i)).displayedFrame();
  43. frames.add(spriteFrame);
  44. }
  45. CCAnimation anim=CCAnimation.animation("", 0.2f, frames);
  46. // 序列帧一般必须永不停止的播放 不需要永不停止播放,需要指定第二个参数 false
  47. if(isForerver){
  48. CCAnimate animate=CCAnimate.action(anim);
  49. CCRepeatForever forever=CCRepeatForever.action(animate);
  50. return forever;
  51. }else{
  52. CCAnimate animate=CCAnimate.action(anim,false);
  53. return animate;
  54. }
  55. }
  56. }`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

至于其他小的细节我就不一一啰嗦了,只说一下僵尸和植物对战需要的几个关键代码:

  1. /**
  2. * 处理游戏开始后的操作
  3. *
  4. *
  5. */
  6. public class GameCotroller {
  7. private GameCotroller() {
  8. }
  9. private static GameCotroller cotroller = new GameCotroller();
  10. public static GameCotroller getInstance() {
  11. return cotroller;
  12. }
  13. public static boolean isStart; // 游戏是否开始
  14. private CCTMXTiledMap map;
  15. private List<ShowPlant> selectPlants;
  16. private static List<FightLine> lines; // 管理了五行
  17. private List<CGPoint> roadPoints;
  18. static {
  19. lines = new ArrayList<FightLine>();
  20. for (int i = 0; i < 5; i++) {
  21. FightLine fightLine = new FightLine(i);
  22. lines.add(fightLine);
  23. }
  24. }
  25. /**
  26. * 开始游戏
  27. *
  28. * @param map
  29. * 游戏的地图
  30. * @param selectPlants
  31. * 玩家已选植物的集合
  32. */
  33. public void startGame(CCTMXTiledMap map, List<ShowPlant> selectPlants) {
  34. isStart = true;
  35. this.map = map;
  36. this.selectPlants = selectPlants;
  37. loadMap();
  38. // 添加僵尸
  39. // 定时器
  40. // 参数1 方法名(方法带float类型的参数) 参数2 调用方法的对象 参数3 间隔时间 参数4 是否暂停
  41. CCScheduler.sharedScheduler().schedule("addZombies", this, 1,false);
  42. // CCCallFunc.action(target, selector)
  43. // addZombies();
  44. // 安放植物
  45. // 僵尸攻击植物
  46. // 植物攻击僵尸
  47. progress();
  48. }
  49. CGPoint[][] towers = new CGPoint[5][9];
  50. private void loadMap() {
  51. roadPoints = CommonUtils.getMapPoints(map, "road");
  52. for (int i = 1; i <= 5; i++) {
  53. List<CGPoint> mapPoints = CommonUtils.getMapPoints(map,
  54. String.format("tower%02d", i));
  55. for (int j = 0; j < mapPoints.size(); j++) {
  56. towers[i - 1][j] = mapPoints.get(j);
  57. }
  58. }
  59. }
  60. /***
  61. * 添加僵尸
  62. *
  63. * @param t
  64. */
  65. public void addZombies(float t) {
  66. Random random = new Random();
  67. int lineNum = random.nextInt(5);// [0-5)
  68. PrimaryZombies primaryZombies = new PrimaryZombies(
  69. roadPoints.get(lineNum * 2), roadPoints.get(lineNum * 2 + 1));
  70. map.addChild(primaryZombies,1);// 让僵尸一直在植物的上面
  71. lines.get(lineNum).addZombies(primaryZombies);// 把僵尸记录到行战场中
  72. progress+=5;
  73. progressTimer.setPercentage(progress);//设置新的进度
  74. }
  75. public void endGame() {
  76. isStart = false;
  77. }
  78. private ShowPlant selectPlant; // 玩家选择的植物
  79. private Plant installPlant;
  80. /**
  81. * 当游戏开始后处理点击事件的方法
  82. *
  83. * @param point
  84. * 点击到的点
  85. */
  86. public void handleTouch(CGPoint point) {
  87. CCSprite chose = (CCSprite) map.getParent().getChildByTag(
  88. FightLayer.TAG_CHOSE);
  89. if (CGRect.containsPoint(chose.getBoundingBox(), point)) {
  90. // 认为玩家有可能选择了植物
  91. if (selectPlant != null) {
  92. selectPlant.getShowSprite().setOpacity(255);
  93. selectPlant = null;
  94. }
  95. for (ShowPlant plant : selectPlants) {
  96. CGRect boundingBox = plant.getShowSprite().getBoundingBox();
  97. if (CGRect.containsPoint(boundingBox, point)) {
  98. // 玩家选择了植物
  99. selectPlant = plant;
  100. selectPlant.getShowSprite().setOpacity(150);
  101. int id = selectPlant.getId();
  102. switch (id) {
  103. case 1:
  104. installPlant =new PeasePlant();
  105. break;
  106. case 4:
  107. installPlant = new Nut();
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. }
  114. } else {
  115. // 玩家有可能安放植物
  116. if (selectPlant != null) {
  117. int row = (int) (point.x / 46) - 1; // 1-9 0-8
  118. int line = (int) ((CCDirector.sharedDirector().getWinSize().height - point.y) / 54) - 1;// 1-5
  119. // 0-4
  120. // 限制安放的植物的范围
  121. if (row >= 0 && row <= 8 && line >= 0 && line <= 4) {
  122. // 安放植物
  123. // selectPlant.getShowSprite().setPosition(point);
  124. // installPlant.setPosition(point); // 坐标需要修改
  125. installPlant.setLine(line);// 设置植物的行号
  126. installPlant.setRow(row); // 设置植物的列号
  127. installPlant.setPosition(towers[line][row]); // 修正了植物的坐标
  128. FightLine fightLine = lines.get(line);
  129. if (!fightLine.containsRow(row)) { // 判断当前列是否已经添加了植物 如果添加了 就不能再添加了
  130. fightLine.addPlant(installPlant);// 把植物记录到了行战场中
  131. map.addChild(installPlant);
  132. }
  133. }
  134. installPlant = null;
  135. selectPlant.getShowSprite().setOpacity(255);
  136. selectPlant = null;// 下次安装需要重新选择
  137. }
  138. }
  139. }
  140. CCProgressTimer progressTimer;
  141. int progress=0;
  142. private void progress() {
  143. // 创建了进度条
  144. progressTimer = CCProgressTimer.progressWithFile("image/fight/progress.png");
  145. // 设置进度条的位置
  146. progressTimer.setPosition(CCDirector.sharedDirector().getWinSize().width - 80, 13);
  147. map.getParent().addChild(progressTimer); //图层添加了进度条
  148. progressTimer.setScale(0.6f); // 设置了缩放
  149. progressTimer.setPercentage(0);// 每增加一个僵尸需要调整进度,增加5
  150. progressTimer.setType(CCProgressTimer.kCCProgressTimerTypeHorizontalBarRL); // 进度的样式
  151. CCSprite sprite = CCSprite.sprite("image/fight/flagmeter.png");
  152. sprite.setPosition(CCDirector.sharedDirector().getWinSize().width - 80, 13);
  153. map.getParent().addChild(sprite);
  154. sprite.setScale(0.6f);
  155. CCSprite name = CCSprite.sprite("image/fight/FlagMeterLevelProgress.png");
  156. name.setPosition(CCDirector.sharedDirector().getWinSize().width - 80, 5);
  157. map.getParent().addChild(name);
  158. name.setScale(0.6f);
  159. }
  160. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186

还有一个很关键的就是:

  1. /**
  2. * 对战界面的图层
  3. *
  4. */
  5. public class FightLayer extends BaseLayer {
  6. public static final int TAG_CHOSE = 10;
  7. private CCTMXTiledMap map;
  8. private List<CGPoint> zombilesPoints;
  9. private CCSprite choose; // 玩家可选植物的容器
  10. private CCSprite chose; // 玩家已选植物的容器
  11. public FightLayer() {
  12. init();
  13. }
  14. private void init() {
  15. loadMap();
  16. parserMap();
  17. showZombies();
  18. moveMap();
  19. }
  20. // 加载展示用的僵尸
  21. private void showZombies() {
  22. for (int i = 0; i < zombilesPoints.size(); i++) {
  23. CGPoint cgPoint = zombilesPoints.get(i);
  24. ShowZombies showZombies = new ShowZombies();
  25. showZombies.setPosition(cgPoint);// 给展示用的僵尸设置了位置
  26. map.addChild(showZombies);// 注意: 把僵尸加载到地图上
  27. }
  28. }
  29. private void parserMap() {
  30. zombilesPoints = CommonUtils.getMapPoints(map, "zombies");
  31. }
  32. // 移动地图
  33. private void moveMap() {
  34. int x = (int) (winSize.width - map.getContentSize().width);
  35. CCMoveBy moveBy = CCMoveBy.action(3, ccp(x, 0));
  36. CCSequence sequence = CCSequence
  37. .actions(CCDelayTime.action(4), moveBy, CCDelayTime.action(2),
  38. CCCallFunc.action(this, "loadContainer"));
  39. map.runAction(sequence);
  40. }
  41. private void loadMap() {
  42. map = CCTMXTiledMap.tiledMap("image/fight/map_day.tmx");
  43. map.setAnchorPoint(0.5f, 0.5f);
  44. CGSize contentSize = map.getContentSize();
  45. map.setPosition(contentSize.width / 2, contentSize.height / 2);
  46. this.addChild(map);
  47. }
  48. // 加载两个容器
  49. public void loadContainer() {
  50. chose = CCSprite.sprite("image/fight/chose/fight_chose.png");
  51. chose.setAnchorPoint(0, 1);
  52. chose.setPosition(0, winSize.height);// 设置位置是屏幕的左上角
  53. this.addChild(chose,0,TAG_CHOSE);
  54. choose = CCSprite.sprite("image/fight/chose/fight_choose.png");
  55. choose.setAnchorPoint(0, 0);
  56. this.addChild(choose);
  57. loadShowPlant();
  58. start = CCSprite.sprite("image/fight/chose/fight_start.png");
  59. start.setPosition(choose.getContentSize().width/2, 30);
  60. choose.addChild(start);
  61. }
  62. private List<ShowPlant> showPlatns; // 展示用的植物的集合
  63. // 加载展示用的植物
  64. private void loadShowPlant() {
  65. showPlatns = new ArrayList<ShowPlant>();
  66. for (int i = 1; i <= 9; i++) {
  67. ShowPlant plant = new ShowPlant(i); // 创建了展示的植物
  68. CCSprite bgSprite = plant.getBgSprite();
  69. bgSprite.setPosition(16 + ((i - 1) % 4) * 54,
  70. 175 - ((i - 1) / 4) * 59);
  71. choose.addChild(bgSprite);
  72. CCSprite showSprite = plant.getShowSprite();// 获取到了展示的精灵
  73. // 设置坐标
  74. showSprite.setPosition(16 + ((i - 1) % 4) * 54,
  75. 175 - ((i - 1) / 4) * 59);
  76. choose.addChild(showSprite); // 添加到了容器上
  77. showPlatns.add(plant);
  78. }
  79. setIsTouchEnabled(true);
  80. }
  81. public void unlock(){
  82. isLock=false;
  83. }
  84. private List<ShowPlant> selectPlants = new CopyOnWriteArrayList<ShowPlant>();// 已经选中植物的集合
  85. boolean isLock;
  86. boolean isDel; // 是否删除了选中的植物
  87. private CCSprite start;
  88. @Override
  89. public boolean ccTouchesBegan(MotionEvent event) {
  90. // 需要把Android坐标系中的点 转换成Cocos2d坐标系中的点
  91. CGPoint point = this.convertTouchToNodeSpace(event);
  92. if(GameCotroller.isStart){// 如果游戏开始了 交给GameCtoller 处理
  93. GameCotroller.getInstance().handleTouch(point);
  94. return super.ccTouchesBegan(event);
  95. }
  96. CGRect boundingBox = choose.getBoundingBox();
  97. CGRect choseBox = chose.getBoundingBox();
  98. // 玩家有可能反选植物
  99. if(CGRect.containsPoint(choseBox, point)){
  100. isDel=false;
  101. for(ShowPlant plant:selectPlants){
  102. CGRect selectPlantBox = plant.getShowSprite().getBoundingBox();
  103. if(CGRect.containsPoint(selectPlantBox, point)){
  104. CCMoveTo moveTo=CCMoveTo.action(0.5f, plant.getBgSprite().getPosition());
  105. plant.getShowSprite().runAction(moveTo);
  106. selectPlants.remove(plant);// 走到这一步 确实代表反选植物了
  107. isDel=true;
  108. continue;// 跳出本次循环,继续下次循环
  109. }
  110. if(isDel){
  111. CCMoveBy ccMoveBy=CCMoveBy.action(0.5f, ccp(-53, 0));
  112. plant.getShowSprite().runAction(ccMoveBy);
  113. }
  114. }
  115. }else if (CGRect.containsPoint(boundingBox, point)) {
  116. if(CGRect.containsPoint(start.getBoundingBox(), point)){
  117. // 点击了一起来摇滚
  118. ready();
  119. }else if (selectPlants.size() < 5&&!isLock) { //如果已经选择满了 就不能再选择了
  120. // 有可能 选择植物
  121. for (ShowPlant plant : showPlatns) {
  122. CGRect boundingBox2 = plant.getShowSprite()
  123. .getBoundingBox();
  124. if (CGRect.containsPoint(boundingBox2, point)) {// 如果点恰好落在植物展示的精灵矩形之中
  125. // 当前植物被选中了
  126. isLock=true;
  127. // System.out.println("我被选中了...");
  128. CCMoveTo moveTo = CCMoveTo.action(
  129. 0.5f,
  130. ccp(75 + selectPlants.size() * 53,
  131. 255));
  132. CCSequence sequence=CCSequence.actions(moveTo, CCCallFunc.action(this, "unlock"));
  133. plant.getShowSprite().runAction(sequence);
  134. selectPlants.add(plant);
  135. }
  136. }
  137. }
  138. }
  139. return super.ccTouchesBegan(event);
  140. }
  141. /**
  142. * 点击了一起来摇滚 做的操作
  143. */
  144. private void ready() {
  145. // 缩小玩家已选植物容器
  146. chose.setScale(0.65f);
  147. // 把选中的植物重新添加到 存在的容器上
  148. for(ShowPlant plant:selectPlants){
  149. plant.getShowSprite().setScale(0.65f);// 因为父容器缩小了 孩子一起缩小
  150. plant.getShowSprite().setPosition(
  151. plant.getShowSprite().getPosition().x * 0.65f,
  152. plant.getShowSprite().getPosition().y
  153. + (CCDirector.sharedDirector().getWinSize().height - plant
  154. .getShowSprite().getPosition().y)
  155. * 0.35f);// 设置坐标
  156. this.addChild(plant.getShowSprite());
  157. }
  158. choose.removeSelf();// 回收容器
  159. // 地图的平移
  160. int x = (int) (map.getContentSize().width-winSize.width);
  161. CCMoveBy moveBy = CCMoveBy.action(1, ccp(x, 0));
  162. CCSequence sequence=CCSequence.actions(moveBy, CCCallFunc.action(this, "preGame"));
  163. map.runAction(sequence);
  164. }
  165. private CCSprite ready;
  166. public void preGame(){
  167. ready=CCSprite.sprite("image/fight/startready_01.png");
  168. ready.setPosition(winSize.width/2, winSize.height/2);
  169. this.addChild(ready);
  170. String format="image/fight/startready_%02d.png";
  171. CCAction animate = CommonUtils.getAnimate(format, 3, false);
  172. CCSequence sequence=CCSequence.actions((CCAnimate)animate, CCCallFunc.action(this, "startGame"));
  173. ready.runAction(sequence);
  174. }
  175. public void startGame(){
  176. ready.removeSelf();// 移除中间的序列帧
  177. GameCotroller cotroller=GameCotroller.getInstance();
  178. cotroller.startGame(map,selectPlants);
  179. }
  180. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214

这里写图片描述

在做这个的过程中总是遇到空指针的异常,例如这次:后来发现是因为我ShowPlant.java这个地方写错了
这里写图片描述

做媒一个项目都需要一些成长和一些经验,在之前的CCSequence,CGPoint,CCSprite,CCTMXTiledMap
后面又学会了CCScheduler.sharedScheduler().schedule(“attackPlant”, this, 0.5f, false),ready.removeSelf();// 移除中间的序列帧等内容,让我对这整个架构有了初步了了解了实践,学习之路很长,我们一起加油!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/770527
推荐阅读
相关标签
  

闽ICP备14008679号