由于游戏非常多使用阻断,因此,我们创建了一个单独的类中Block。
于Blcok.h声明了两个初始化函数:
static Block* createWithArgs(Color3B color, Size size, std::string label, float fontSize, Color4B textColor); //初始化參数:方块颜色、方块大小、字、字体大小、字体颜色 virtual bool initWithArgs(Color3B color, Size size, std::string label, float fontSize, Color4B textColor);
由于方块有涉及到颜色、大小、字、字体大小、字体颜色,所以我们把这些都写进了參数里面。
用Vector来存放block,便于管理
//用来存放block static Vector<Block*> * blocks;
在cpp中文件来详细实现这两个函数以及blocks:
//初始化blocks
Vector<Block*> * Block::blocks = new Vector<Block*>();
Block* Block::createWithArgs(Color3B color, Size size, std::string label, float fontSize, Color4B textColor)
{
auto b = new Block();
b->initWithArgs(color, size, label, fontSize, textColor);
b->autorelease();
//将b加入到blocks中
blocks->pushBack(b);
return b;
}
//初始化參数:方块颜色、方块大小、字、字体大小、字体颜色
bool Block::initWithArgs(Color3B color, Size size, std::string label, float fontSize, Color4B textColor)
{
Sprite::init();
//设置大小
setContentSize(size);
//设置锚点为左下角
setAnchorPoint(Point::ZERO);
//设置区域
setTextureRect(Rect(0, 0, size.width, size.height));
//设置颜色
setColor(color);
//设置文字
auto myLabel = Label::create();
myLabel->setString(label);
myLabel->setSystemFontSize(fontSize);
myLabel->setTextColor(textColor);
addChild(myLabel);
myLabel->setPosition(size.width/2, size.height/2);
return true;
}
另外还需一个函数来移除Vector中的block
//从Vector中移除blocks void Block::removeBlock() { removeFromParent(); blocks->eraseObject(this); }
版权声明:本文博主原创文章,博客,未经同意不得转载。