Is there a way to set a label in a scene object from other objects or functions?

// add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hi!", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
    this->addChild(pLabel, 1); <<<<<<<<<<<<<<<<<<<<< How should this code look like if I'm trying to add a label outside of the object, from another class?

Why would you add a label as a child of one object from another object? addChild is a public method so you can easily call it for another object. Just write pointerToAnotherObject~~>addChild instead of this~~>addChild(label, 1)

Hey again!

I’m trying to use the MVC model and setting a label as a dummy test inside a method of the controller, the controller doesn’t know about the view/ scene, the only way I see this working is if I pass a reference to the controller from the scene, then using that reference to add a child with the method inside the controller, does that sound right to you?

Are you trying to access the scene class from within the controller class?
If the controller created the class in the first place then it must know the type and can therefore call any public function that exists in the scene.

I don’t think you should add to the scene from within the controller directly but call a function passing any info required and let the scene add the object.

You could have a function in the scene class like this:-

public void addLabel(string text, CCPoint position)
then from the controller call:-
Scene->addLable("some text", ccp(0.5, 0.5));

The only MVC pattern that I know is ASP.NET MVC.
In this pattern, you would not be doing anything in the controller except fetching the required info and passing it to the scene.
The controller would not have/need any access to the scene.

The scene would be creating the label based on the info passed to it from the controller.

What I’m doing right now:

The delegate creates a main scene
The scene creates relevant controllers inside it
You interact with the model or data with the controller <<<<<<<<< I’m not quite sure how to pass the main scene as an object inside a method here, I’m trying a couple of things as of typing!
The scene gets notified of any changes in the model and renders the sprites accordingly

Basically I’m trying to create my own buttons that can detect a tap, a hold and a release, and call 3 different methods with one button, I’m not sure if I’m doing things right(doesn’t seem like it):smiley:

In the delegate:

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = MainScene::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

In the scene:

bool MainScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    Button *pCloseItem = Button::create(
                                                          "CloseNormal.png",
                                                          "CloseSelected.png",
                                                          this,
                                                          menu_selector(MainScene::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
//    CCLabelTTF* pLabel = CCLabelTTF::create("Test Scene", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

//    // position the label on the center of the screen
//    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );
//    
//    // add the label as a child to this layer
//    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(size.width/2, size.height/2) );

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

    return true;
}

Trying to pass the scene by reference but failing, in the controller:

class Button : public CCMenuItemImage {
private:
    static CCScene* mScene;

In the controller .ccp, overriding inherited methods:

void Button::selected(){
    CCMenuItemImage::selected();

    // add a label shows "Selected"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Selected", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
//    if (mScene) {
        mScene->addChild(pLabel, 1);
//    }
}

void Button::unselected(){
    CCMenuItemImage::unselected();

    // add a label shows "Unselected"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Unselected", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
//    if (mScene) {
        mScene->addChild(pLabel, 1);
//    }
}

I think your problem is here:-

Button *pCloseItem = Button::create(
                                                          "CloseNormal.png",
                                                          "CloseSelected.png",
                                                          this,
                                                          menu_selector(MainScene::menuCloseCallback) );

“this” will be a CCLayer object, you are storing a CCScene object.

Wow, you’re a lifesaver, I got too caught up in the logic to notice that misunderstanding :smiley: :smiley: :smiley:

I’m currently doing the other controls because I couldn’t solve this one, I’ll revisit it again later and (hopefully) solve it with the situation cleared!