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

发布于 2024-10-09 17:04:27 字数 1172 浏览 0 评论 0原文

我试图从 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
    ...
}

I'm trying to derive class from CCSprite to store the sprites reference to its corresponding b2Body, but I've get the following errors (comments in Code)

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

I was hoping to create the sprite and assign the body with:

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

Then access the b2Body by:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少情深 2024-10-16 17:04:27

而不是

@property (nonatomic, retain) b2Body* bod;

使用

@property (assign) b2Body *bod;

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

Instead of

@property (nonatomic, retain) b2Body* bod;

use

@property (assign) b2Body *bod;

since you're not passing an objective-c object.
The @synthesize directive will work too so you do not need to create your own getter and setter methods unless you want to do something else at the same time.

哆兒滾 2024-10-16 17:04:27

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];
    ...
}

b2Body is a C++ object, and so I have to make my own getters and setters, and rename BoxSprite.m to a .mm file.

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

Create:

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

Access:

if ([node isKindOfClass:[BoxSprite class]]) {
    BoxSprite *spr = (BoxSprite*)node;
    b2Body *body = [spr getBod];
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文