Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:15:38+00:00 2026-06-13T05:15:38+00:00

Most of the tutorials that I’ve read only explain Cocos2D examples in the HelloWorld

  • 0

Most of the tutorials that I’ve read only explain Cocos2D examples in the HelloWorld class, but as I’ve started to build a simple game I need to know how to send an event to different classes, and for them to respond whenever it happens.

I have GameSceneLayer, a CCLayer class which loads in my different CCLayers of Sprites:

@implementation GameSceneLayer
+ (CCScene *)scene {
    CCScene *scene = [CCScene node]; // Create a container scene instance
    GameSceneLayer *gameLayer = [GameSceneLayer node]; // Create an instance of the current layer class
    [scene addChild:gameLayer]; // Add new layer to container scene
    return scene; // Return ready-made scene and layer in one
}
-(id)init
{
self = [super init];
if (self != nil)
{
    Background *background = [Background node];
    [self addChild:background z:0];
    Player *player = [player node];
    [self addChild:player z:1];
    MainMenu *mainMenu = [MainMenu node];
    [self addChild:mainMenu z:2];

}
return self;
}
@end

However, when my MainMenu CCLayer START sprite is touched I would like it to spawn in the PLAYER sprite from the Player CCLayer.

I’m guessing that I need a GlobalVariables.h with something like:

 #define gameStart @"0"

So when the START sprite is pressed it changes gameStart to 1, and somewhere in the PLAYER sprite there is

 if (gameStart == 1)
{
    [self addChild:PLAYER];
}

However I’m not sure how to set up the code so that the PLAYER sprite is always looking for that information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:15:39+00:00Added an answer on June 13, 2026 at 5:15 am

    You have objects (instances of classes). You want one object to communicate with another. In Objective-C, that’s called sending messages. In other languages it’s simply calling a method.

    You don’t need global variables. Instead, the receiving object MainMenu needs to send a message to (call a method on) the Player object. How do you get two objects to know each other? You could put them in a stinky, loud, overcrowded discotheque and hope for the best.

    Or you could simply let them talk to each other, but alas, they shouldn’t. Since both are siblings of GameSceneLayer, they shouldn’t hold references to each other themselves (danger of creating retain cycles unless you’re using weak references).

    But both have the same parent. So what does a good parent do when two siblings won’t talk to each other? It relays the message!

    In MainMenu, send a message to the parent GameSceneLayer:

    [(GameSceneLayer*)self.parent gameWillStart];
    

    The GameSceneLayer implements that selector, and forwards the message to any other object that should be informed about starting the game:

    -(void) gameWillStart
    {
        [player gameWillStart];
        [ingameUI gameWillStart];
        // etc.
    }
    

    And Player also implements said selector:

    -(void) gameWillStart
    {
        [self addChild:PLAYER];
    }
    

    Just one other thing: in GameSceneLayer, make player and all other objects ivars (instance variables) so that GameSceneLayer has these references readily available. The alternative would be to tag (less often used) objects and then use getChildByTag:

    PS: adding PLAYER as child looks dubious to me. If you have already created whatever node PLAYER is, you should add it right away and if necessary, set it to be invisible and/or paused while the game hasn’t started yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most of the tutorials that I've read on arrays in JavaScript (including w3schools and
I found that most tutorials use one big router. For example: https://github.com/thomasdavis/backboneboilerplate/blob/gh-pages/js/router.js Wouldn't it
Most of the tutorials around explain how to create a java applet: create a
With lots of efforts and tutorials I got xdebug/wincachegrind up and running but most
I have a navigation controller with a table view. In most tutorials I've read
I am new to Grails. I have noticed that most tutorials show queries being
Are there any good tutorials that outline proven Architecture patterns for Facebooks applications? Most
I find examples and tutorials about models and about validation. And I places that
The Java tutorials say that creating a Thread is expensive. But why exactly is
been working with symfony for a while. most tutorials describe having multiple actions in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.