2013-01-08 59 views
2

我在下面的「學習的Cocos2D」和第4章中,我遇到了以下指令:爲「類」無可見@interface聲明選擇「選擇」

而在GameLayer init方法,加在調用initSpiders方法接下來討論,scheduleUpdate之後:

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

我得到和ARC錯誤消息:「GameLayer」不可見的@interface聲明選擇「initSpiders」

我得到了同樣的信息在線:self resetSpiderrs

我在想什麼?一切都建立起來並且運行良好。

+0

你寫過的'initSpiders'和'resetSpiders'但這些方法? 「下一步討論」這一行意味着你即將寫出這些方法。 –

回答

0

此問題源於initSpidersresetSpiders未在您的類接口中聲明,並且在使用它們之後的.m文件中定義。

如果他們沒有完全丟失,您可以在任何的2種方式解決這個問題:

  1. 移動的initSpidersresetSpiders以上方法你init方法和錯誤會消失的定義;

  2. 在該類的@interface中爲這兩種方法添加聲明。

(如果兩者都做,它也將工作)

檢查你的代碼,看看是否對這些方法的實施是可用的。

+0

如果使用XCode 4.3+,這不會是問題。此警告不再顯示該原因。 –

+0

@sergio - 我已經在書中包含了.h文件聲明: – brentg

+0

@brentg:您是否有可能試圖編譯得太早?我的意思是,你是否已經在書中找到了'initSpiders'和'resetSpiders'實現並將它們粘貼到你的源文件中? – sergio

0

你的錯誤似乎是你沒有遵循本書的下一部分。完成下一節應該允許你編譯你的代碼而不用像這樣的警告。

更完整的書的那部分的提取物是:

而在GameScene init方法的調用添加到下一個討論的initSpiders方法,之後scheduleUpdate:

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

之後,將一小段代碼添加到GameScene類中,從開始清單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]; 
} 
+0

有趣。我複製了一個粘貼你的代碼來定義init方法,並取代我的。我用.h文件中聲明的NSMutableArray更改了[[CCArray alloc] initWithCapacity:numSpiders]。錯誤消息爲[self initSpiders]消失,但錯誤仍然存​​在[self resetSpiders]調用。 – brentg

+0

這不是我的代碼..這是本書的下一個部分的引用。 (第二版) –

相關問題