我有自定义 CCSprite 子类的 cocos2d 项目:
MyCustomSprite.h:
#import "cocos2d.h"
@interface MyCustomSprite : CCSprite
@end
MyCustomSprite.m:
#import "MyCustomSprite.h"
@implementation MyCustomSprite
- (id)init
{
self = [super initWithFile:@"index.png"];
return self;
}
@end
由于某些奇怪的原因,此代码将因“EXC_BAD_ACCESS”而崩溃。
但尽管如此,如果我像往常一样初始化超级,然后从 CCSprite 的 initWithFile 和 initWithTexture 编写代码,它会正常工作:
self = [super init];
if (self) {
// Code from CCSprite.m - initWithFile
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: @"index.png"];
CGRect rect = CGRectZero;
rect.size = texture.contentSize;
// Code from CCSprite.m - iniWithTexture
[self setTexture:texture];
[self setTextureRect:rect];
return self;
}
第一个示例崩溃的原因是什么,第二个示例没有,它们之间有什么区别?
感谢您的回答!