继承CCSprite时不要重写init方法,否则会无限循环

在Learn cocos2D Game Development with iOS 5一书中
第六章提到了A Common and Fatal Mistake
大概说的就是当继承CCSrpite对象的时候不要重写init方法,例如


@interface Ship : CCSprite 
{
}
+(id) ship;

@implementation Ship

+(id) ship
{
	return [[[self alloc] init] autorelease];
}

-(id) init//这里是错误的
{
	CCLOG(@"Ship init called ... this will repeat infinetely.");
	
	// WARNING: this will cause an infinite loop!
	// Reason: [super initWithFile:...] will call -(id) init which is overridden here ... repeat ad infinitum
	// Fix: rename this init method, for example initWithShipImage
	// Tip: never call anything but [super init] in your class' init method. If you call any initWith* method
	// then you should never do this from the -(id) init method.
	if ((self = [super initWithFile:@"ship.png"]))
	{
		[self scheduleUpdate];
	}
	return self;
}
@end

正确的方式应该是这样的

-(id) initWithShipImage//这里的函数名可以随便去,就是不要与init一样
{
	if ((self = [super initWithFile:@"ship.png"])) {
		[self scheduleUpdate]; 
	}
    return self;
}

为什么会出现这种问题你自己追踪到CCSprite源码就明白了
1。点击super initWithFile跳转到CCSprite的initWithFile方法中
源码如下

-(id) initWithFile:(NSString*)filename
{
	NSAssert(filename!=nil, @"Invalid filename for sprite");

	CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: filename];
	if( texture ) {
		CGRect rect = CGRectZero;
		rect.size = texture.contentSize;
		return [self initWithTexture:texture rect:rect];
	}

	[self release];
	return nil;
}

2。点击self initWithTexture跳转到CCSprite的initWithTexture方法中
源码如下

-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
	NSAssert(texture!=nil, @"Invalid texture for sprite");
	// IMPORTANT: [self init] and not [super init];
	if( (self = [self init]) )
	{
		[self setTexture:texture];
		[self setTextureRect:rect];
	}
	return self;
}

3。注意看上面代码中if( (self = [self init]) ),
你发现OC里边比较习惯的就是用if ((self = [super init]) ),而这里却不是,这就是在继承CCSrpite后如果你重写了init方法程序就会无限循环的原因

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值