从另一个方法访问时,body为null

时间:2013-05-22 02:40:42

标签: cocos2d-iphone box2d box2d-iphone

我有一个联系人监听器,可以处理两个box2d主体之间的联系。我正在从HelloWorldLayer中的Contacter访问主体,因为box2d建议应该保存联系主体并在时间步之后实施更改。请参阅以下代码:

Contacter.h:

#import "CCPhysicsSprite.h"

@interface Contacter : CCPhysicsSprite {

}

@property(nonatomic, assign) NSMutableArray* arrayOfBodies;
@property(nonatomic, assign) CCPhysicsSprite* spriteToDestroy;


-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite:(CCPhysicsSprite*)twoPhysicsSprite;

@end

Contacter.mm:

#import "Contacter.h"
#import "Box2D.h"

@implementation Contacter

@synthesize arrayOfBodies = _arrayOfBodies;
@synthesize spriteToDestroy = _spriteToDestroy;

-(void)destroyBodies:(b2Body*)body   {
    _arrayOfBodies = [[NSMutableArray alloc] init];
    NSValue *bodyValue = [NSValue valueWithPointer:body];
    [_arrayOfBodies addObject:bodyValue];
}

-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite:   (CCPhysicsSprite*)twoPhysicsSprite; {
int firstTag = onePhysicsSprite.tag;
int secondTag = twoPhysicsSprite.tag;

if (((firstTag == 90) && (secondTag == 101 )) || ((firstTag == 101) && (secondTag == 90))) {

    if (tag1 == 90) {
        [self destroyBodies:onePhysicsSprite.b2Body];// adds body to array to be destroyed
        spriteToDestroy = onePhysicsSprite; // taking note of sprite to be destroyed

    }
    else if (tag2 == 90)   {
        [self destroyBodies:twoPhysicsSprite.b2Body];
        spriteToDestroy = twoPhysicsSprite;
    }
}

}

在更新方法中调用HelloWorldLayer.mm中的以下方法:

-(void)removeDestroyedBodiesAndSprites  {
bodyContact = [Contacter node];    
if ([bodyContact arrayOfBodies]) {


for (NSValue* bodyValue in [bodyContact arrayOfBodies]) {
    b2Body *removeBody;
    removeBody = (b2Body*)[bodyValue pointerValue];

    world->DestroyBody(removeBody);
    removeBody = NULL;
    [self removeChild:[bodyContact spriteToDestroy]];
       }
    }
}

有接触但是精灵没有被删除,并且在removeDestroyedBodiesAndSprites中没有销毁体。在使用CCLOG测试后,我发现for循环不满意,这意味着arrayOfBodies可能为null。自联系建立以来,这是令人惊讶的。非常感谢您的协助。

已更新

以下是联系人听众:

TestContactListener.h:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "GameObjects.h"
#import "Contacter.h"


class TestContactListener : public b2ContactListener    {
public:
    Contacter* contacter;

    void BeginContact(b2Contact* contact);
};

TestContactListener.mm:

#import "TestContactListener.h"

void TestContactListener:: BeginContact(b2Contact *contact)
{
    contacter = [Contacter node];

    b2Fixture       *fixtureA = contact->GetFixtureA();
    b2Fixture       *fixtureB = contact->GetFixtureB();
    b2Body          *fixtureABody = fixtureA->GetBody();
    b2Body          *fixtureBBody = fixtureB->GetBody();

    CCPhysicsSprite* physicsSprite = (CCPhysicsSprite*)fixtureABody->GetUserData();
    CCPhysicsSprite* physicsSprite2 = (CCPhysicsSprite*)fixtureBBody->GetUserData();

    [contacter physicsSpritesContact:physicsSprite otherSprite:physicsSprite2];

}

2 个答案:

答案 0 :(得分:1)

将其从destroyBodies方法移至init方法,因此只调用一次:

_arrayOfBodies = [[NSMutableArray alloc] init];

答案 1 :(得分:0)

仔细看看你的destroyBodies方法。每次调用它时,都会创建一个新数组,替换任何现有数组。因此,您只会销毁传入该方法的最后一个主体。

如果你不使用ARC,你也会泄漏所有被替换的阵列。