Cocos2d:CCSprite initWithFile在CCSprite子类崩溃 [英] Cocos2d: CCSprite initWithFile in CCSprite subclass crashes

查看:653
本文介绍了Cocos2d:CCSprite initWithFile在CCSprite子类崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有cocos2d项目与自定义CCSprite子类:



MyCustomSprite.h:

 code> #importcocos2d.h
@interface MyCustomSprite:CCSprite
@end


b $ b

MyCustomSprite.m:

  #importMyCustomSprite.h
@implementation MyCustomSprite
- (id)init
{
self = [super initWithFile:@index.png];
return self;
}
@end

出于某种奇怪的原因, EXC_BAD_ACCESS。



但是尽管如此,如果i init超级作为ususal,然后从CCSprite的initWithFile和initWithTexture写代码,

  self = [super init]; 
if(self){
//来自CCSprite.m的代码 - initWithFile
CCTexture2D * texture = [[CCTextureCache sharedTextureCache] addImage:@index.png];
CGRect rect = CGRectZero;
rect.size = texture.contentSize;

//来自CCSprite.m的代码 - iniWithTexture
[self setTexture:texture];
[self setTextureRect:rect];

return self;
}

第一个示例崩溃的原因是什么,

解决方案

好的,原因是CCSprite设计不好。如果我们看看CCSprite.h,我们可以找到:

   - (id)initWithTexture:(CCTexture2D *)texture rect :( CGRect)rect 
{
NSAssert(texture!= nil,@Invalid texture for sprite);
//重要:[self init]而不是[super init];
if((self = [self init]))
}

这是原因。而不是 [super init] 这个方法调用 [self init] ,并创建递归 [self init] - [super InitWithFile:] - [self initWithTexture] - [self init] -...)



/ p>

所以,解决这个问题的最简单的方法 - 只是重命名你的init方法为其他东西(例如initialize),并调用它而不是init: [[MyCustomSprite
alloc] initialize]


I have cocos2d project with custom CCSprite subclass:

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

For some strange reason, this code will crash with "EXC_BAD_ACCESS".

But in spite of this, if i init super as ususal and then write code from CCSprite's initWithFile and initWithTexture, it will work fine:

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;
} 

What's the reason that the first example crashes, and second not and what's the difference between them?

Thanks for your answers!

解决方案

Ok, the reason is bad CCSprite design. If we look to CCSprite.h, we can find:

-(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]) )
}

And thats the reason. Instead of [super init] this method calls [self init], and creates recursion ([self init]-[super InitWithFile:]-[self initWithTexture]-[self init]-...).

.

So, the simplest way to solve this problem - just re-name your init method to something else (for example "initialize") and call it instead of init: [[MyCustomSprite alloc] initialize].

这篇关于Cocos2d:CCSprite initWithFile在CCSprite子类崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆