Cocos2D-X2.2.3學習筆記12(瞬時動作)

來源:互聯網
上載者:User


到目前我們已經學習了有

座標系統

記憶體管理

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.