Cocos2d iphone 在for循环内创建Box2d实体
我有一个问题,在创建了许多box2d实体并将它们添加到世界中后,我的应用程序在迭代世界中的实体时出现EXEC_BAD_访问错误,导致应用程序崩溃,以下是我创建实体的方法:Cocos2d iphone 在for循环内创建Box2d实体,cocos2d-iphone,box2d-iphone,Cocos2d Iphone,Box2d Iphone,我有一个问题,在创建了许多box2d实体并将它们添加到世界中后,我的应用程序在迭代世界中的实体时出现EXEC_BAD_访问错误,导致应用程序崩溃,以下是我创建实体的方法: for(CXMLElement *node in obstaclesArr) { b2BodyDef nObstacleBody; b2Body *obsBody; CCSprite *obstacle; obstacle = [CCSprite spriteWithFile:[NSStrin
for(CXMLElement *node in obstaclesArr)
{
b2BodyDef nObstacleBody;
b2Body *obsBody;
CCSprite *obstacle;
obstacle = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@.png",[[node attributeForName:@"body"]stringValue]]];
NSString *strX = [[node attributeForName:@"x"]stringValue];
NSString *strY = [[node attributeForName:@"y"]stringValue];
float x;
float y;
x = [strX floatValue];
y = [strY floatValue];
obstacle.tag = E;
obstacle.position = ccp(x,y);
[self addChild:obstacle z:9];
nObstacleBody.type = b2_staticBody;
nObstacleBody.position.Set(x/PTM_RATIO, y/PTM_RATIO);
nObstacleBody.userData = obstacle;
obsBody = world->CreateBody(&nObstacleBody);
[[GB2ShapeCache sharedShapeCache]addFixturesToBody:obsBody forShapeName:[[node attributeForName:@"body"]stringValue]];
[obstacle setAnchorPoint:[[GB2ShapeCache sharedShapeCache]anchorPointForShape:[[node attributeForName:@"body"]stringValue]]];
}
这里是坠机发生的地方:
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);//<-- crashes in this line
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
for(b2Body*b=world->GetBodyList();b;b=b->GetNext())
{
如果(b->GetUserData()!=NULL){
//将AtlasSprites的位置和旋转与相应的实体同步
CCSprite*myActor=(CCSprite*)b->GetUserData();
myActor.position=CGPointMake(b->GetPosition().x*PTM_比率,b->GetPosition().y*PTM_比率);//GetAngle();
}
}
我不知道为什么会崩溃,有人能帮我吗?可能是你的myActor不是cocos2d对象 或
您正在从层(removeChild:)中删除,然后尝试访问?在这种情况下,保留计数将减少,对象将不再存在。您可以通过检查b2Body中的Userdata类型来避免崩溃
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
if(myActor && [myActor isKindOfClass:[CCSprite class]] )
{
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);//<-- crashes in this line
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
for(b2Body*b=world->GetBodyList();b;b=b->GetNext())
{
如果(b->GetUserData()!=NULL){
//将AtlasSprites的位置和旋转与相应的实体同步
CCSprite*myActor=(CCSprite*)b->GetUserData();
if(myActor&[myActor是类的种类:[CCSprite类]])
{
myActor.position=CGPointMake(b->GetPosition().x*PTM_比率,b->GetPosition().y*PTM_比率);//GetAngle();
}
}
}
非常感谢Raj,我还找到了另一种避免崩溃的方法,我在init中调用的方法中构建了级别,如果我去掉该方法并在init方法中构建了级别,崩溃就不会发生,但是使用您的解决方案会更好更干净。谢谢!