How can I get my scheduler to call a method inside another implementation files ccLayer?
Here are the files I have:
GameHUD.h
GameHUD.m
GameScene.h
GameScene.m
Scene setup under GameScene.m
+(id) scene
{
CCScene *scene = [CCScene node];
GameScene *GameLayer = [GameScene node];
[scene addChild:GameLayer];
GameHUD *HUDLayer = [GameHUD node];
[scene addChild:HUDLayer z:2];
return scene;
}
My Scheduler inside GameHUD.m
[self schedule:@selector(movePlayerUp)];
The method I would like to call in GameScene.m
-(void) movePlayerUp {
Player.position = ccp(Player.position.x, Player.position.y + 1);
Player.rotation = 0;
}
I'm still a few months new into Cocos2D and Kobold2D. I know I need to start by changing my 'self' to 'GameLayer' but apart from that I need some more help. Thanks.
Save a pointer to the GameScene in GameHUD, like this:
Add an @property to GameHUD.h
@property (assign) GameScene *game;
Now set it on your scene method
+(id) scene
{
CCScene *scene = [CCScene node];
GameScene *GameLayer = [GameScene node];
[scene addChild:GameLayer];
GameHUD *HUDLayer = [GameHUD node];
HUDLayer.game = GameLayer;
[scene addChild:HUDLayer z:2];
return scene;
}
and use the property to call the scheduler:
[game schedule:@selector(movePlayerUp)];
you can check if game is nil to avoid errors too:
if(game!=nil)
{
[game schedule:@selector(movePlayerUp)];
}