XCode中的“b2Body之前的预期说明符 - 量词列表”错误

时间:2010-12-29 09:01:59

标签: cocos2d-iphone box2d-iphone

我正在尝试从CCSprite派生类来将精灵引用存储到其对应的b2Body,但我得到以下错误(代码中的注释)

BoxSprite.h

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

@interface BoxSprite : CCSprite {
    b2Body* bod; // Expected specifier-quantifier-list before b2Body
}

@property (nonatomic, retain) b2Body* bod; // Expected specifier-quantifier-list before b2Body

@end // Property 'bod' with 'retain' attribute must be of object type

BoxSprite.m

#import "BoxSprite.h"

@implementation BoxSprite

@synthesize bod; // No declaration of property 'bod' found in the interface

- (void) dealloc
{
    [bod release]; // 'bod' undeclared
    [super dealloc];
}

@end

我希望创建精灵并为身体分配:

BoxSprite *sprite = [BoxSprite spriteWithBatchNode:batch rect:CGRectMake(32 * idx,32 * idy,32,32)];
...
sprite->bod = body; // Instance variable 'bod' is declared protected

然后通过以下方式访问b2Body:

if ([node isKindOfClass:[BoxSprite class]]) {
    BoxSprite *spr = (BoxSprite*)node;
    b2Body *body = spr->bod; // Instance variable 'bod' is declared protected
    ...
}

2 个答案:

答案 0 :(得分:1)

而不是

@property (nonatomic, retain) b2Body* bod;

使用

@property (assign) b2Body *bod;

因为你没有传递一个objective-c对象。 @synthesize指令也可以工作,因此您不需要创建自己的getter和setter方法,除非您想要同时执行其他操作。

答案 1 :(得分:0)

b2Body是一个C ++对象,所以我必须创建自己的getter和setter,并将BoxSprite.m重命名为.mm文件。

BoxSprite.h

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

@interface BoxSprite : CCSprite {
    b2Body* bod;
}

-(b2Body*) getBod;
-(void) setBod:(b2Body *)b;

@end

BoxSprite.mm

#import "BoxSprite.h"

@implementation BoxSprite

-(b2Body*) getBod {
    return bod;
}

-(void) setBod:(b2Body *)b {
    bod = b;
}

@end

创建:

BoxSprite *sprite = [BoxSprite spriteWithBatchNode:batch rect:CGRectMake(32 * idx,32 * idy,32,32)];
...
[sprite setBod:body];

访问:

if ([node isKindOfClass:[BoxSprite class]]) {
    BoxSprite *spr = (BoxSprite*)node;
    b2Body *body = [spr getBod];
    ...
}