<small id='4FcNU'></small><noframes id='4FcNU'>

      1. <legend id='4FcNU'><style id='4FcNU'><dir id='4FcNU'><q id='4FcNU'></q></dir></style></legend>

      2. <i id='4FcNU'><tr id='4FcNU'><dt id='4FcNU'><q id='4FcNU'><span id='4FcNU'><b id='4FcNU'><form id='4FcNU'><ins id='4FcNU'></ins><ul id='4FcNU'></ul><sub id='4FcNU'></sub></form><legend id='4FcNU'></legend><bdo id='4FcNU'><pre id='4FcNU'><center id='4FcNU'></center></pre></bdo></b><th id='4FcNU'></th></span></q></dt></tr></i><div id='4FcNU'><tfoot id='4FcNU'></tfoot><dl id='4FcNU'><fieldset id='4FcNU'></fieldset></dl></div>
        <tfoot id='4FcNU'></tfoot>

        • <bdo id='4FcNU'></bdo><ul id='4FcNU'></ul>

        Cocos2d:CCSprite 子类中的 CCSprite initWithFile 崩溃

        时间:2024-08-12
        <tfoot id='Eb8bs'></tfoot>

          <i id='Eb8bs'><tr id='Eb8bs'><dt id='Eb8bs'><q id='Eb8bs'><span id='Eb8bs'><b id='Eb8bs'><form id='Eb8bs'><ins id='Eb8bs'></ins><ul id='Eb8bs'></ul><sub id='Eb8bs'></sub></form><legend id='Eb8bs'></legend><bdo id='Eb8bs'><pre id='Eb8bs'><center id='Eb8bs'></center></pre></bdo></b><th id='Eb8bs'></th></span></q></dt></tr></i><div id='Eb8bs'><tfoot id='Eb8bs'></tfoot><dl id='Eb8bs'><fieldset id='Eb8bs'></fieldset></dl></div>

          <small id='Eb8bs'></small><noframes id='Eb8bs'>

            <bdo id='Eb8bs'></bdo><ul id='Eb8bs'></ul>
                <tbody id='Eb8bs'></tbody>

              <legend id='Eb8bs'><style id='Eb8bs'><dir id='Eb8bs'><q id='Eb8bs'></q></dir></style></legend>

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

                  问题描述

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

                  I have cocos2d project with custom CCSprite subclass:

                  MyCustomSprite.h:

                  MyCustomSprite.h:

                  #import "cocos2d.h"
                  @interface MyCustomSprite : CCSprite
                  @end
                  

                  MyCustomSprite.m:

                  MyCustomSprite.m:

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

                  由于某些奇怪的原因,这段代码会因EXC_BAD_ACCESS"而崩溃.

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

                  但尽管如此,如果我像往常一样初始化超级,然后从 CCSprite 的 initWithFile 和 initWithTexture 编写代码,它会正常工作:

                  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?

                  感谢您的回答!

                  推荐答案

                  好吧,原因是 CCSprite 设计不好.如果我们查看 CCSprite.h,我们可以发现:

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

                  这就是原因.此方法调用 [self init] 代替 [super init],并创建递归 ([self init]-[super InitWithFile:]-[self initWithTexture]-[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]-...).

                  .

                  因此,解决此问题的最简单方法 - 只需将您的 init 方法重命名为其他名称(例如initialize")并调用它而不是 init:[[MyCustomSpritealloc] 初始化].

                  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 子类中的 CCSprite initWithFile 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何实现 Cocos2D? 下一篇:将 GameCenter 添加到仅限横向的 cocos2d 应用程序后,iOS 6 出现错误

                  相关文章

                    <i id='0Hk99'><tr id='0Hk99'><dt id='0Hk99'><q id='0Hk99'><span id='0Hk99'><b id='0Hk99'><form id='0Hk99'><ins id='0Hk99'></ins><ul id='0Hk99'></ul><sub id='0Hk99'></sub></form><legend id='0Hk99'></legend><bdo id='0Hk99'><pre id='0Hk99'><center id='0Hk99'></center></pre></bdo></b><th id='0Hk99'></th></span></q></dt></tr></i><div id='0Hk99'><tfoot id='0Hk99'></tfoot><dl id='0Hk99'><fieldset id='0Hk99'></fieldset></dl></div>

                    <small id='0Hk99'></small><noframes id='0Hk99'>

                  1. <legend id='0Hk99'><style id='0Hk99'><dir id='0Hk99'><q id='0Hk99'></q></dir></style></legend>
                      <bdo id='0Hk99'></bdo><ul id='0Hk99'></ul>

                    1. <tfoot id='0Hk99'></tfoot>