objective-ccocos2d-iphoneccnode

Cannot add CCNode to Scene


I have the IntroScene, and I wanna add a node, but it doesn't seem to work. Here are two different ways I tried doing it, and BOTH failed.

First way, failed:

in hearts2.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Hearts2 : CCNode {

}

@end

in hearts2.m

#import "Hearts2.h"


@implementation Hearts2

@end

in IntroLayer.m

- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);

heart2 *heart;
[self addChild:heart z:2];

// done
return self;
}

I didn't expect that to work (actually I was desperate and tried it that way as the second way just to see if it would work). The actual first attempt I tried to do was this, and it also Failed:

in hearts1.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Hearts1 : CCNode

+ (Hearts1 *)node;
- (id)init;
-(void)selfAnimate;



@end

in hearts1.m

#import "Hearts1.h"

@implementation Hearts1 {
}

+ (Hearts1 *)node
{
    return [[self alloc] init];
}

- (id)init
{
    self = [super init];
    if (!self) return(nil);


    return self;

}
- (void)dealloc
{
}

- (void)onEnter
{
    [super onEnter];

}

- (void)onExit
{
    // always call super onExit last
    [super onExit];
}



- (void)selfAnimate
{
}
@end

in IntroLayer.m

- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);

heart1 *heart;
[self addChild:heart z:2];

// done
return self;
}

Please, I would do anything if someone could help me figure this out thanks everyone very much. I always get the SigABRT so I have no idea what is going wrong. I'm sure I'm just stupid and don't know how to code and missing something simple.


Solution

  • heart2 *heart;
    

    You named your class Hearts2 so use the exact same name, including uppercase.

    Secondly you created a variable but this will be nil. If you aren't using ARC (which you should) this will create an uninitialized object.

    This will create an instance of Hearts2, assign it to the local var heart and add it as a child:

    Hearts2 *heart = [Hearts2 node];
    [self addChild:heart z:2];