2012-07-23 4 views
1

많은 box2d 본문을 만든 후 세계에 추가하면 EXEC_BAD_ACCESS 오류가 발생하여 EXEC_BAD_ACCESS 오류가 발생합니다. 본문을 만드는 방법은 다음과 같습니다.주기 내에서 Box2d 본문 만들기

충돌이 발생하는 위치
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()); 
    } 
} 

내가 왜 충돌되는 등의 단서가 없다, 누가 좀 도와주세요 수 있습니까?

답변

1

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()); 
      } 
     } 
    } 
+0

덕분에 많은 라즈도 내가, 내가 초기화에서 호출되는 한 방법 수준을 구축하고 충돌을 피하는 또 다른 방법을 발견하고, init 메소드 내부에서 크래시가 발생하지 않지만 솔루션을 사용하는 것이 더 깔끔하고 깨끗합니다. 고마워요! – Usul

1

myActor가 cocos2d 객체가 아닐 수 있습니까?

또는

당신은 레이어 (로 removeChild :에서 제거하고 나중에 액세스하려고? 이 경우 보유 수는 줄어들고 객체는 더 이상 존재하지 않게됩니다.