Iphone 找不到Cocos2d 2.0实例方法;接受者是一个前卫阶级

Iphone 找不到Cocos2d 2.0实例方法;接受者是一个前卫阶级,iphone,ios,xcode,methods,cocos2d-iphone,Iphone,Ios,Xcode,Methods,Cocos2d Iphone,我的游戏场景.m文件: #import "GameScene.h" // Needed to obtain the Navigation Controller #import "AppDelegate.h" #pragma mark - GameScene // GameScene implementation @implementation GameScene // on "init" you need to initialize your instance -(id) init {

我的游戏场景.m文件:

#import "GameScene.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"

#pragma mark - GameScene

// GameScene implementation
@implementation GameScene

// on "init" you need to initialize your instance
-(id) init
{
self = [super init];
if (self != nil)
{
    [self addChild:[GameLayer node]];
}
return self;
}


- (void) dealloc
{
[super dealloc];
}

@end
@implementation GameLayer
@synthesize hero;

-(id) init
{
if( (self=[super init] )) 
{
    hero = [[Hero alloc] initWithGame:self];
}
return self;
}

-(void) dealloc
{
[super dealloc];
}

@end
我的游戏场景.h文件:

#import <GameKit/GameKit.h>

// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"

// GameScene
@interface GameScene : CCScene 
{
}

@end

@class Hero;

@interface GameLayer : CCLayer
{
Hero * _hero;

NSMutableArray * _bullets;
bool _playerFiring;

NSMutableArray * _enemies;
float _lastTimeEnemyLaunched;
float _enemyInterval;

int _score;
int _lives;
int _bombs;
int _level;
}

@property (nonatomic,retain) Hero * hero;
@property (nonatomic,readwrite) bool playerFiring;
@property (nonatomic,readwrite) float lastTimeEnemyLaunched;
@property (nonatomic,readwrite) float enemyInterval;
@property (nonatomic,retain) NSMutableArray * enemies;
@property (nonatomic,retain) NSMutableArray * bullets;
@property (assign,readwrite) int score;
@property (assign,readwrite) int lives;
@property (assign,readwrite) int bombs;
@property (assign,readwrite) int level;

@end
我的问题是:我收到两个警告-1。“未找到实例方法-initWithGame(返回类型默认为'id')”和2。“接收器‘Hero’是转发类,相应的@interface可能不存在”

我试图将“-(id)initWithGame:(GameLayer*)game”行添加到Hero.h界面,但它无法工作。我试图添加那一行,但用+代替-,但什么也没有


我最终没有在屏幕上显示我的英雄。有人知道如何解决这个问题吗(我使用最新版本的Xcode)?

gamesence.m
中,你应该

#import "Hero.h"
这就解释了为什么会出现“forward class”警告:由于没有导入头,编译器在游戏场景编译单元中唯一知道的就是forward声明


一旦你这样做了,如果你也在Hero.h中声明了
initWithGame
,那么你就不会得到任何警告。

你还有一个分号悬在空中:PHello。我在GameSecene.m中添加了“#import”Hero.h”。我消除了错误“Receiver'Hero'是一个转发类,相应的@interface可能不存在“。但第二个错误仍然存在,并阻止我的精灵显示。怎么办?对不起,我可能不够清楚:您还应该在.h文件中声明initWithGame方法。谢谢,所有错误都消失了。但是精灵没有出现。你能告诉我为什么吗?在我看来,你并没有在任何地方添加你的精灵作为一个孩子的图层。如果我的回答解决了你原来的问题,别忘了接受。
#import "Hero.h"

@implementation Hero

@synthesize theGame,mySprite;

-(id) initWithGame:(GameLayer *)game
{
self = [super init];
if(self != nil)
{

    // ask director for the window size
    CGSize size = [[CCDirector sharedDirector] winSize];

    self.theGame = game;
    mySprite = [CCSprite spriteWithFile:@"hero.png"];
    [theGame addChild:mySprite z:2];
    [mySprite setPosition:ccp(size.width/2,50)];

    self.lastTimeFired = 0;
    self.fireInterval = 3;
    self.firingSpeed = 10;
    self.movementSpeed = 5;

}
return self;
}

-(void) dealloc
{
[super dealloc];
}

@end
#import "Hero.h"