0

Im running an animation using the following code

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("kid1.plist");


        CCSpriteBatchNode *spritesheet = CCSpriteBatchNode::create("kid1.png");
        this->addChild(spritesheet);

        CCArray *kidframes = new CCArray;
        for(int i=1; i<3; i++){
            CCString *filename = CCString::createWithFormat("kid%d.png",i);
            CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(filename->getCString());
            kidframes->addObject(frame);
        }

        CCAnimation *runanim = CCAnimation::createWithSpriteFrames(kidframes, 0.1);
        CCSprite *kiddo = CCSprite::createWithSpriteFrameName("kid2.png");

        kiddo->setPositionX(100*setScreenX);
        kiddo->setPositionY(100*setScreenY);
        kiddo->setScaleX(setScreenX);
        kiddo->setScaleY(setScreenY);

        CCAction *action = CCRepeatForever::create(CCAnimate::create(runanim));
        kiddo->runAction(action);
        spritesheet->addChild(kiddo);

THIS ISNT WORKING ITS GIVING ME AN ERROR WHEN I ADD FRAMES TO KID FRAMES CCAssert(m_uReference > 0, "reference count should greater than 0"); any help?

3 Answers3

1

Here's something that we used to animate our Sprites.

CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.05f);

//Add the frames to the animation
string animationFramePrefix = "mySprite";
string  animationFrames = "1,2,3,4,5,6,7,8,9";

char *strArr=new char[animationFrames.size()+1];
strArr[animationFrames.size()]=0;
memcpy(strArr,animationFrames.c_str(),animationFrames.size());

const char* p;
for (p = strtok(strArr, "," );  p;  p = strtok( NULL, "," ))
{
    string frameName = animationFramePrefix+"_"+p+".png";
    CCSpriteFrame* sprite =  CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frameName.c_str());
    animation->addSpriteFrame(sprite);
}

//initialize sprite with initial display frame
CCSprite *mySprite = CCSprite::createWithSpriteFrameName("mySprite_1.png");

//then use the animation when required!
CCAnimate *animationAction = CCAnimate::create(animation);
CCRepeatForever *repeatAction = CCRepeatForever::create(animationAction);
mySprite->runAction(repeatAction);
uchamp
  • 2,492
  • 1
  • 20
  • 31
0

Try to change

CCArray *kidframes = new CCArray;

to

CCArray *kidframes = CCArray::create();
pktangyue
  • 8,326
  • 9
  • 48
  • 71
  • i added the plist files again its not showing an error now but what its doing is that it starts the game but when the animation has to start the game just closes :/ – user2585915 Jul 17 '13 at 09:19
0

instead of new ccarray();

try create::ccarray();

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
Muhammad Shauket
  • 2,643
  • 19
  • 40