Block.h

  1. #pragma once
  2. #ifdef WIN32
  3. #pragma execution_character_set("utf-8")
  4. #include "cocos2d.h"
  5. #include "ui/CocosGUI.h"
  6. #include "SimpleAudioEngine.h"
  7. #include <string.h>
  8. #include <time.h>
  9. #include "cocos-ext.h"
  10. using namespace cocos2d::ui;
  11. using namespace CocosDenshion;
  12. USING_NS_CC_EXT;
  13. USING_NS_CC;
  14. class Block:public Sprite
  15. {
  16. private:
  17. int myClasses; //方块的分类:0,1,2,3..等分类
  18. float blockSize = 70.0f; //每个方块的默认大小
  19. bool doesErase = false;//是否会被清除的记号
  20. Sprite* mySprite;
  21. int posX; //游戏中的位置标记,从左下角开始
  22. int posY; //游戏中的位置标记,从左下角开始
  23. Vec2 pos[9][20]; //保存着每一个坐标对应的具体Vec2位置
  24. float movingTime = 0.25f; //移动时间
  25. bool selected = false;//是否被选中的记号
  26. public:
  27. Block(int myClasses, int x, int y);
  28. Block();
  29. static Block* Block::create(const std::string& filename, int myClasses, int x, int y);
  30. void setMySprite(Sprite* sprite);
  31. void setDoesErase(bool flag); //设置当前的方块用不用被标记删除
  32. void setSelected(bool flag);
  33. int getClasses();
  34. bool getDoesErase();
  35. bool getSelected();
  36. int getX();
  37. int getY();
  38. Sprite* getMySprite();
  39. void moveToPos(int x, int y);
  40. void moveByOffset(int x, int y);
  41. void remove(float dl);
  42. void updateVisiable(float dl); //实时更新方块可见性
  43. void blastAnimation();
  44. ~Block();
  45. };
  46. #endif


Block.cpp

  1. #include "Block.h"
  2. Block::Block(int myClasses, int x, int y) {
  3. this->myClasses = myClasses;
  4. this->doesErase = false;
  5. //log("我的分类是:%d", this->myClasses);
  6. this->posX = x;
  7. this->posY = y;
  8. for (int i = 0; i < 9; i++)
  9. for (int j = 0; j < 20; j++)
  10. this->pos[i][j] = Vec2(blockSize * i, blockSize * j); //初始化坐标信息
  11. //this->pos[i][j] = Vec2(blockSize * i - 40, blockSize * j + 150); //初始化坐标信息
  12. }
  13. int Block::getX() {
  14. return this->posX;
  15. }
  16. int Block::getY() {
  17. return this->posY;
  18. }
  19. int Block::getClasses() {
  20. return this->myClasses;
  21. }
  22. bool Block::getDoesErase() {
  23. return this->doesErase;
  24. }
  25. void Block::setDoesErase(bool flag) {
  26. this->doesErase = flag;
  27. }
  28. Sprite* Block::getMySprite() {
  29. return this->mySprite;
  30. }
  31. void Block::remove(float dl) {
  32. log("我将要被删除,我的编号为:%d", myClasses);
  33. this->removeFromParent();
  34. }
  35. Block* Block::create(const std::string& filename, int myClasses, int x, int y)
  36. {
  37. Sprite *sprite = new (std::nothrow) Sprite();
  38. Block *block = new Block(myClasses, x, y);
  39. if (sprite && sprite->initWithFile(filename))
  40. {
  41. sprite->autorelease();
  42. //sprite->setAnchorPoint(Point(0, 0));
  43. sprite->setPosition(Vec2(sprite->getContentSize().width / 2, sprite->getContentSize().width / 2));
  44. block->setMySprite(sprite); //设置本方块中的精灵图片
  45. block->setContentSize(sprite->getContentSize()); //统一方块的大小
  46. if (y > 7) { //如果高过游戏区域,设置为不可见
  47. sprite->setVisible(false);
  48. }
  49. block->addChild(sprite); //本方块挂载精灵
  50. return block;
  51. }
  52. CC_SAFE_DELETE(block);
  53. return nullptr;
  54. }
  55. void Block::setSelected(bool flag) {
  56. if (flag) {
  57. log("该位置的方块被选中(%d,%d)", this->getX(), this->getY());
  58. this->mySprite->setOpacity(135);
  59. }
  60. else {
  61. log("该位置的方块取消选中(%d,%d)", this->getX(), this->getY());
  62. this->mySprite->setOpacity(255);
  63. }
  64. this->selected = flag;
  65. }
  66. bool Block::getSelected() {
  67. return this->selected;
  68. }
  69. void Block::setMySprite(Sprite* sprite) {
  70. this->mySprite = sprite;
  71. }
  72. Block::Block() {
  73. this->myClasses = 0;
  74. this->doesErase = false;
  75. }
  76. void Block::moveToPos(int x, int y) {
  77. schedule(schedule_selector(Block::updateVisiable), 0.1f);
  78. auto moveTo = MoveTo::create(movingTime, pos[x][y]);
  79. this->posX = x;
  80. this->posY = y;
  81. auto move_ease_in = EaseBackInOut::create(moveTo->clone());
  82. this->runAction(move_ease_in);
  83. }
  84. void Block::moveByOffset(int x, int y) {
  85. schedule(schedule_selector(Block::updateVisiable), 0.1f);
  86. float offsetX = (x - posX)*blockSize;
  87. float offsetY = (y - posY)*blockSize;
  88. auto moveBy = MoveBy::create(movingTime, Vec2(offsetX,offsetY));
  89. this->posX = x;
  90. this->posY = y;
  91. auto move_ease_in = EaseBackInOut::create(moveBy->clone());
  92. this->runAction(move_ease_in);
  93. }
  94. void Block::updateVisiable(float dl) {
  95. if (this->getPositionY() <= blockSize * 7 + 150) { //当降落到Y值小于最高位置的区域时,开始显示
  96. mySprite->setVisible(true);
  97. this->unscheduleAllSelectors();
  98. }
  99. }
  100. void Block::blastAnimation() {
  101. auto fadeOut = FadeOut::create(0.1);
  102. this->mySprite->runAction(fadeOut);
  103. char blastPlistName[64]="";
  104. char blastTextureName[64] = "";
  105. sprintf(blastPlistName, "blast/b%d.plist", this->myClasses);
  106. sprintf(blastTextureName, "blast/b%d.png", this->myClasses);
  107. Animation* animation = Animation::create();
  108. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(blastPlistName, blastTextureName);
  109. char frame_name[64];//每帧图片的名称,注意图片名符号个数
  110. int i; //索引
  111. for (i = 0; i < 8; i++) {//图片个数
  112. sprintf(frame_name, "blast_%05d.png", i); //使用sprintf写入每一帧的名称至字符串中
  113. //由于我们的图片中,不足三位帧的名称为dp001,所以需要用%03d来补充前导零,表示不足3位,则高位补零
  114. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frame_name);
  115. animation->addSpriteFrame(spriteFrame);
  116. }
  117. animation->setLoops(1);//设置循环次数,,这里我们只想让精灵播放一次动画
  118. animation->setDelayPerUnit(0.03f);//设置帧与帧之间的时间间隔,我这里有8张,所以就是总共播放0.24s
  119. Animate* animate = Animate::create(animation);
  120. this->mySprite->runAction(animate);
  121. this->scheduleOnce(schedule_selector(Block::remove), 0.5f); //0.5s后再销毁,给动画播放腾出时间
  122. }
  123. Block::~Block()
  124. {
  125. }