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

  • SEARCH
  • Home
  • 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 6692921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:55:23+00:00 2026-05-26T05:55:23+00:00

I’ve got a class called GamePlay , which looks somewhat like this: @implementation GamePlay

  • 0

I’ve got a class called GamePlay, which looks somewhat like this:

@implementation GamePlay
-(id)init{
    if((self = [super init])){
        self.isAccelerometerEnabled = YES;

        //ship defined in header
        ship = [Ship shipWithParentNode:self];
        CGSize screenSize = [[CCDirector sharedDirector] winSize];
        float imageHeight = [ship spriteContentSize].height;
        [ship setSpritePosition:CGPointMake(screenSize.width*0.5, imageHeight*0.5)];
    }
    return self;
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
    CGPoint pos = [ship spritePosition];
    pos.x = acceleration.x * 10;
    [ship setSpritePosition:pos];
}
@end

So, the Ship class looks something like this:

@implementation Ship

+(id)shipWithParentNode:(CCNode *)parentNode{
    return [[[Ship alloc] initWithParentNode:parentNode] autorelease];
}

-(id)initWithParentNode:(CCNode *)parentNode{
    if((self = [super init])){
        sprite = [CCSprite spriteWithFile:@"ship.png"];
        SpriteLoader *spriteLoader = [SpriteLoader sharedSpriteLoader];
        [spriteLoader addTextureAtlas:@"Spites_default.plist"];
        CCAnimation *spriteAnimation = [CCAnimation animationWithFrames:[spriteLoader getSpriteFramesWithName:@"ship-" andAmount:[NSNumber numberWithInt:5]] delay:0.08f];
        CCAnimate *anim = [CCAnimate actionWithAnimation:spriteAnimation];
        CCRepeatForever *repeat = [CCRepeatForever actionWithAction:anim];
        [sprite runAction:repeat];
        [parentNode addChild:sprite z:0 tag:SHIP];
    }
    return self;
}

-(void)setSpritePosition:(CGPoint)point{
    sprite.position = point;
}

-(CGPoint)spritePosition{
    return sprite.position;
}

-(CGSize)spriteContentSize{
    return sprite.contentSize;
}
@end

GamePlay initialises the Ship fine and continues to modify the CCSprite within Ship, and in the simulator, all is well. When it comes to running it on a device, we get accelerometer input. In the accelerometer method, the first line crashed the program because the ship no longer exists. If I look in the debugger with a breakpoint on the first line of the accelerometer method I see the following:
self = (GamePlay *)
-> ship = (Ship *) 0x004701e0

If I then continue the running of the program, I get this message:
message sent to deallocated instance 0x4701e0

But I’m not sure why it’s been deallocated…And presumably 0x004701e0 == 0x4701e0 ?

Many thanks,
Ben

  • 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-26T05:55:24+00:00Added an answer on May 26, 2026 at 5:55 am

    To explain the problem:

    • GamePlay class initializes Ship class instance as autorelease
    • Ship class initializes a sprite class and adds it as children to parentNode (GamePlay class), which retains the sprite but not the Ship class itself

    Ole is correct that the Ship needs a retain in the code you posted. Personally I see a design problem here. Your Ship class should derive from CCNode, and you should add the Ship to the node hierarchy via addChild.

    If Ship derives just from NSObject, what you end up with is sort of like a “broken” node hierarchy:

    • Cocos2D GamePlay class (ie CCLayer)
    • NSObject Ship class
    • Cocos2D CCSprite instance contained in Ship class, but added to GamePlay class

    The problem with that is that the Ship class’ sprite is “managed” by the Ship class but retained by the GamePlay class. That means you’re responsible for retaining the non-CCNode classes like Ship yourself. The following makes for a simpler and more Cocos2D conform design, because it relies on Cocos2D managing the entire node hierarchy:

    • Cocos2D GamePlay class (ie CCLayer)
    • Cocos2D Ship class (derived from CCNode)
    • Cocos2D CCSprite instance contained in Ship class, added to Ship class as child

    That way you would add the Ship class as child to the GamePlay class. The Ship class adds its CCSprite instance to self (the Ship class). You can (and should) get rid of the parentObject passed in the shipWithParentNode method. In Cocos2D it is quite dangerous to pass an instance of a node to another node, you might be tempted to retain that, which can lead to leaking the entire scene because of circular retain dependencies (a retains b, b retains a, both can’t let go).

    Overall this makes your node hierarchy a bit deeper but it is much easier to manage. You can assume all objects to derive from a common base class (CCNode) and you can rely on Cocos2D deallocating the node hierarchy in the right order at the right times.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string

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.