编写iOS / objective-c / cocos2d应用程序。事情一直很好,直到这个障碍...我应该注意到它是Objective-C ++,即。 .mm文件,为iOS 5.0模拟器编译。
基本上,我只是尝试子类化接口。错误发生在@interface行:“无法找到GameObject的接口声明”
#import <Foundation/Foundation.h>
#import "CCSprite.h"
#import "cocos2d.h"
#import "Box2D.h"
#import "GameObject.h"
@interface AvalancheBlock : GameObject
{
}
@end
这是GameObject.h的代码:
#import <UIKit/UIKit.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "b2Body.h"
#import "OhSnowGame.h"
@interface GameObject : CCNode {
@public
b2Body *body;
b2BodyDef *bodyDef;
b2FixtureDef *fixtureDef;
b2PolygonShape *polygonShape;
b2CircleShape *circleShape;
CCSprite *sprite;
int typeTag;
bool markedForDestruction;
}
@property (readwrite, assign) b2Body *body;
@property (nonatomic, assign) b2BodyDef *bodyDef;
@property (nonatomic, assign) b2FixtureDef *fixtureDef;
@property (nonatomic, assign) b2PolygonShape *polygonShape;
@property (nonatomic, assign) b2CircleShape *circleShape;
@property (nonatomic, retain) CCSprite *sprite;
@property (readwrite, assign) int typeTag;
@property (readwrite, assign) bool markedForDestruction;
@property (readonly) int type;
-(id) init;
-(void) initBox2D;
-(int) type;
@end
和GameObject.mm:
#import "GameObject.h"
@implementation GameObject
@synthesize body, bodyDef, fixtureDef, polygonShape, circleShape, sprite, type, typeTag, markedForDestruction;
-(id) init {
if( (self=[super init]) ) {
[self initBox2D];
markedForDestruction = NO;
}
return self;
}
-(void) initBox2D {
bodyDef = new b2BodyDef();
fixtureDef = new b2FixtureDef();
//Initial fixture settings
fixtureDef->density = 1.0f;
fixtureDef->friction = 0.5f;
fixtureDef->restitution = 0.3f;
bodyDef->userData = self;
}
-(int) type {
return 0;
}
@end
如果正在包含正确的文件...那么为什么会出错? 谢谢!
解决: 想出来......有一个循环引用,但我仍然需要参考。解决方案是从GameObject.h中删除#import“OhSnowGame.h”,而不是添加指令: @class OhSnowGame
每天都学习新东西。
第二次更新: 新问题,但非常相关。 我有OhSnowGame.mm,其中包含一个“AvalancheGrid.h”。 AvalancheGrid.h包括AvalancheParams.h。 在OhSnowGame中,我引用了AvalancheGrid和AvalancheParams。
编译器抛出链接器错误: “在OhSnowGame.o和AvalancheGrid.o中复制符号AvalancheParams”
但实际上似乎没有任何循环引用我。 如果我用OhSnowGame.mm替换#import“AvalancheGrid.h” @class AvalancheGrid; (还有@class AvalancheParams.h;) 然后它不允许我像我需要的那样引用那些对象的属性,即:
avParams.severity = 3; //nope
avParams->severity = 3; //nope
[avParams setSeverity:3]; //nope (with @property severity exposed and synthesized)
我不明白。如何避免链接器错误并仍然访问这些属性?