0

I am attempting to create an animated sprite class, which has methods for starting and stoping the animation. When I move my CCSprite variable from a local to an ivar, the class throws an exception. With the CCSprite variable as a local, the class work, that is the sprite appears and is animated.

The project is ARC enabled, with all of the coocs2d files flagged with -fno-objc-arc. I used TexturePacker to create the underlying files, altho I don't think that has anything to do with the problem.

What am I doing wrong?

cocos2D version 2.01.00 Xcode Version 4.6.2

How the class is being called:

CCSpriteBatchNode *animatedSprite = [[AnimatedSprite alloc] init];
animatedSprite.position = ccp(winSize.width / 2, winSize.height / 2);
[self addChild:animatedSprite];

class header

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

@interface AnimatedSprite : CCSpriteBatchNode
{
}

@end

class that works

#import "AnimatedSprite.h"

@interface AnimatedSprite()
{
    CCAction *action;
//    CCSprite *sprite;
}

@end

@implementation AnimatedSprite

- (id)init
{
    if (self = [super init])
    {
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spriteSheet.plist"];
        self = [CCSpriteBatchNode batchNodeWithFile:@"spriteSheet.png"];

        NSMutableArray *animatedFrames = [NSMutableArray array];
        for (int i=1; i<=4; i++)
        {
            [animatedFrames addObject:
             [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
              [NSString stringWithFormat:@"sprite%d.png",i]]];
        }
        CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
//        sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
        CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animatedFrames delay:0.1f];
        action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
        [sprite runAction:action];
        [self addChild:sprite];
    }
    return self;
}

@end

class that throws an exception

#import "AnimatedSprite.h"

@interface AnimatedSprite()
{
    CCAction *action;
    CCSprite *sprite;
}

@end

@implementation AnimatedSprite

- (id)init
{
    if (self = [super init])
    {
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spriteSheet.plist"];
        self = [CCSpriteBatchNode batchNodeWithFile:@"spriteSheet.png"];

        NSMutableArray *animatedFrames = [NSMutableArray array];
        for (int i=1; i<=4; i++)
        {
            [animatedFrames addObject:
              [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
               [NSString stringWithFormat:@"sprite%d.png",i]]];
        }
//        CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
        sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
        CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animatedFrames delay:0.1f];
        action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
        [sprite runAction:action];
        [self addChild:sprite];
    }
    return self;
}

@end

Stack trace

#0  0x01efc0ab in objc_release ()
#1  0x01efcbd9 in (anonymous namespace)::AutoreleasePoolPage::pop(void*) ()
#2  0x024ae468 in _CFAutoreleasePoolPop ()
#3  0x0156f8e4 in -[NSAutoreleasePool release] ()
#4  0x00abbc16 in _UIApplicationHandleEvent ()
#5  0x03214df9 in _PurpleEventCallback ()
#6  0x03214ad0 in PurpleEventCallback ()
#7  0x02481bf5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#8  0x02481962 in __CFRunLoopDoSource1 ()
#9  0x024b2bb6 in __CFRunLoopRun ()
#10 0x024b1f44 in CFRunLoopRunSpecific ()
#11 0x024b1e1b in CFRunLoopRunInMode ()
#12 0x00ab717a in -[UIApplication _run] ()
#13 0x00ab8ffc in UIApplicationMain ()
#14 0x000dcc66 in main at /iPhone Apps/Cocos/SpriteTest/SpriteTest/main.m:15
#15 0x000022b5 in start ()
zermat
  • 991
  • 9
  • 15

1 Answers1

-1

Why are you trying to create a class that does sprite animation? There are many things out already that does this work for you. Look into sprite and level helper. They do animation automatically for you. There is no need to reinvent something that is already out for you :)

LAMBORGHINI
  • 338
  • 2
  • 13
  • It is great that you gave him a link for some help. However, it is kind of rude not to answer the question. Giving alternative information is cool, but the reason he/she posted the question, was for an answer. – AlienDev Aug 24 '14 at 03:10