加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

2cocos2dx别踩白块游戏案例

发布时间:2020-12-14 19:14:31 所属栏目:百科 来源:网络整理
导读:?? 1 建立一个别踩白块的项目 dtwb(Don’ttouch white block) 2 修改 main.cpp 中的代码 3 修改 AppDelegate.cpp 中的代码 4 案例代码 Block.h #ifndef __BLOCK_H__ #define __BLOCK_H__ #include "cocos2d.h" USING_NS_CC ; class Block : public CCSprite
??

1 建立一个别踩白块的项目dtwb(Don’ttouch white block)

2 修改main.cpp中的代码

3 修改AppDelegate.cpp中的代码

4 案例代码

Block.h

#ifndef __BLOCK_H__

#define __BLOCK_H__

#include "cocos2d.h"

USING_NS_CC;

class Block :public CCSprite

{

public:

//分別表示块大小,块颜色,块中的字符串,字的大小,颜色

static Block * create(CCSize size,ccColor3B color,

CCString str,float strSize,133); font-family:新宋体; font-size:9.5pt">ccColor3B strColor);

//create方法依赖init方法,所以这里的init参数和create

//参数实际上相同的

bool init(ccColor3B strColor);

//CCArray用于存储block的信息

static CCArray * array;

//获得块对应的Array

static CCArray * getBlocksArray();

//相当于定义一个LineIndexgetLineIndex,setLineIndex方法

CC_SYNTHESIZE(int,_lineIndex,LineIndex);

//向下移动并且将下面的元素清空

void moveDownAndCleanUp();

};

#endif

Block.cpp

#include "Block.h"

#include "AppMacros.h"

//定义块的CCArray

CCArray * Block::array = NULL;

//这里的create依赖下面的init,所以create中的参数和init中的参数是一样的

Block * Block::ccColor3B strColor)

{

//如果array是空的,那么就创建一个新的

if (array == NULL)

{

array = CCArray::create();

//因为array走的不是渲染数,所以要加上retain()

array->retain();

}

Block * pRet = new Block;

if (pRet && pRet->init(size,color,str,strSize,strColor))

{

pRet->autorelease();

//将这个块儿添加到array中去

array->addObject(pRet);

}

else

{

delete pRet;

pRet = NULL;

}

return pRet;

}

bool ccColor3B strColor)

{

//注意:这里的块是一个精灵

CCSprite::init();

//设置块的大小

setContentSize(size);

//设置渲染的大小

setTextureRect(CCRectMake(0,size.width,size.height));

//设置块儿的颜色

setColor(color);

//设置块儿的锚点

setAnchorPoint(ccp(0,0));

//设置块儿中的文字,不在create里面赋值是因为默认的字体更好些

CCLabelTTF *label = CCLabelTTF::create();

//设置label的字符串

label->setString(str.getCString());

//设置字体的大小

label->setFontSize(strSize);

//设置字体的颜色

label->setColor(strColor);

//设置字的位置

label->setPosition(ccp(size.width / 2,size.height / 2));

addChild(label);

return true;

}

//取出array

getBlocksArray()

{

return array;

}

void moveDownAndCleanUp()

{

//行号减去1

_lineIndex--;

//往下走一个格

CCMoveTo * to = CCMoveTo::create(0.01,

ccp(getPositionX(),0); font-family:新宋体; font-size:9.5pt">getPositionY() - winSize.height / 4));

this->runAction(to);

//从节点中拿走

if (_lineIndex < 0)

{

//从数组中拿走,这里将引用计数减1,让它不影响渲染数的问题

array->removeObject(this);

removeFromParentAndCleanup(true);

}

}

运行结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读