手动动画在Cocos2d-x中不起作用

时间:2014-01-06 01:12:24

标签: animation cocos2d-x

我正在测试手动动画。但是,动画似乎有一些问题,所以它不起作用。

这些是代码。你能看一看,让我在哪里错了。

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h" 
#include <string>
USING_NS_CC;

class HelloWorld : public cocos2d::CCLayerColor{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();  

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);    

virtual void onEnter();

protected:
CCSprite*    m_grossini;

};
#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h" 

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();

    HelloWorld *layer = HelloWorld::create();

    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayerColor::initWithColor( ccc4(255,255,255,255) ) )
    {
        return false;
    }

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));

    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    m_grossini = CCSprite::create("grossini.png");
    m_grossini->retain();
    this->addChild(m_grossini);
    m_grossini->setPosition(ccp(winSize.width/2, winSize.height/2));
    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

void HelloWorld::onEnter(){
    CCAnimation* animation = CCAnimation::create();
    for( int i=1;i<15;i++)
    {
        char szName[100] = {0};
        sprintf(szName, "grossini_dance_%02d.png", i);
        animation->addSpriteFrameWithFileName(szName);
    }
    // should last 2.8 seconds. And there are 14 frames.
    animation->setDelayPerUnit(2.8f / 14.0f);
    animation->setRestoreOriginalFrame(true); 
    CCAnimate* action = CCAnimate::create(animation);
    m_grossini->runAction(CCSequence::create(action, action->reverse(), NULL));
}

我希望你能帮助我。非常感谢你!

2 个答案:

答案 0 :(得分:4)

尝试以下事项:..

首先删除此行m_grossini->retain();,因为addchild会自动增加其保留计数。

发现问题..

当您重写OnEnter方法时,需要手动为基类调用它。在你的情况下添加行: -

    CCLayerColor::onEnter();
在OnEnter方法中的

将完成工作。

将来在覆盖基类方法时要小心。

答案 1 :(得分:0)

试试这段代码:

CCAnimation* animation = CCAnimation::create();

for( int i=1;i<15;i++)

animation->addSpriteFrameWithFileName((CCString::createWithFormat("grossini_dance_%02d.png",i)->getCString()));
// should last 2.8 seconds. And there are 14 frames.
 animation->setDelayPerUnit(2.8f / 14.0f);
 animation->setRestoreOriginalFrame(true); 
 CCAnimate* action = CCAnimate::create(animation);
 m_grossini->runAction(action);  //Run once all frame(OR) 
 m_grossini->runAction(CCRepeatForever::create(action)); //Run RepeatForever