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 4574728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:57:47+00:00 2026-05-21T19:57:47+00:00

I started experimenting with Cocos2D with Tiled, and had the player sprite and actions

  • 0

I started experimenting with Cocos2D with Tiled, and had the player sprite and actions coded within a CCLayer along with everything else. Before continuing on, I wanted to subclass the player into a CCLayer, which I hope is correct.

My header and main code is as follows:

HeroClass.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface HeroClass : CCLayer {
    CCSprite *_hero;
    CCAction *_heroSpriteFlyAction;

}

@property(nonatomic, retain) CCSprite *hero;
@property(nonatomic, retain) CCAction *heroSpriteFlyAction;

@end

HeroClass.m

#import "HeroClass.h"

@implementation HeroClass

@synthesize hero =_hero;
@synthesize heroSpriteFlyAction = _heroSpriteFlyAction;

-(id) init{
    self = [super init];
    if (!self) {
        return nil;
    }

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"heroTestSheet.plist"];

    CCSpriteBatchNode *heroSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"heroTestSheet.png"];

    [self addChild:heroSpriteSheet];

    NSMutableArray *heroSpriteFlyAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 2; ++i) {
        [heroSpriteFlyAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"heroFrame%d.png", i]]];
    }

    CCAnimation *heroSpriteFlyAnim = [CCAnimation animationWithFrames:heroSpriteFlyAnimFrames delay:0.03f];

    self = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"];  

    _heroSpriteFlyAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:heroSpriteFlyAnim restoreOriginalFrame:NO]];
    [self runAction:_heroSpriteFlyAction];

    [heroSpriteSheet addChild:self];


    return self;
}

- (void) dealloc{
    self.hero = nil;
    self.heroSpriteFlyAction = nil;
    [super dealloc];
}

@end

I think the idea I want to achieve is that I can access things in this class as properties in other files. The code above gives no errors when I build it, but maybe I didn’t do something right. The problem I’m having with the migration is what is happening now in my CCLayer class DebugZoneLayer, which creates the map and is supposed to add my player sprite but is giving me errors.

In DebugZoneLayer.h I imported the HeroClass.h and made a pointer from the HeroClass of the hero sprite and gave it a property. No errors here but it may be the start of where I’m going wrong:

#import "cocos2d.h"
#import "HeroClass.h"
@class HeroClass;

// DebugZone Layer
@interface DebugZoneLayer : CCLayer {

    HeroControl *heroControl;

    HeroClass *hero;    

    CCTMXTiledMap *theMap;
    CCTMXLayer *blocksCollidable;
    CCTMXLayer *invisiblePropertiesLayer;   
}


@property(nonatomic, retain) CCSprite *hero;

In DebugZoneLayer.m, when I synthesize hero, it gives the error “Type of property ‘hero’ does not match type of ivar ‘hero’

@synthesize hero;

The rest of the file gives me more errors related to anything referencing hero, but at least that’s where it starts.

EDIT (updated)

Just wanted to mention, since this was solved I cleared up some major issues in HeroClass.m which was causing a crash:

#import "HeroClass.h"

@implementation HeroClass

@synthesize heroSprite =_heroSprite;
@synthesize heroSpriteSheet =_heroSpriteSheet;
@synthesize heroSpriteFlyAction = _heroSpriteFlyAction;

-(id) init{
    self = [super init];
    if (!self) {
        return nil;
    }

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"heroTestSheet.plist"];

    _heroSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"heroTestSheet.png"];

    //[self addChild:_heroSpriteSheet];

    NSMutableArray *heroSpriteFlyAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 2; ++i) {
        [heroSpriteFlyAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"heroFrame%d.png", i]]];
    }

    CCAnimation *heroSpriteFlyAnim = [CCAnimation animationWithFrames:heroSpriteFlyAnimFrames delay:0.03f];

    _heroSprite = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"];  

    _heroSpriteFlyAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:heroSpriteFlyAnim restoreOriginalFrame:NO]];
    [self runAction:_heroSpriteFlyAction];

    [_heroSpriteSheet addChild:_heroSprite];


    return self;
}

- (void) dealloc{
    self.heroSprite = nil;
    self.heroSpriteSheet = nil;
    self.heroSpriteFlyAction = nil;
    [super dealloc];
}

@end
  • 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-21T19:57:48+00:00Added an answer on May 21, 2026 at 7:57 pm

    Trying changing your property in the DebugZoneLayer class from:

    @property(nonatomic, retain) CCSprite *hero;
    

    To:

    @property(nonatomic, retain) HeroClass *hero;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started experimenting with SDL in C++, and I thought checking for memory
I've just started experimenting something with Matlab, and since I'm used to Vim's interface,
I recently started experimenting with Python decorators (and higher-order functions) because it looked like
I've started experimenting with Hudson as a build server. I'm using subversion and have
I have recently started experimenting with Django for some web applications in my spare
I just started experimenting with Aptana Jaxer server side javascript engine for my next
I just started experimenting with Threads and I ran in to a problem I'm
I just started experimenting with java today (have experience with javascript and PHP) and
I just started experimenting with a new technique I name (for the moment at
I've recently started experimenting with PostSharp and I found a particularly helpful aspect to

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.