Cocos2D - 图层背景颜色无法改变

3

我想在一个视图中集成cocos2d。因此,我有一个普通的视图控制器(MapEditorViewController)和一个视图,在我的视图控制器中创建了一个(IBOutlet UIView *openGLView),我希望cocos2d在其中运行。在我的视图控制器中,我有一个名为setupCocos2D的方法:

- (void)setupCocos2D {
    CCGLView *glView = [CCGLView viewWithFrame:self.openGLView.bounds
                                   pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];
    glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.openGLView insertSubview:glView atIndex:0];
    [[CCDirector sharedDirector] setOpenGLView:glView];
    CCScene *scene = [HelloWorldLayer scene];
    [[CCDirector sharedDirector] runWithScene:scene];
}

setupCocos2D函数在MapEditorViewController类的viewDidLoad方法中被调用。

我有一个层(HelloWorldLayer),它基本上是根据http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial教程中的代码编写的。

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

- (void) addMonster {

    CCSprite * monster = [CCSprite spriteWithFile:@"backbutton.png"];

    // Determine where to spawn the monster along the Y axis
    CGSize winSize = [CCDirector sharedDirector].winSize;
    int minY = monster.contentSize.height / 2;
    int maxY = winSize.height - monster.contentSize.height/2;
    int rangeY = maxY - minY;
    int actualY = (arc4random() % rangeY) + minY;

    // Create the monster slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above
    monster.position = ccp(winSize.width + monster.contentSize.width/2, actualY);
    [self addChild:monster];

    // Determine speed of the monster
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;

    // Create the actions
    CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
                                                position:ccp(-monster.contentSize.width/2, actualY)];
    CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
        [node removeFromParentAndCleanup:YES];
    }];
    [monster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super's" return value
    if( (self=[super initWithColor:ccc4(255,0,255,255)]) ) {

        CGSize winSize = [CCDirector sharedDirector].winSize;
        CCSprite *player = [CCSprite spriteWithFile:@"carteIntrouvable.png"];
        player.position = ccp(player.contentSize.width/2, winSize.height/2);
        [self addChild:player];


        [self setIsTouchEnabled:YES];

        [self schedule:@selector(gameLogic:) interval:1.0];

            }
    return self;
}

-(void)gameLogic:(ccTime)dt {
    [self addMonster];
}

现在,我不知道我做错了什么,图层出现了,但它是黑色的,即使我在-(id) init中更改了initWithColor行。

我该如何更改图层的背景颜色呢?因为如果我不将cocos2d与UIKit集成,这段代码就可以工作...


你的初始化函数被调用了吗? - YvesLeBorg
是的,我已经验证过了。 - Etienne Noël
你把 HelloWorldLayer 改成了 CCLayerColor 的子类了吗? - bvogelzang
1个回答

6

替代方案:将CCLayerColor添加到基础层中。

-(void) onEnter
{
    [super onEnter];
    ccColor4B color = {255,255,0,255};
    CCLayerColor *colorLayer = [CCLayerColor layerWithColor:color];
    [self addChild:colorLayer z:LAST_LAYER_PLUS_1];
}

将这个函数放在onEnter方法里面...然后它就应该可以工作了...确保你放置了正确的z-ordering。 - Guru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接