void Cms::showMonster(CCArray* monsterArray,int type) { <span style="color:#ff0000;">static int posN=0; </span> for(int i=0;i<monsterArray->count();i++) { auto monsterSprite=(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静态变量造成的,,我们知道静态变量是在 内存空间的静态 区域开辟的。。它有个特点,就是如果程序没有结束,尽管是场景的跳转,也无法销毁这个变量。。所以当我们再次运行这段代码是 静态变量的计数不是从零开始,而是某个值;
C++ 静态static 变量在 cocos2d-x 里面使用误区
原文:http://blog.csdn.net/u010296979/article/details/40706229