1

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.

Troy R
  • 426
  • 2
  • 16
  • 3
    What do you mean by "using a void" or "void action"? Neither of that makes sense. Did you mean "calling a method whose return value is void" or perhaps "calling a method that takes no parameters"? I'm guessing you want to know how to get a reference to some other node, in that case read this: http://www.koboldtouch.com/x/NAAp PS: next time don't waste weeks (!) trying to get unstuck. You won't get anywhere in programming without asking for help the instant you notice that you're not getting anywhere with a problem. ;) – CodeSmile Apr 05 '13 at 08:18
  • I've tried to understand any of these strategies and they are hard to learn as a beginner. – Troy R Apr 05 '13 at 08:27
  • I think he just wants to call a void method from the layer's parent layer/scene. But indeed this is very unclear. – giorashc Apr 05 '13 at 08:41
  • I'm just trying to get 1 layer to get a selector to sent to another layer that owns the void with actions. Everything I read isn't working. – Troy R Apr 05 '13 at 08:45
  • I'm trying to get my HUD layer to tell my Level layer to move player via CCMenuItemSprite – Troy R Apr 05 '13 at 08:55
  • I have fixed by question – Troy R Apr 05 '13 at 09:26

1 Answers1

0

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.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • By using the target to 'target' my other layer I'm getting the following error Undeclared selector 'MoveUpSelected' even though I have a - (void)MoveUpSelected {} in my target layer. – Troy R Apr 05 '13 at 09:17
  • did you declare it in header file of that layer class? – Kreiri Apr 05 '13 at 09:31
  • just declared in header (Code: - (void)MoveUpSelected;) and used the following void to test but still nothing (Code: - (void)MoveUpSelected { NSLog(@"UpSceneButton"); }) – Troy R Apr 05 '13 at 09:39
  • Instead of using target:self.parent is there anyway to have the target point to a cclayer. Eg: Level1 in Level1.m – Troy R Apr 05 '13 at 09:59
  • How do I implements a instance instead of class object? – Troy R Apr 05 '13 at 11:04
  • in your line `Level1* layer = [Level1 node]` `Level1` is class and `layer` is the instance. so you have to pass this object to your hud it to be able to use it as target for CCMenuItem. Or you can implement some property in your scene to ask hud's layer parent about level layer – Morion Apr 05 '13 at 11:12
  • So I tried using CCLayer *layer; & @property (nonatomic, retain) CCLayer *layer; as I think that's what your trying to say. But still nothing :( I also have the Learn Cocos2D book if you need to point to any references. – Troy R Apr 05 '13 at 11:35
  • Should I be using a singleton? – Troy R Apr 05 '13 at 12:16
  • 2
    From your comments I understood that you are very far from programming. You don't know even basic and common things. And i don't have time to learn you. Read at least some literature and then read my answer again. – Morion Apr 05 '13 at 12:39
  • Thanks Morion got it to work with GameScene * _layer; and - (id)initWithHUD:(GameScene *)layer; :D – Troy R Apr 06 '13 at 07:36