The below code works great when I'm calling the void in the same .m file but I want my selector to go to a 2nd layer in my scene for its 'MoveUpSelected' void (which contains my actions for the sprites movement). How can I do this?
HUDLayer.m Button code in my Layer to communicate with other layer
self.dpad = [CCSprite spriteWithFile:@"dpad.png"];
CCSprite *dpadSelectedSprite = [CCSprite spriteWithTexture:[dpad texture]];
dpadSelectedSprite.color = ccGRAY;
//float dpadHeight = flareSprite.texture.contentSize.height;
CCMenuItemSprite *dpadButtons = [CCMenuItemSprite itemWithNormalSprite:dpad selectedSprite:dpadSelectedSprite target:Level1 selector:@selector(MoveUpSelected)];
dpadButtons.position = CGPointMake(size.width / 2, 150);
[menu addChild:dpadButtons];
Level1.m Void in my 2nd layer waiting to be called by 1st layer button
- (void)MoveUpSelected {
int yPosition = self.Player.position.y;
yPosition += [self.Player texture].contentSize.height/2;
CGSize size = [[CCDirector sharedDirector] winSize];
if (yPosition >= (size.height - [self.Player texture].contentSize.height/2)) {
yPosition = (size.height - [self.Player texture].contentSize.height/2);
}
self.Player.position = CGPointMake(self.Player.position.x, yPosition);
}
I have a GameScene1.m holding both layers in separate files.
+(id) scene
{
CCScene *scene = [CCScene node];
HudLayer *HUD = [HudLayer node];
[scene addChild:HUD z:2];
Level1 *layer = [Level1 node];
[scene addChild:layer];
return scene;
}
Please explain with lines of code.
It is VERY hard to understand what do you want to do. As Stephen mentioned in his comment, "using a void" and "void action" phrases have no sense. Anyway, if you just want to call some instance's method on btn click, just set it as target of your menu item. In your case
CCMenuItemSprite *dpadButtons = [CCMenuItemSprite itemWithNormalSprite:dpad selectedSprite:dpadSelectedSprite target:self selector:@selector(MoveUpSelected)];
uses self
as target. Change it to self.parent
or any other instance and menu item will try to call selector on this target.
EDIT:
CCMenuItemSprite *dpadButtons = [CCMenuItemSprite itemWithNormalSprite:dpad selectedSprite:dpadSelectedSprite target:levelInstance selector:@selector(MoveUpSelected)];
I your edits you tried to use class object, not it's instance as target. So it cannot find static methods.