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 7604163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:50:24+00:00 2026-05-30T23:50:24+00:00

I was playing around with a Joystick moving a Sprite around in one layer.

  • 0

I was playing around with a Joystick moving a Sprite around in one layer. Now, according to best practice, the Joystick and the Sprite have to be in different Layers of the same scene. I have managed to separate these, yet I am now completely stuck having absolutely no clue whatsoever how to pass joystick commands from one layer to another? What is the recommended way to do this?

Scene

  • Game Play Layer
    • Sprite
  • Game Control Layer
    • Joystick

When the joystick is manipulated I need to pass this info to the GamePlayLayer to control the sprite.

  • 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-05-30T23:50:26+00:00Added an answer on May 30, 2026 at 11:50 pm

    Well, I got a great Cocos2d book, written by Rod Strougo and Ray Wenderlich, by the name “Learning Cocos2d”. In their book, they implement a game, which has a joystick implemented and all, using your initial setup. The GamePlayLayer contains both the joyStick and the hero sprite. (See book page 40).

    I don’t believe they would use bad practices in their book, given they are very talented!

    …

    With that being said, I have possible solutions, if you wish to implement them on separate layers:

    GameScene
    |
    |__>GameLayer
    |
    |__>ControlLayer
    

    That’s your basic setup. But, intuitively, what is the purpose of the control layer? Control the game layer’s content! So, I would suggest you hold a (weak) reference to the GameLayer within the ControlLayer. That way, using a simple:

    @property (nonatomic, assign) CCSprite* hero;
    

    you now have access to the hero from the ControlLayer!

    Extra (if needed):

    //GameScene init:
    - (id)init {
        ....
        gameLayer = [GameLayer node];
        controlLayer = [ControlLayer node];
        [controlLayer setGameLayerRef:gameLayer];
        ...
    }
    
    // Control layer:
    @property (nonatomic, assign) GameLayer* gameLayerRef;
    

    Even though I just suggested that way, I don’t use it in my games 🙂

    What I normally do is:

    Make the GameScene class a “Semi-Singleton”. (I learned this method from “Learn iPhone and iPad Game Dev” By Itterheim (aka gaming horror, Kobold2d publisher … etc).

    Then, inside the control layer, I would call the GameScene object:

    [[GameScene sharedScene] doSomethingToTheGameLayer];
    

    Yeah, the gameScene has simplistic methods that just relies what the control need to update in the game layer.


    Edit:

    Implementing the Semi-singleton pattern, as described by Itterheim in his book.

    But, what is semi-singleton?

    It has the singleton pattern’s property: you can access the object instance from anywhere using a static call.

    [GameScene sharedScene];
    

    However, singleton objects are usually retained, after being created for the first time, till the end of the application’s life. In the Semi-singleton pattern, this is not the case.

    Once you create the instance, you cannot create another instance before destroying the old one, BUT once you are done with the instance, you destroy it (dealloc). Creating another one when necessary.

    Recap:
    Semi-Singleton: Create many object from it, but only one at any given time. Only recreate after destroying the old.

    Implementation:

    Of course, as you do with any singleton class, you first declare a static variable of the same type of the class:

    //In GameScene.m
    static GameScene* instance = nil;
    
    @implementation
    
    //Here we have the static method to access the instance:
    +(GameScene*)sharedScene {
        //you must have created one before trying to access it "Globally".
        ///Typically, created where you transition the GameScene using CCDirector's replaceScene.
        NSAssert(instance != nil, @"GameScene not instantiated!!");
        return instance;
    }
    
    -(id)init {
        if((self = [super init])) {
            //Make sure you don't have another instance of the class created
            //NOTE: Possible race condition that I wouldn't worry about, since it should never happen.
            NSAssert(instance == nil, @"Another instance is already created!!");
            instance = self;
    
            ...
        }
        return self;
    }
    
    //Don't forget!! Dealloc!!
    - (void)dealloc {
        //the only instance we had is dealloc'd! We are back to zero. Can create a new one later! :D
        instance = nil;
        [super dealloc];
    }
    

    Edit2:

    So, the timeline:

    CCScene* scene = [GameScene node];
    [[CCDirector sharedDirector] replaceScene:scene];
    ...
    [[GameScene sharedScene] doSomething];
    ...
    [[CCDirector sharedDirector] replaceScene:otherScene];
    //After the new scene replaces GameScene, dealloc will be called, implicitly. Making instance = nil;
    instance = nil;
    [super dealloc];
    ...
    //Loop again
    CCScene* scene = [GameScene node];
    [[CCDirector sharedDirector] replaceScene:scene];
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just playing around with the now released Silverlight 2.0. I'm trying to put a
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
Just playing around with Rails 3.1 at the moment, and one issue I'm having
I have been playing around with various methods of making the Visitor pattern in
I've been playing around with Simple.Data and have run across something that I can't
I'm just playing around with some code but it seemed to have the specific
Playing around with Google Maps these days, with some directions. I have a map
I am playing around with the AVAudioRecorder so I can have it in my
Playing around in order to learn XSLT, I have the following XML file and
Playing around with MSVC++ 2005, I noticed that if the same class is defined

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.