So far, we have learned
Coordinate System
Memory Management
UI System
Event Processing
Ry
Today, let's learn about action management. OK.
Let's take a look at the class structure diagram.
Base class of all CCAction
The following three subclasses are derived: CCFiniteTimeAction, CCFollow, and CCSpeed.
We will not take a look at these. We will mainly introduce transient actions,
CCActionInstant
An action that is completed in an instant without delay.
Good start
#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;}
We have created two genie displays, which are mainly used to display the results when the flip action is demonstrated.
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); // flip X axis // parameter: true flip false do not flip CCActionInstant * pFlipX = CCFlipX: create (true ); pSprite1-> runAction (pFlipX); // flip Y axis // parameter: true flip false do not flip 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 ;}
We use genie 1 to execute the action, while genie 2 does nothing. Obviously, we can see the difference.
Show Hidden actions:
// Hide the animation CCActionInstant * pHide = CCHide: create (); pSprite1-> runAction (pHide); // display the animation CCActionInstant * pShow = CCShow: create (); pSprite1-> runAction (pShow); // if this action is hidden, it will be displayed. If it is displayed, it will hide CCActionInstant * pToggleVisibility = CCToggleVisibility: create ();; pSprite1-> runAction (pToggleVisibility );
Move position action:
// Parameter: You need to move to the specified position CCActionInstant * pPlace = CCPlace: create (ccp (visibleSize. width/2-pSprite1-> getContentSize (). width, visibleSize. height/2-50); pSprite1-> runAction (pPlace );
Remove action:
// Parameter: whether to clear the memory; true: false: No. // Note: The parameter here is not whether to remove CCActionInstant * pRemoveSelf = CCRemoveSelf: create (true ); pSprite1-> runAction (pRemoveSelf );
Okay, this is the instantaneous action. There are several other things, such as Grid and Bsound, which are not commonly used. Please try again later.
Summary:
Instantaneous action base class CCActionInstant
Flip action: CCFlipX, CCFlipY
Hide show action: CCHideCCShowCCToggleVisibility
Move position action: CCPlace
Removal action: CCRemoveSelf