Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am very new at Cocos2d and is testing to displaying an image over the "Hello World" text on a second layer and need help to get it work. I guess it is some basic stuff here and appreciate any tips etc. with this.

I know that if i put the display-code (myLayer1) in the "init" it work or do the call [self goHere] from the "init" in myLayer1 it works but i want to call the "goHere" directly.

I have the following code:

HelloWorld.m:

#import "HelloWorldLayer.h"
#import "myLayer1.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
myLayer1 *layer1 = [myLayer1 node];

// add layer as a child to scene
[scene addChild: layer];
[scene addChild: layer1];

// return the scene
return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

    // create and initialize a Label
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

    // ask director the the window size
    CGSize size = [[CCDirector sharedDirector] winSize];

    // position the label on the center of the screen
    label.position =  ccp( size.width /2 , size.height/2 );

    // add the label as a child to this Layer
    [self addChild: label];


    myLayer1 *a1 = [myLayer1 new];
    [a1 goHere];
    [myLayer1 release];


}
return self;
}

myLayer1.m:

#import "myLayer1.h"

@implementation myLayer1


-(void)goHere {

NSLog(@">>>>goHere<<<<");

CGSize size = [[CCDirector sharedDirector] winSize];

CCSprite *vv = [CCSprite spriteWithFile:@"hand.png"];
vv.position =  ccp( size.width /2 , size.height/2 );
[self addChild:vv z:3];
}

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {


}

return self;
}

@end
share|improve this question
    
You're adding myLayer twice. Once in HelloWorld -init and once in HelloWorld +scene. Is that on purpose? –  bummzack Mar 21 '12 at 10:21
    
i am afraid not, thanks :-) –  PeterK Mar 22 '12 at 23:39
add comment

2 Answers

I am not sure what are your intentions for adding a ccsprite on different layer is, but you can have the reference of the layer in your ccscene class and then add the ccsprite on the go to that reference.

But if you dont want to have an instance of that object in your ccscene class then you can get the reference to that layer by adding the tag and then get the layer with tag when ever you want to add any node to that layer.

The procedure is simple.

myLayer1 *a1 = [myLayer1 new];
a1.tag = 300;
[self addChild:a1];
[a1 release];

//some other function
myLayer1 *a1 = (myLayer1 *)[self getChildByTag:300];
[a1 goHere];

PS. also there are some problems with your code, you might want to dig into that.

share|improve this answer
add comment

These lines of code are wrong:

 myLayer1 *a1 = [myLayer1 new];
[a1 goHere];
[myLayer1 release];

for many reasons. First of all you have already instantiated a myLayer1 object in the scene static method (notice that the node method is equivalent to an alloc plus init invokation returning an autorelease pointer). Moreover you are not adding a1 instance to any parent object so its content won't be depicted. Finally you are releasing a class no the instance.

What you should do is to go in the HelloWorldLayer.h and put there the myLayer1 declaration as a class attribute

myLayer1 *layer1;

then in the HelloWorldLayer.m scene method you just have to instantiate it this way:

layer1 = [myLayer1 node];
[scene addChild:layer1];

then in the HelloWorldLayer.m init method you can invoke its method goHere

[layer1 goHere];
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.