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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:55:59+00:00 2026-06-11T02:55:59+00:00

I have 3 different classes Tree , Fruit , and Basket . Each of

  • 0

I have 3 different classes Tree, Fruit, and Basket. Each of them have their own CCSprite member that I access through getSprite( ) ( each class has its own getSprite( ) ). Also, each class has a draw( ) method which sets the CCSprite‘s texture, position, and other CCSprite related stuff. When I make an instance of these classes, I always need to addChild( ) each of these sprites in the main layer. Nothing’s actually wrong with it but I want it so that each draw( ) method would be the one handling the addChild( ) method to the main layer.

My code is like this:

// Tree.cpp
#include "Tree.h"
#include "cocos2d.h"

using namespace cocos2d;

Tree::Tree( ) {
   draw( );
}

CCSprite * Tree:getSprite( ) {
   return m_TreeSprite;
}
void Tree::draw( ) {
   m_TreeSprite = CCSprite::create( "tree.png" );
   m_TreeSprite -> setPosition ( 100, 100 );
}

/*
 * The other classes have similar structure. Will take a long long post if I write them all.
 */ 

// MainLayer.cpp
void MainLayer::drawScreenObjects( ) {
   m_Tree = Tree( );
   this -> addChild( m_Tree.getSprite( ) );

   m_Basket = Basket( );
   this -> addChild( m_Basket.getSprite( ) );
}

I want it so that the draw( ) method could look similar to this:

void Tree::draw( ) {
   m_TreeSprite = CCSprite::create( "tree.png" );
   m_TreeSprite -> setPosition( 100, 100 );
   MainLayer -> addChild( m_TreeSprite );
}

Solutions I am thinking of are

  1. Passing references to the MainLayer to every class and have a member variable hold that reference.
  2. Making all classes inherit from CCNode so that they can have children of their own.

Any other possible solutions? I just feel my current setup is dirty and unstable. Thanks in advance.

  • 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-11T02:56:00+00:00Added an answer on June 11, 2026 at 2:56 am

    I don’t really know any c++ but maybe try making a constructor with your MainLayer as a parameter then just calling addChild with the reference. You also wouldn’t need a member variable to hold the reference, you can call removeFromParentAndCleanup instead.

    You could also make a singleton object that holds a reference to the current MainLayer then you can call the singleton from anywhere, but that sounds more expensive. I would just go with your #1 solution.

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

Sidebar

Related Questions

I have two classes that represent two different database entities. Their relationship is 1:m
I have 4 different classes with a common superlass called WebService that each have
I have different values in different classes. I need to insert them in the
I have many different small classes which have a few fields each, e.g. this:
I have a need for two slightly different classes, that have the same members,
I have a page/gsp that displays 3 different classes. This means that I need
I have a header menu and try to define different CSS classes for each
I have classes that are named exactly the same across different plug-ins that I
I have multiple classes that will store similar data for different uses and I
Currently, if I have different classes containing functions etc, I would structure them all

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.