Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:33:24+00:00 2026-05-25T12:33:24+00:00

I have cocos2d project with custom CCSprite subclass: MyCustomSprite.h: #import cocos2d.h @interface MyCustomSprite :

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:33:25+00:00Added an answer on May 25, 2026 at 12:33 pm

    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]
    .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my cocos2d project, I have two scenes. I transition between the two using
I have Integrated Cocos2d to my Windows based application. When I run my Project
I have a project containing cocos2d framework, sharekit, asi & json. I've noticed that
I have a cocos2d project. Everything works fine, except when I am replacing a
I have an Xcode project (for an iPhone application) that uses cocos2d. I have
Currently I am working on a cocos2d+Box2D project so I have deal with some
I have a cocos2d project with some scenes (home scene, level selection scene, game
I have writing a small shooting game in cocos2d. Winning the game is to
I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
I have created a normal app - initially without cocos2d. Now I need cocos2d

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.