【问题标题】:Cocos2d 2.0 instance method not found; reciever is a forward class未找到 Cocos2d 2.0 实例方法;接收者是前锋类
【发布时间】:2025-11-24 13:00:01
【问题描述】:

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

我的 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

还有我的 Hero.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.“接收者 'Hero' 是一个转发类,对应的 @interface 可能不是存在”

我尝试在 Hero.h 界面中添加“-(id) initWithGame:(GameLayer *)game” 行,但它不起作用。我尝试添加该行,但使用 + 而不是 -,但什么也没有。

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

【问题讨论】:

    标签: iphone ios xcode methods cocos2d-iphone


    【解决方案1】:

    GameScene.m,你应该

    #import "Hero.h"
    

    这解释了为什么您会收到“前向类”警告:由于您没有导入标头,因此编译器在 GameScene 编译单元中唯一知道的就是前向声明。

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

    【讨论】:

    • 你有一个额外的分号悬在空中:P
    • 你好。我在 GameScene.m 中添加了“#import“Hero.h”。我摆脱了错误“Receiver 'Hero' 是一个前向类,相应的@interface 可能不存在”。但是第二个错误仍然存​​在,并阻止我的精灵出现。怎么办?
    • 抱歉,我可能不够清楚:您还应该在 .h 文件中声明您的 initWithGame 方法。
    • 谢谢,所有错误都消失了。但是精灵没有出现。你能告诉我为什么吗?
    • 在我看来,您并没有将您的精灵作为孩子添加到任何地方的图层中。如果它解决了您最初的问题,请不要忘记接受我的回答。
    最近更新 更多