0

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

盒子精灵.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
    ...
}
4

2 回答 2

1

代替

@property (nonatomic, retain) b2Body* bod;

采用

@property (assign) b2Body *bod;

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

于 2010-12-31T01:49:17.243 回答
0

b2Body 是一个 C++ 对象,因此我必须制作自己的 getter 和 setter,并将 BoxSprite.m 重命名为 .mm 文件。

盒子精灵.h

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

@interface BoxSprite : CCSprite {
    b2Body* bod;
}

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

@end

盒子精灵.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];
    ...
}
于 2010-12-30T00:37:07.313 回答