Search code examples
cocos2d-iphoneccsprite

UIButton of previous scene overlaps CCSprite


I have a UIButton that moves around randomly on the screen. On clicking on the button, a new scene is loaded that, for now, contains a CCSprite. Here is the code:

//in init
CCSprite *a = [CCSprite spriteWithFile:@"a.png"];
[a setPosition:ccp(0,0)];
[self addChild:a];

Pretty straightforward, and it stumps me why after 'replaceScene' the UIButton of theHelloWorldScene.m is still visible, right on top of the Sprite. Where am I going wrong?


Solution

  • I'm assuming since you're adding a UIButton into a cocos2d Scene you're using the openGLView something like:

    [[[CCDirector sharedDirector] openGLView] addSubView:button];

    If this is the case, then before you replace your HelloWorldScene you'll need call something similar to

    [button removeFromSuperview]

    Where button is the name of your UIButton (in both instances).

    A suggestion though would be to use a CCMenu with a CCMenuItem on your HelloWorldScene as UIKit objects don't really mesh very well with Cocos2d.