Block.h
#pragma once#ifdef WIN32#pragma execution_character_set("utf-8")#include "cocos2d.h"#include "ui/CocosGUI.h"#include "SimpleAudioEngine.h"#include <string.h>#include <time.h>#include "cocos-ext.h"using namespace cocos2d::ui;using namespace CocosDenshion;USING_NS_CC_EXT;USING_NS_CC;class Block:public Sprite{private: int myClasses; //方块的分类:0,1,2,3..等分类 float blockSize = 70.0f; //每个方块的默认大小 bool doesErase = false;//是否会被清除的记号 Sprite* mySprite; int posX; //游戏中的位置标记,从左下角开始 int posY; //游戏中的位置标记,从左下角开始 Vec2 pos[9][20]; //保存着每一个坐标对应的具体Vec2位置 float movingTime = 0.25f; //移动时间 bool selected = false;//是否被选中的记号public: Block(int myClasses, int x, int y); Block(); static Block* Block::create(const std::string& filename, int myClasses, int x, int y); void setMySprite(Sprite* sprite); void setDoesErase(bool flag); //设置当前的方块用不用被标记删除 void setSelected(bool flag); int getClasses(); bool getDoesErase(); bool getSelected(); int getX(); int getY(); Sprite* getMySprite(); void moveToPos(int x, int y); void moveByOffset(int x, int y); void remove(float dl); void updateVisiable(float dl); //实时更新方块可见性 void blastAnimation(); ~Block();};#endif
Block.cpp
#include "Block.h"Block::Block(int myClasses, int x, int y) { this->myClasses = myClasses; this->doesErase = false; //log("我的分类是:%d", this->myClasses); this->posX = x; this->posY = y; for (int i = 0; i < 9; i++) for (int j = 0; j < 20; j++) this->pos[i][j] = Vec2(blockSize * i, blockSize * j); //初始化坐标信息 //this->pos[i][j] = Vec2(blockSize * i - 40, blockSize * j + 150); //初始化坐标信息}int Block::getX() { return this->posX;}int Block::getY() { return this->posY;}int Block::getClasses() { return this->myClasses;}bool Block::getDoesErase() { return this->doesErase;}void Block::setDoesErase(bool flag) { this->doesErase = flag;}Sprite* Block::getMySprite() { return this->mySprite;}void Block::remove(float dl) { log("我将要被删除,我的编号为:%d", myClasses); this->removeFromParent();}Block* Block::create(const std::string& filename, int myClasses, int x, int y){ Sprite *sprite = new (std::nothrow) Sprite(); Block *block = new Block(myClasses, x, y); if (sprite && sprite->initWithFile(filename)) { sprite->autorelease(); //sprite->setAnchorPoint(Point(0, 0)); sprite->setPosition(Vec2(sprite->getContentSize().width / 2, sprite->getContentSize().width / 2)); block->setMySprite(sprite); //设置本方块中的精灵图片 block->setContentSize(sprite->getContentSize()); //统一方块的大小 if (y > 7) { //如果高过游戏区域,设置为不可见 sprite->setVisible(false); } block->addChild(sprite); //本方块挂载精灵 return block; } CC_SAFE_DELETE(block); return nullptr;}void Block::setSelected(bool flag) { if (flag) { log("该位置的方块被选中(%d,%d)", this->getX(), this->getY()); this->mySprite->setOpacity(135); } else { log("该位置的方块取消选中(%d,%d)", this->getX(), this->getY()); this->mySprite->setOpacity(255); } this->selected = flag;}bool Block::getSelected() { return this->selected;}void Block::setMySprite(Sprite* sprite) { this->mySprite = sprite;}Block::Block() { this->myClasses = 0; this->doesErase = false;}void Block::moveToPos(int x, int y) { schedule(schedule_selector(Block::updateVisiable), 0.1f); auto moveTo = MoveTo::create(movingTime, pos[x][y]); this->posX = x; this->posY = y; auto move_ease_in = EaseBackInOut::create(moveTo->clone()); this->runAction(move_ease_in);}void Block::moveByOffset(int x, int y) { schedule(schedule_selector(Block::updateVisiable), 0.1f); float offsetX = (x - posX)*blockSize; float offsetY = (y - posY)*blockSize; auto moveBy = MoveBy::create(movingTime, Vec2(offsetX,offsetY)); this->posX = x; this->posY = y; auto move_ease_in = EaseBackInOut::create(moveBy->clone()); this->runAction(move_ease_in);}void Block::updateVisiable(float dl) { if (this->getPositionY() <= blockSize * 7 + 150) { //当降落到Y值小于最高位置的区域时,开始显示 mySprite->setVisible(true); this->unscheduleAllSelectors(); }}void Block::blastAnimation() { auto fadeOut = FadeOut::create(0.1); this->mySprite->runAction(fadeOut); char blastPlistName[64]=""; char blastTextureName[64] = ""; sprintf(blastPlistName, "blast/b%d.plist", this->myClasses); sprintf(blastTextureName, "blast/b%d.png", this->myClasses); Animation* animation = Animation::create(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile(blastPlistName, blastTextureName); char frame_name[64];//每帧图片的名称,注意图片名符号个数 int i; //索引 for (i = 0; i < 8; i++) {//图片个数 sprintf(frame_name, "blast_%05d.png", i); //使用sprintf写入每一帧的名称至字符串中 //由于我们的图片中,不足三位帧的名称为dp001,所以需要用%03d来补充前导零,表示不足3位,则高位补零 SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frame_name); animation->addSpriteFrame(spriteFrame); } animation->setLoops(1);//设置循环次数,,这里我们只想让精灵播放一次动画 animation->setDelayPerUnit(0.03f);//设置帧与帧之间的时间间隔,我这里有8张,所以就是总共播放0.24s Animate* animate = Animate::create(animation); this->mySprite->runAction(animate); this->scheduleOnce(schedule_selector(Block::remove), 0.5f); //0.5s后再销毁,给动画播放腾出时间}Block::~Block(){}