C++ 静态static 变量在 cocos2d-x 里面使用误区

2015-01-27 10:18:43 · 作者: · 浏览: 13
void Cms::showMonster(CCArray*  monsterArray,int type)
{
	
	    static int posN=0;
	    for(int i=0;i
  
   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静态变量造成的,,我们知道静态变量是在 内存空间的静态 区域开辟的。。它有个特点,就是如果程序没有结束,尽管是场景的跳转,也无法销毁这个变量。。所以当我们再次运行这段代码是 静态变量的计数不是从零开始,而是某个值;