我有一个继承自CCSprite
GameObject
的类。我还有一个名为ActionDispatcher
的单例,它继承自NSObject
。
ActionDispatcher sharedActionDispatcher's
作业是在被询问时返回CCActionInterval
,执行此操作的方法需要引用询问对象,类型为GameObject
(或其子类) )。但是,GameObject
能够提出要求,需要参考ActionDispatcher
。我在#import "GameObject.h"
标题中有一个ActionDispatcher
,在#import "ActionDispatcher"
标题中有一个GameObject
。
我在Expected ')' before 'GameObject'
方法中收到错误ActionManager
,其中引用了GameObject
。
编辑:添加评论以显示我根据下面接受的答案修正了这个问题。
//The GameObject interface
#import "cocos2d.h"
#import "ActionDispatcher.h"
@interface GameObject : CCSprite {
CGPoint homeLocation;
}
@property (readwrite) CGPoint homeLocation;
- (void)updateStateWithDeltaTime:(ccTime)deltaTime
andListOfGameObjects:(CCArray*)listOfGameObjects;
@end
//The GameObject implementation
#import "GameObject.h"
@implementation GameObject
@synthesize homeLocation;
- (void)updateStateWithDeltaTime:(ccTime)deltaTime
andListOfGameObjects:(CCArray *)listOfGameObjects
{
//CCLOG(@"updateStateWithDeltaTime method should be overriden");
}
@end
//The ActionDispatcher interface
#import "cocos2d.h"
#import "CCRotateAround.h"
#import "Constants.h"
#import "GameObject.h" // Answer: Remove this line
// Add this here: @class GameObject;
@interface ActionDispatcher : NSObject {
}
+ (ActionDispatcher *)sharedActionDispatcher;
- (id)actionWithType:(ActionType)actionType
withObject:(GameObject *)gameObject
withDuration:(float)duration;
@end
//The ActionDispatcher implementation
#import "ActionDispatcher.h"
// and add this here: #import "GameObject.h"
@implementation ActionDispatcher
static ActionDispatcher* _sharedActionDispatcher = nil;
+ (ActionDispatcher*)sharedActionDispatcher
{
@synchronized([ActionDispatcher class])
{
if(!_sharedActionDispatcher)
[[self alloc] init];
return _sharedActionDispatcher;
}
return nil;
}
+ (id)alloc
{
@synchronized ([ActionDispatcher class])
{
NSAssert(_sharedActionDispatcher == nil,
@"Attempted to allocated a second instance of the ActionManager singleton");
_sharedActionDispatcher = [super alloc];
return _sharedActionDispatcher;
}
return nil;
}
- (id)actionWithType:(ActionType)actionType
withObject:(GameObject *)gameObject
withDuration:(float)duration
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
id action = nil;
switch (actionType) {
case kActionDiveBomb:
CCLOG(@"ActionManager returning action of type: dive bomb");
CGPoint controlPoint1 = ccp(gameObject.position.x, gameObject.position.y*0.5f);
CGPoint controlPoint2 = ccp(screenSize.width*0.5f, gameObject.position.y*0.5f);
CGPoint destination = ccp(screenSize.width*0.5f, -gameObject.contentSize.height*0.5f);
ccBezierConfig diveBombBezier;
diveBombBezier.controlPoint_1 = controlPoint1;
diveBombBezier.controlPoint_2 = controlPoint2;
diveBombBezier.endPosition = destination;
id diveAction = [CCBezierTo actionWithDuration:duration bezier:diveBombBezier];
id returnToTopAction = [CCMoveTo actionWithDuration:0.0f position:ccp(gameObject.homeLocation.x, screenSize.height+gameObject.contentSize.height/2)];
id fallInAction = [CCMoveTo actionWithDuration:duration*0.2 position:gameObject.homeLocation];
action = [CCSequence actions:diveAction, returnToTopAction, fallInAction, nil];
break;
case kActionLoop:
CCLOG(@"ActionManager returning action of type: loop");
CGPoint centerPoint = ccp(screenSize.width/2, screenSize.height/2);
float span = 360.0;
action = [CCRotateAround actionWithDuration:duration centerPoint:centerPoint spanAngle:span];
break;
case kActionSpin:
CCLOG(@"ActionManager returning action of type: spin");
action = [CCRotateBy actionWithDuration:duration angle:360.0f];
break;
case kActionGoHome:
CCLOG(@"ActionManager returning action of type: go home");
action = [CCMoveTo actionWithDuration:0.0f position:[gameObject homeLocation]];
break;
case kActionFallIn:
CCLOG(@"ActionManager returning action of type: fall in");
action = [CCMoveTo actionWithDuration:duration position:[gameObject homeLocation]];
break;
case kActionIdle:
CCLOG(@"ActionManager returning action of type: idle");
action = [CCDelayTime actionWithDuration:duration];
break;
default:
CCLOG(@"ActionManager returning action of type: no valid action");
break;
}
return action;
}
@end
答案 0 :(得分:2)
只需在@class
上方的ActionDispatcher.h中添加@interface
指令即可。这是在实现中导入的类的前向声明。
@class GameObject;
答案 1 :(得分:0)
可能的。尝试插入
@class GameObject
在ActionManager.m导入后转发声明?这应该会打破这个循环。
答案 2 :(得分:-1)
这不是问题。 #import指令自动处理已包含文件的包含。 Discussion is here。 要检查它注释掉所有其他导入,从NSObject继承您的GameObject,它将编译没有错误。 该错误告诉您包含的其中一个文件有问题。有时它在@end关键字后的头文件中可能是一个错误的符号,这会使编译器在错误的地方抱怨。