【问题标题】:Cocos2d Displaying a Layer IssueCocos2d 显示图层问题
【发布时间】:2011-03-28 18:46:44
【问题描述】:

我正在尝试从 applicationDidEnterBackground: 方法显示暂停游戏层,并且由于某种原因它确实调用了该方法但没有任何反应。

委托


- (void)applicationDidEnterBackground:(UIApplication*)application {
        ship = [[Ship alloc] init];
        [ship pause];

暂停方法


- (void)pause
{


    BOOL isPaused = [[CCDirector sharedDirector] isPaused];

    if(!isPaused) 
    {
        //Pause the game
        ccColor4B c = {100,100,0,100}; 
        PauseLayer *pauseLayer = [[[PauseLayer alloc] initWithColor:c] autorelease]; 

        [self.leftMenuItem setIsEnabled:NO];
        [self.rightMenuItem setIsEnabled:NO];
        [self.fireMenuItem setIsEnabled:NO];

        [self addChild:pauseLayer z:10 tag:100];
        [[CCDirector sharedDirector] pause];        
    }
}

暂停层


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


- (id)initWithColor:(ccColor4B)color
{
    if((self = [super initWithColor:color])) 
    {
        self.isTouchEnabled = YES; 
        [CCMenuItemFont setFontName:@"Marker Felt"];
        [CCMenuItemFont setFontSize:40];

        CCMenuItemFont *resumeGameItem = [CCMenuItemFont itemFromString:@"Resume" target:self selector:@selector(resumeGame)];
        CCMenuItemFont *menuGameItem = [CCMenuItemFont itemFromString:@"Menu" target:self selector:@selector(goToGameMenu)];

        CCMenu *menu = [CCMenu menuWithItems:resumeGameItem,menuGameItem,nil];
        [menu alignItemsVerticallyWithPadding:40.00];

        [self addChild:menu];
    }
    return self;
}

谢谢!

【问题讨论】:

    标签: cocos2d-iphone layer cclayer


    【解决方案1】:

    如果你在委托中初始化船,它不会被添加到我能看到的任何 cocos 层中。您必须获取对当前场景的引用并将船添加到其中(假设船是 Cocos 节点的子类)。

    【讨论】:

    • 您好,谢谢您的回答,我的所有类都继承自CCLayer,它仍然调用该方法但没有任何反应
    • 如何将船添加到 cocos 层?我在上面的代码中没有看到它。 AppDelegate 不是 cocos 节点。 Ship 必须添加到图层才能显示。
    • 使用 CCDirector 获取当前场景并将船添加到其中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多