cocos2d 2.X的 碰撞检测(子弹碰到怪物,怪物消失) 我的代码老是内存错误
void HelloWorld::upadates(float dt)
{
this->addpaomo();
//循环敌人
std::vector<CCSprite*>::iterator pos;
for (pos=targets->begin();pos!=targets->end();pos++)
{
CCSprite *_targets=*pos;
//获取敌人的矩阵
CCRect _targetsrect=CCRectMake(_targets->getPosition().x-_targets->getContentSize().width/2, _targets->getPosition().y-_targets->getContentSize().height/2, _targets->getContentSize().width, _targets->getContentSize().height);
//循环子弹
std::vector<CCSprite*>::iterator pos1;
for (pos1=zidan->begin();pos1!=zidan->end();pos1++)
{
CCSprite *_zidans=*pos1;
//获取子弹的矩阵
CCRect _zidansrect=CCRectMake(_zidans->getPosition().x-_zidans->getContentSize().width/2, _zidans->getPosition().y-_zidans->getContentSize().height/2, _zidans->getContentSize().width, _zidans->getContentSize().height);
//检测碰撞子弹和敌人的碰撞
if (CCRect::CCRectIntersectsRect(_targetsrect,_zidansrect))
{
//将受到碰撞的敌人 放入toDetory容器
toDestroy->push_back(_targets);
}
}
std::vector<CCSprite*>::iterator pos2;
for (pos2=toDestroy->begin(); pos2!=toDestroy->end(); pos2++) {
CCSprite *_detory=*pos2;
this->removeChild(_detory,true);
//运行以下代码崩溃(从targets容器中清除敌人)
pos2=targets->erase(pos2);
}
}
toDestroy->clear();
}
void HelloWorld::upadates(float dt)
{
this->addpaomo();
//循环敌人
std::vector<CCSprite*>::iterator pos;
for (pos=targets->begin();pos!=targets->end();pos++)
{
CCSprite *_targets=*pos;
//获取敌人的矩阵
CCRect _targetsrect=CCRectMake(_targets->getPosition().x-_targets->getContentSize().width/2, _targets->getPosition().y-_targets->getContentSize().height/2, _targets->getContentSize().width, _targets->getContentSize().height);
//循环子弹
std::vector<CCSprite*>::iterator pos1;
for (pos1=zidan->begin();pos1!=zidan->end();pos1++)
{
CCSprite *_zidans=*pos1;
//获取子弹的矩阵
CCRect _zidansrect=CCRectMake(_zidans->getPosition().x-_zidans->getContentSize().width/2, _zidans->getPosition().y-_zidans->getContentSize().height/2, _zidans->getContentSize().width, _zidans->getContentSize().height);
//检测碰撞子弹和敌人的碰撞
if (CCRect::CCRectIntersectsRect(_targetsrect,_zidansrect))
{
//将受到碰撞的敌人 放入toDetory容器
toDestroy->push_back(_targets);
}
}
std::vector<CCSprite*>::iterator pos2;
for (pos2=toDestroy->begin(); pos2!=toDestroy->end(); pos2++) {
CCSprite *_detory=*pos2;
this->removeChild(_detory,true);
//运行以下代码崩溃(从targets容器中清除敌人)
pos2=targets->erase(pos2);
}
}
toDestroy->clear();
}
解决方案 »
- Cocos2d-x中CCScrollView如何控制触摸响应的区域??
- iphone模拟器960x640怎么开启使用
- UIView嵌套问题
- 新手求助:我注册的99美元公司型开发者账户,能在windows中VM7下安装的IOS系统里,让程序在真机上跑吗?
- 新手求教camera程序解读
- oc能在栈上创建对象么?
- 求救,NSOutputStream exc_bad_access导致 crash
- navigation直接push页面可以,但是用长按时间push后返回就出问题
- tab页的title(字)如何修改大一点
- 多线程和多进程
- iPhone向服务器端传文件
- 关于ios6.0系统上tabbar 和 navigation同时布局出现的问题,求高手解答
可能别的地方越界了?
可以先去掉其它代码,单独测下toDestroy->push_back(_targets);,targets->erase(pos2);
排除别的问题,把问题缩小,最后就能找到问题的所在了