开发者

how to do a function for a sprite or menu in cocos2d?

I created two CCLayers, one is gamelayer, another is howlayer. The code of gamelayer.m is

-(id)init{
if (self = [super init]) {

    CCSprite *gamebg = [CCSprite spriteWithFile:@"bg.png"];
    gamebg.anchorPoint = CGPointZero;
    [self addChild:gamebg z:0 tag:1];

    HowLayer *howLayer = [HowLayer node];
    [self addChild:howLayer];
   [self schedule:@selector(showthegamecontent:) interval:0.4];
 }
 return self;
}

the code of howlayer is

-(id)init{
if (self=[super init]) {
    CCSprite *howbg = [CCSprite spriteWithFile:@"translucentbg.png"];
    howbg.anchorPoint = CGPointZero;
    [self addChild:howbg z:5 tag:1];

    CCMenuItem *howmenu = [CCMenuItemImage itemFromNormalImage:@"how.png"
                                                   selectedImage:@"how.png"
                                                          target:self 
                                                        selector:@selector(startgame:)];
    CCMenu *ccMenuhowmenu = [CCMenu menuWithItems:howmenu, nil];
    ccMe开发者_开发百科nuhowmenu.position=ccp(517,384);
    [self addChild:ccMenuhowmenu z:5 tag:2];
}
return self;
}

-(void)startgame:(id)sender{
 [self removeAllChildrenWithCleanup:YES];
}

I want to do function like this:

When I click the menu on howlayer, the Howlayer will be removed (I have done), and then the game starts, calls the selector 'showthegamecontent', so how should I do?


Simple hack in your howlayer:

 -(void)startgame:(id)sender{ 
 gameLayer* parent = (gameLayer*) self.parent; 
 [parent showthegamecontent];
  }

but it may leave you with a warning.. But it works..

The implementation without warning is that you have to store a reference to the parent with you init. Which i feel its unnecessary as you only need to reference it once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜