展会信息港展会大全

C++ 静态static 变量在 cocos2d-x 里面使用误区
来源:互联网   发布日期:2015-09-27 14:59:22   浏览:2938次  

导读:void Cms::showMonster(CCArray* monsterArray,int type) { static int posN=0; for(int i=0;icount();i++) { auto monsterSprite=(CCSprite*)monsterArray-objectAtIndex(i); if(type==1) { monsterSprite-setPosition(ccp(640+posN*480,4*32-16)); } if(ty...

void Cms::showMonster(CCArray*monsterArray,int type)

{

static int posN=0;

for(int i=0;icount();i++)

{

automonsterSprite=(CCSprite*)monsterArray->objectAtIndex(i);

if(type==1)

{

monsterSprite->setPosition(ccp(640+posN*480,4*32-16));

}

if(type==2)

{

monsterSprite->setPosition(ccp(160+posN*640,192*2));

}

this->addChild(monsterSprite);

posN++;

}

}在上述的这段代码中,我使用了一个static 变量posN, 在游戏重新开始时,发现怪物的位置不在原来的位置了。这个问题是static静态变量造成的,,我们知道静态变量是在内存空间的静态 区域开辟的。。它有个特点,就是如果程序没有结束,尽管是场景的跳转,也无法销毁这个变量。。所以当我们再次运行这段代码是静态变量的计数不是从零开始,而是某个值;

赞助本站

人工智能实验室
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港