i’m beginner for cocos2d-iphone.
i have a problem with addchild to gamescene.
i made a simple class derived from CCSprite and i’ve tried to display this class.
but it didn’t work and i don’t know what is problem.
this is my code of class:
//myClass.h
#import "cocos2d.h"
@interface myClass:CCSprite{
}
@end
//myClass.m
#import "myClass.h"
@implementation myClass
-(id) init{
if( self = [super initWithFile:@"title.png"] ){
self.position = ccp(240, 240);
}
return self;
}
@end
and this is a part of gamescene:
//HelloWorldLayer.m
...
// this worked well.
// myClass* temp = [CCSprite spriteWithFile:@"title.png"];
// temp.position = ccp(240, 240);
// [self addChild:temp];
// but this won't work.
myClass* temp = [[myClass alloc] init];
[self addChild:temp];
...
what should i do to solve this problem?
If you check the code, initWithFile calls initWithTexture that calls init, so you are in a loop and that’s cause the problem. Try to make another constructor or a class method that creates your sprite and set position.
For example