2013-02-13 51 views
0

我GameScene.m文件:未找到Cocos2d 2.0實例方法; reciever是一個正向類

#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 

我GameScene.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 

我Hero.h文件:

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

@class GameLayer; 

@interface Hero : CCNode 
{ 
CCSprite * mySprite; 
GameLayer * theGame; 
float _lastTimeFired; 
float _fireInterval; 
float _firingSpeed; 
float _movementSpeed; 
} 

@property (nonatomic,retain) CCSprite * mySprite; 
@property (nonatomic,retain) GameLayer * theGame; 
@property (nonatomic,readwrite) float lastTimeFired; 
@property (nonatomic,readwrite) float fireInterval; 
@property (nonatomic,readwrite) float firingSpeed; 
@property (nonatomic,readwrite) float movementSpeed; 

@end 

而且我心目中的英雄。 m文件:

#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 

這裏是我的問題:我得到兩個警告 - 1.「實例方法-initWithGame找不到(返回類型默認爲'id')」和2.「Receiver'Hero'是一個前向類,相應的@interface可能不存在「

我試着向Hero.h接口添加」 - (id)initWithGame:(GameLayer *)遊戲「這一行,但它不起作用。我嘗試添加該行,但使用+而不是 - ,但沒有任何內容。

我最終沒有在顯示屏上顯示我的英雄。有誰知道如何解決這個問題(我使用最新版本的Xcode)?

回答

1

GameScene.m,你應該

#import "Hero.h" 

這就解釋了爲什麼你的「正向類」警告:因爲你沒有導入頭,已知在GameScene編譯單元的編譯器的唯一的事情就是前向聲明。

一旦你這樣做,如果你還在Hero.h中聲明initWithGame,那麼你將不會得到任何警告。

+1

你有一個額外的分號掛在空中:P – 2013-02-13 17:47:00

+0

謝謝,@ H2CO3。 – sergio 2013-02-13 17:48:45

+0

不客氣,說編譯器;-) – 2013-02-13 17:49:12

相關問題