遇到了两个不是很难但却有点儿类似于“找茬”的bug
1个是添加到 batch 中的 sprite 调用 removeFromParentAndCleanUp 方法之后,竟然没有被顺利移除出去的bug
蛋疼了好一阵,真的可以说是百思不得其解,还以为有时 cocos2d 冒出了什么新的 bug,
但我总是被事实打击到,不过我也习惯了,貌似每次都是我自己的问题,哈哈~
贴张图先:
右下角的那只小鱼没有如我所愿的被移除出去,小鱼所依附的 fixture,body已经被正常销毁掉了~
解决方案:
仔细查看代码,看是否是因为将某个 sprite 加入 batchNode 的时候添加了两次
值得注意的是,并非所有被加入过两次的 sprite 再被移除的时候都能留下点儿什么~
另外一个bug是因为我对代码做肆意的位置调整所导致的,如下:
/** Iterate over the bodies in the physics world */
for (b2Body* b =_world->GetBodyList(); b; b = b->GetNext()) {
if(b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding(相应的) body
CCSprite *actor = (CCSprite*)b->GetUserData();
CGPoint po = CGPointMake(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO);
if(![MGameScene isPositionOutOfBounds:po]) { //如果没有越界的话,实时更新~
actor.position = po;
actor.rotation = -1 *CC_RADIANS_TO_DEGREES(b->GetAngle());
if(actor.tag == TAG_STATE_PIRATE_DEAD) {
[actorremoveFromParentAndCleanup:YES];
[auaddAnimSpriteAtPosi:@"pirateArr"start:0 end:8 delay:0.05foneOff:YES posi:actor.rotation];
b->SetUserData(NULL);
b->SetTransform(b2Vec2(7.5f,20.0f), 0.0f);
b->SetType(b2_staticBody);
[[AudioManagergetInstance] musicBalloonExplode];
}
} else {
[actorremoveFromParentAndCleanup:YES];
b->SetUserData(NULL);
b->SetTransform(b2Vec2(7.5f,20.0f), 0.0f); // 设置一个合理的位置储存这些用处已经不大的body们~
b->SetType(b2_staticBody);
}
}
}
之前我是这么写的,上面只是简单的将两句代码调换了一下位置~/** Iterate over the bodies in the physics world */
for (b2Body* b =_world->GetBodyList(); b; b = b->GetNext()) {
if(b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding(相应的) body
CCSprite *actor = (CCSprite*)b->GetUserData();
CGPoint po = CGPointMake(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO);
if(![MGameScene isPositionOutOfBounds:po]) { //如果没有越界的话,实时更新~
actor.position = po;
actor.rotation = -1 *CC_RADIANS_TO_DEGREES(b->GetAngle());
if(actor.tag == TAG_STATE_PIRATE_DEAD) {
[auaddAnimSpriteAtPosi:@"pirateArr"start:0 end:8 delay:0.05foneOff:YES posi:actor.position];
[actorremoveFromParentAndCleanup:YES];
b->SetUserData(NULL);
b->SetTransform(b2Vec2(7.5f,20.0f), 0.0f);
b->SetType(b2_staticBody);
[[AudioManagergetInstance] musicBalloonExplode];
}
} else {
[actorremoveFromParentAndCleanup:YES];
b->SetUserData(NULL);
b->SetTransform(b2Vec2(7.5f,20.0f), 0.0f); // 设置一个合理的位置储存这些用处已经不大的body们~
b->SetType(b2_staticBody);
}
}
}
显而易见,第一段代码里面,我在取 actor.position 的时候,actor 已经被销毁掉了~因此便报出一大段很难让人确定到具体出错位置的控制台输出~
现在有点儿不再状态了,遇到了bug感觉脑袋里面就是轰的一声,像爆了一颗手榴弹
想去年刚开始弄得时候,被 EXC_BAD_ACCESS 是各种虐啊,那时候倒是越挫越勇~
一句话,心态很重要! 还是慢慢来吧,归根节点还是因为我自己有点儿过于心急了~