0

我使用以下代码运行动画

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);

当我将帧添加到儿童帧 CCAssert(m_uReference > 0, "reference count should greater than 0"); 有什么帮助吗?

4

3 回答 3

1

这是我们用来为 Sprite 设置动画的东西。

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);
于 2014-01-09T06:41:26.767 回答
0

代替new ccarray();

尝试create::ccarray();

于 2014-05-22T13:02:53.777 回答
0

尝试改变

CCArray *kidframes = new CCArray;

CCArray *kidframes = CCArray::create();
于 2013-07-17T08:40:48.487 回答