新手Cocos2d-x的CCArray有关问题
新手求助Cocos2d-x的CCArray问题
我是刚刚学习C++的之前是做java.
这些天在研究cocos2d,现遇到CCArray sprites->removeObject(s);问题,实在太痛苦了,看了2天也不知道为什么老在removeObject这里报错。如果有大虾看到点拨一下,不胜感激。
开发环境 Visual Studio 2010
出错代码片段:
出错点1与出错点2,代码不能同时出现
错误信息:
update1.....................1.000609
Current life is 2
update1.....................1.000675
Current life is 1
update1.....................1.002188
Current life is 0
update1.....................1.004764
Befer Sprite retainCount:3 Sprites count:1
Remove from sprites 2
The thread 'Win32 Thread' (0xfe0) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0x102c) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0xf78) has exited with code 3 (0x3).
cocos2d: deallocing 101FAF00
The program '[5856] Test_1.win32.exe: Native' has exited with code 3 (0x3).
------解决方案--------------------
不要在CCARRAY_FOREACH中进行当前数组的删除操作,或者删除一个就立即break.
------解决方案--------------------
CCARRAY_FOREACH只是用来查询数组的
------解决方案--------------------
array,连续的一段东西
remove obj之后,这个array少了一个,后面的会往前移,可是结束条件没有变化,会运行到最后一个的下一个,那不是有错了吗。
可以这样解决,不要用CCARRAY_FOREACH这个宏,把它展开来放到上面的代码去,然后自己写个类似功能的即可。
------解决方案--------------------
你的2个出错点做的同一件事,s已经被删除了,又删除一遍,就错了
我是刚刚学习C++的之前是做java.
这些天在研究cocos2d,现遇到CCArray sprites->removeObject(s);问题,实在太痛苦了,看了2天也不知道为什么老在removeObject这里报错。如果有大虾看到点拨一下,不胜感激。
开发环境 Visual Studio 2010
出错代码片段:
出错点1与出错点2,代码不能同时出现
void HelloWorld::onUpdate(float time){
CCLog("update1.....................%f",time);
if(sprites->count()==0){
return;
}
CCObject *obj=NULL;
CCARRAY_FOREACH(sprites,obj){
MySprite *s=(MySprite*)obj;
if(s->getLife()<=0){
CCLog("Befer Sprite retainCount:%d Sprites count:%d",s->retainCount(),sprites->count());
//s->retain();
sprites->removeObject(s); //出错点1
CCLog("Remove from sprites %d",s->retainCount());
this->removeChild(s,true); //出错点2
CCLog("Remove from layer");
CCLog("After Sprite retainCount:%d Sprites count:%d",s->retainCount(),sprites->count());
}else{
s->reduceLife();
}
}
}
错误信息:
update1.....................1.000609
Current life is 2
update1.....................1.000675
Current life is 1
update1.....................1.002188
Current life is 0
update1.....................1.004764
Befer Sprite retainCount:3 Sprites count:1
Remove from sprites 2
The thread 'Win32 Thread' (0xfe0) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0x102c) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0xf78) has exited with code 3 (0x3).
cocos2d: deallocing 101FAF00
The program '[5856] Test_1.win32.exe: Native' has exited with code 3 (0x3).
------解决方案--------------------
不要在CCARRAY_FOREACH中进行当前数组的删除操作,或者删除一个就立即break.
------解决方案--------------------
CCARRAY_FOREACH只是用来查询数组的
------解决方案--------------------
array,连续的一段东西
remove obj之后,这个array少了一个,后面的会往前移,可是结束条件没有变化,会运行到最后一个的下一个,那不是有错了吗。
可以这样解决,不要用CCARRAY_FOREACH这个宏,把它展开来放到上面的代码去,然后自己写个类似功能的即可。
------解决方案--------------------
你的2个出错点做的同一件事,s已经被删除了,又删除一遍,就错了