2014-01-06 44 views
0

我正在測試手動動畫。但是,動畫似乎有一些問題,所以它不起作用。手動動畫在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)); 
} 

我希望你能幫助我。非常感謝!

+0

「不工作」 指的究竟是什麼? – LearnCocos2D

+0

@ LearnCocos2D:它不動畫,雖然它似乎加載圖像來創建動畫 – lolyoshi

+0

你需要給我們一些logcat錯誤或告訴我們你的代碼不工作在哪一行? –

回答

4

嘗試以下的事情:..

首先刪除此行m_grossini->retain();,因爲自動的addChild增加其保留計數。

發現問題..

當你正在重寫OnEnter方法,你需要手動調用它的基類。在你的情況下添加行: -

CCLayerColor::onEnter(); 

在OnEnter方法將做的工作。

將來在重寫基類方法時要小心。

+0

我試圖刪除m_grossini-> retain();並嘗試你的解決方案。但是,動畫不受影響。我的意思是它似乎只是我添加的精靈,沒有更多。此外,onEnter被稱爲:( – lolyoshi

+0

嘗試使用CCLayer而不是CCLayerColor。我不明白爲什麼你甚至使用它。 – Jain

+0

我改變了所有,但沒有解決。我使用CCLayerColor,因爲我想設置背景爲白色網站 – lolyoshi

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