如何在cocos2d中为精灵或菜单执行函数?

发布于 2024-12-06 22:35:52 字数 1314 浏览 0 评论 0原文

我创建了两个CCLayer,一个是gamelayer,另一个是howlayer。 gamelayer.m 的代码是

-(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;
}

howlayer 的代码,

-(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];
    ccMenuhowmenu.position=ccp(517,384);
    [self addChild:ccMenuhowmenu z:5 tag:2];
}
return self;
}

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

我想做这样的功能:

当我单击 howlayer 上的菜单时,Howlayer 将被删除(我已经完成),然后游戏开始,调用选择器“showthegamecontent” ’,那么我该怎么办呢?

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];
    ccMenuhowmenu.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-12-13 22:35:52

对您的 howlayer 进行简单的修改:

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

但它可能会给您留下警告..但它有效..

没有警告的实现是您必须在 init 中存储对父级的引用。我觉得这是不必要的,因为你只需要参考它一次。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文