通过cocos2dx中的texturepacker为windows phone加载动画

时间:2013-07-17 07:11:20

标签: animation windows-phone cocos2d-x texturepacker

我使用以下代码运行动画

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&gt; 0,“引用计数应大于0”); 有什么帮助吗?

3 个答案:

答案 0 :(得分:1)

这是我们用来为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);

答案 1 :(得分:0)

尝试更改

CCArray *kidframes = new CCArray;

CCArray *kidframes = CCArray::create();

答案 2 :(得分:0)

而不是new ccarray();

尝试create::ccarray();