Search code examples
ioscocos2d-iphone

Accessing method from another class in Cocos2D


I am trying to access my custom method from another class in Cocos2D. This is something I have done before out with Cocos2D but for some reason it is not working now.

heroNode.h (CCNode)

@interface heroNode()

    -(void) staminaCounter;

@end

#import "heroNode.h"

@implementation heroNode {
}
        -(void)staminaCounter {
        //My Code Here

    }
@end;

MainScene.h (CCScene)

#import "heroNode.h"

@implementation MainScene
{
    heroNode *HeroStuff;
}

//Using
    HeroStuff = [[heroNode alloc] init];

//Then this is where I am having the issue calling `staminaCounter`
//Do something like
//[heroStuff staminaCounter];

Solution

  • Declare -(void)staminaCounter in "heroNode.h", because if you don't do it, the method is private, you can only use it from inside your class.

    Between @interface and @end