2

我正在关注“Learn Cocos2D”,在第 4 章中我遇到了以下指令:

在 GameLayer 的 init 方法中,在 scheduleUpdate 之后添加对接下来讨论的 initSpiders 方法的调用:

-(id) init {
if ((self=[super init])) {
...
       [self scheduleUpdate];
       [self initSpiders];
   }
   return self;
}

我收到 ARC 错误消息:“GameLayer”没有可见的@interface 声明选择器“initSpiders”

我在该行收到相同的消息:self resetSpiders

我错过了什么?到那时,一切都构建并运行良好。

4

2 回答 2

0

This issue derives from the fact that the initSpiders and resetSpiders are not declared in your class interface and are defined in the .m file after the point where they are used.

If they are not missing altogether, you can fix this in either of 2 ways:

  1. move the definition of the initSpiders and resetSpiders methods above your init method and the errors will disappear;

  2. add a declaration for both methods in the @interface of the class.

(If you do both, it will also work)

Check your code to see if the implementation for those methods is available.

于 2013-01-08T15:16:15.477 回答
0

您的错误似乎是您也没有遵循本书的下一部分。完成下一部分应该允许您编译代码而不会出现这样的警告。

本书该部分的更完整摘录是:

然后在 GameSceneinit方法中添加对initSpiders接下来讨论的方法的调用,就在后面scheduleUpdate:

-(id) init  { 
    if ((self = [super init])) 
    { 
        … 96  CHAPTER 4:  Your First Game 
        [self scheduleUpdate]; 
        [self initSpiders]; 
    } 
    return self;  
}   

之后,将相当多的代码添加到GameScene类中,从 initSpiders清单 4-8 中的方法开始,它正在创建蜘蛛精灵。

清单 4-8。 为了更容易访问,蜘蛛精灵被初始化并添加到 CCArray

-(void) initSpiders 
{ 
    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
    // using a temporary spider sprite is the easiest way to get the image's size 
    CCSprite* tempSpider = [CCSprite spriteWithFile:@"spider.png"]; 
    float imageWidth = [tempSpider texture].contentSize.width; 
    // Use as many spiders as can fit next to each other over the whole screen width. 
    int numSpiders = screenSize.width / imageWidth; 
    // Initialize the spiders array using alloc. 
    spiders = [[CCArray alloc] initWithCapacity:numSpiders]; 
    for (int i = 0; i < numSpiders; i++) 
    { 
        CCSprite* spider = [CCSprite spriteWithFile:@"spider.png"]; 
        [self addChild:spider z:0 tag:2]; 
                 
        // Also add the spider to the spiders array. 
        [spiders addObject:spider]; 
    } 
    // call the method to reposition all spiders 
    [self resetSpiders]; 
}
于 2013-01-08T15:31:06.707 回答