您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x常用函数:随机函数,数据显示,动画播放,CCMenuItemToggle,使用粒子

2013-11-22 14:31 489 查看
1、随机函数

方法一:

srand(time(NULL));
inline float RandomFloat(float lo, float hi)//设置随机数
{
float r =(float)(rand() & (RAND_MAX));
r/= RAND_MAX;
r = ((hi+1) - lo) * r + lo;
return r;
}


方法二:

//随机数的实现
float AppUtils::randomFloat(float low, float high)
{
//0.0-1.0
float r = rand()/(RAND_MAX+0.1);
return ((high+1) - low) * r + low;
}


2、数据的显示

游戏中断不了要显示金币等的数据

需求的图片资源是如下形式的,数字之间是等分的:



具体如下:

//其中arr用来存储切割后的单个sprite,变量ch是图片资源的名称
void numSetFunc(CCArray *arr,int num,float numOfFloat,const char*ch,int howmanyItems)
{
CCObject *obj=NULL;
CCSprite *sp;
CCArray *arrofTest=CCArray::create();
CC_SAFE_RETAIN(arrofTest);
CCSprite *sprite=CCSprite::create(ch);
//获得每个图片的宽度
int width=sprite->getContentSize().width/howmanyItems;
//获取图片资源的高度
int height=sprite->getContentSize().height;
int tt;
//num>0表示设置的是整数,否则表示设置的是百分数(比如说我们要显示79.65%)
if (num>=0)
{
if (num==0)
{
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*0,0,width,height));
sp->setAnchorPoint(ccp(0,0.5));
sp->setTag(0);
arrofTest->addObject(sp);
}
else
{
tt=num;
while(tt)
{
int x=tt%10;
//切割出我们所要的数据对应的图片
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*x,0,width,height));
sp->setAnchorPoint(ccp(0,0.5));
sp->setTag(x);
arrofTest->addObject(sp);
tt=tt/10;
}
}
//颠倒数组中存储的整数部分数据,把颠倒过来的数据存到数组arr中
for (int i=(int)arrofTest->count()-1;i>=0;i--)
{
sp=(CCSprite *)arrofTest->objectAtIndex(i);
arr->addObject(sp);
}
}
else	//设置浮点数
{
//numOfFloat=1.23001;//测试用
//我们假设要显示的百分数为79.65%
//取出整数部分
//考虑到优先级问题所以必须写成(int)(numOfFloat*100),而不是(int)numOfFloat*100
num=(int)(numOfFloat*100);//num=79;
tt=num;
//do...while();保证了当整数部分为0时,数组的整数部分可以存到图片“0”
do
{
int x=tt%10;
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*x,0,width,height));
sp->setAnchorPoint(ccp(0,0.5));
sp->setTag(x);
arrofTest->addObject(sp);
tt=tt/10;
} while (tt);

//整数部分79存到数组arrofTest中为97,显然与79颠倒了,下面我们颠倒过来
//颠倒数组中存储的整数部分数据,把颠倒过来的数据存到数组arr中
for (int i=(int)arrofTest->count()-1;i>=0;i--)
{
sp=(CCSprite *)arrofTest->objectAtIndex(i);
arr->addObject(sp);
}
//这样arr中存的是图片79,下面我们把点直接存到arr中,我的图片资源中数据是这样排列的“0123456789.%”,
//所以“.”是第10个数据,百分号是第11个
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*10,0,width,height));
sp->setAnchorPoint(ccp(0,0.5));
sp->setTag(10);
arr->addObject(sp);
//下面设置小数部分,小数部分要乘以10
//例子中要显示79.69,那么剩下的小数为0.69
//获取小数部分
float _num=numOfFloat*100-num;
//如果_num<0.0000001那么小数部分为0,直接设置为0即可
if (_num<0.0000001)
{
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*0,0,width,height));
sp->setAnchorPoint(ccp(0,0.5));
sp->setTag(10);
arr->addObject(sp);
}
else
{
//我们只取小数点后两位,并且不考虑四舍五入
for (int i=0;i<2;i++)
{
num=(int)(_num*10);
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*num,0,width,height));
sp->setAnchorPoint(ccp(0,0.5));
sp->setTag(num);
arr->addObject(sp);
_num=_num*10-num;
}
//最后添加“%”
sp=CCSprite::createWithTexture(sprite->getTexture(),CCRectMake(width*11,0,width,height));
sp->setTag(11);
sp->setAnchorPoint(ccp(0,0.5));
arr->addObject(sp);
}
}
}


<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在上述方法中,我们也可以添加别的参数来完善这个方法,比如说加入要保留的小数点位数和是否要四舍五入等,那您就根据需要再完善下吧!</span>


3、播放动画

//设置动画的第一帧
CCSprite *moving=CCSprite::create("shezhi/set_d0.png");
moving->setPosition(ccp(83,33));
//添加动画的序列帧
CCAnimation *animation=CCAnimation::create();
sprite=CCSprite::create("shezhi/set_d1.png");
animation->addSpriteFrameWithTexture(sprite->getTexture(),sprite->getTextureRect());
sprite=CCSprite::create("shezhi/set_d2.png");
animation->addSpriteFrameWithTexture(sprite->getTexture(),sprite->getTextureRect());
sprite=CCSprite::create("shezhi/set_d3.png");
animation->addSpriteFrameWithTexture(sprite->getTexture(),sprite->getTextureRect());
//设置动画切帧间隔
animation->setDelayPerUnit(0.5f);
animation->setRestoreOriginalFrame(true);
//无限循环播放
animation->setLoops(-1);

CCAnimate *animate=CCAnimate::create(animation);
//动画播放
moving->runAction(animate);

4、CCMenuItemToggle *music_toggle;

sprite=CCSprite::create("shezhi/set_on.png");
picked=CCSprite::create("shezhi/set_off.png");
CCMenuItemSprite*spriteItem1=CCMenuItemSprite::create(sprite, NULL, NULL, this, NULL );
CCMenuItemSprite*spriteItem2=CCMenuItemSprite::create(picked, NULL, NULL, this, NULL );
music_toggle = CCMenuItemToggle::createWithTarget(this,menu_selector(MenuSet::setMusic),(CCMenuItem *)spriteItem1,(CCMenuItem *)spriteItem2 ,NULL );
//添加到菜单
CCMenu *me=CCMenu::create(music_toggle,NULL);
me->setPosition(CCPointZero);
this->addChild(me);


5、添加粒子

//显示火箭动画
CCParticleSystemQuad *mSystemQuad = new CCParticleSystemQuad();
mSystemQuad->initWithFile("rocketParticals.plist");//通过粒子编辑器获得的文件
sprite=CCSprite::create("fire.png");
mSystemQuad->setTexture(sprite->getTexture());
mSystemQuad->setBlendAdditive(true);//是否混合
mSystemQuad->setPosition(ccp(70,0));
mSystemQuad->setAutoRemoveOnFinish(true);


6、boundingBoxForWorld

CCRect AppUtils::boundingBoxForWorld(CCNode* node)
{
CCRect rect = CCRectZero;
CCPoint anchorPoint = node->getAnchorPoint();
CCSize widgetSize = node->getContentSize();
CCPoint worldPosition = node->getPosition();
worldPosition.x -= widgetSize.width*anchorPoint.x;
worldPosition.y -= widgetSize.height*anchorPoint.y;
if (node!=NULL)
{
CCNode* parent = node->getParent();
while(parent!=NULL)
{
worldPosition = ccpAdd(worldPosition,parent->boundingBox().origin);
parent = parent->getParent();
}
rect.origin = worldPosition;
rect.size= widgetSize;
return rect;
}
return rect;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐