到目前我們已經學習了有
座標系統
記憶體管理
UI系統
事件處理
幾何圖形
今天我們來學習動作管理OK
我們來看看類結構圖
CCAction 所有動作的基類
下面派生了三個子類:CCFiniteTimeAction,CCFollow,CCSpeed
這些我們先不看 我們主要來介紹一下瞬時動作,
CCActionInstant
瞬時動作 故而 一瞬間就完成的動作,它沒有延遲時間的
好的 開始
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::CCLayer{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(); // implement the "static node()" method manually CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"USING_NS_CC;CCScene* HelloWorld::scene(){ // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCSprite* pSprite1= CCSprite::create("Icon.png");pSprite1->setPosition(ccp(visibleSize.width/2-pSprite1->getContentSize().width,visibleSize.height/2));this->addChild(pSprite1);CCSprite* pSprite2= CCSprite::create("Icon.png");pSprite2->setPosition(ccp(visibleSize.width/2+pSprite1->getContentSize().width,visibleSize.height/2));this->addChild(pSprite2); return true;}
我們建立了二個精靈顯示,這裡主要是為了等下示範翻轉動作時候能看出效果來
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCSprite* pSprite1= CCSprite::create("Icon.png");pSprite1->setPosition(ccp(visibleSize.width/2-pSprite1->getContentSize().width,visibleSize.height/2));this->addChild(pSprite1);//翻轉X軸//參數:true 翻轉 false 不翻轉CCActionInstant* pFlipX= CCFlipX::create(true);pSprite1->runAction(pFlipX);//翻轉Y軸//參數:true 翻轉 false 不翻轉 CCActionInstant* pFlipY=CCFlipY::create(true); pSprite1->runAction(pFlipY);CCSprite* pSprite2= CCSprite::create("Icon.png");pSprite2->setPosition(ccp(visibleSize.width/2+pSprite1->getContentSize().width,visibleSize.height/2));this->addChild(pSprite2); return true;}
我們用精靈1執行動作,精靈2什麼都不做,明顯就看到區別了
顯示隱藏動作:
//隱藏動畫 CCActionInstant* pHide= CCHide::create(); pSprite1->runAction(pHide); //顯示動畫 CCActionInstant* pShow= CCShow::create(); pSprite1->runAction(pShow); //如果是隱藏執行該動作就顯示,如果是顯示執行動作後就隱藏 CCActionInstant* pToggleVisibility= CCToggleVisibility::create();; pSprite1->runAction(pToggleVisibility);
移動位置動作:
//參數:需要移動到指定的位置 CCActionInstant* pPlace= CCPlace::create(ccp(visibleSize.width/2-pSprite1->getContentSize().width,visibleSize.height/2-50)); pSprite1->runAction(pPlace);
移除動作:
//參數:是否清理記憶體,true 清理 false 不清理 //注:這裡的參數可不是是否移除咯 CCActionInstant* pRemoveSelf= CCRemoveSelf::create(true); pSprite1->runAction(pRemoveSelf);
OK 瞬時動作就這些,還有幾個神馬神馬Grid,Bsound神馬神馬的 都不常用,以後用到的時候再臨時發揮吧
總結:
瞬時動作基類 CCActionInstant
翻轉動作:CCFlipX,CCFlipY
隱藏顯示動作:CCHideCCShowCCToggleVisibility
移動位置動作:CCPlace
移除動作:CCRemoveSelf