2012-07-23 116 views
0

我知道我的問題可能很愚蠢,但我搜索了,我找不到它爲什麼不起作用。 我創建了一個CCLayerBackgroundLayer類下面的實現:cocos2d在場景中添加圖層

#import "BackgroundLayer.h" 

@implementation BackgroundLayer 

- (id)init { 
    if (self != nil) { 
     CCSprite *background = [CCSprite spriteWithFile:@"menu.png"]; 
     background.anchorPoint = ccp(0, 0); 

     [self addChild:background z:-1]; 

     NSLog(@"test"); 
    } 
    return self; 
} 

@end 

,我想它添加在主菜單中的場景,我有:

#import "MainMenuScene.h" 
#import "BackgroundLayer.h" 

@implementation MainMenuScene 

+ (id)scene { 
    CCScene *scene = [CCScene node]; 
    BackgroundLayer *backgroundLayer = [BackgroundLayer node]; 
    [scene addChild:backgroundLayer]; 
    return scene; 
} 

- (id)init { 
    self = [super init]; 
    if (self != nil) { 
    } 
    return self; 
} 

@end 

我的問題是出現NSLog測試,但後臺沒有按加載。如果我在MainMenuSceneinit方法上添加背景,它可以工作......我不應該假設該圖層以這種方式工作嗎?

回答

2

不確定是否相關,但是您在BackgroundLayer中忘記了self = [super init];

嘗試註釋掉錨點線,看看圖像是否顯示。

+0

非常感謝你! 那就是self = [] super init]。 有些時候,你是累了,初學者,你忘記了細節。 – Haris 2012-07-23 12:42:34