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

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静态变量造成的。,我们知道静态变量是在  内存空间的静态 区域开辟的。。它有个特点,就是假设程序没有结束,虽然是场景的跳转,也无法销毁这个变量。。所以当我们再次执行这段代码是  静态变量的计数不是从零開始,而是某个值。
原文地址:https://www.cnblogs.com/mengfanrong/p/5070764.html