Cocosd-X create genie, Cocosd-X create genie

Source: Internet
Author: User

Cocosd-X create genie, Cocosd-X create genie

Genie: Cocos2d is similar to any other game engine. It can be moved, rotated, scaled, animated, and converted.

Cocos2dx Sprite is composed of Texure, frame, and animation, and is rendered by openes. The main class relationship is as follows: a simple process can be described as: Using Texture2D to load images, you can use Texture2D to generate the corresponding SpriteFrame (SpriteFrame) and add SpriteFrame to Animation to generate Animation data, use Animation to generate an Animation (that is, the final Animation action), and use Sprite to execute this action.

Create a project. The results in the solution shown in the project file structure

Add a png image to the Resource folder.


Add the following code to the Sprite. h file:

# Ifndef _ Sprite_H _ # define _ Sprite_H _ // prevents code re-inclusion # include "cocos2d. h "USING_NS_CC; class Sprite: public CCLayer {public: // create a scenario static CCScene * scene (); // initialize the scenario bool init (); // menu callback function void menuCloseCallback (CCObject * pSender); // used to create, Sprite, or layer (same as create () CREATE_FUNC (Sprite);}; # endif


Set bool AppDelegate: applicationDidFinishLaunching () in AppDelegate. cpp

 // create a scene. it's an autorelease object    CCScene *HelloWorld = Sprite::scene();

Change

 // create a scene. it's an autorelease object    CCScene *pScene = Sprite::scene();


Code in Sprite. cpp

# Include "Sprite. h "# include" HelloWorldScene. h "CCScene * Sprite: scene () {// create a scenario CCScene * s = CCScene: create (); // create a layer Sprite * layer = Sprite :: create (); // Add the layer to the scenario s-> addChild (layer); // return scenario return s;} // initialize the Sprite bool Sprite: init (){}


How to Create a genie:

1. Create an genie through a file

Procedure: Call CCLayer: init ()-> Create a wizard-> Add the Wizard to the scenario-> get the window size-> set the location where the Wizard is displayed in the scenario.

// Call the init function of the parent class CCLayer: init (); // create the sprite * sprite = CCSprite: create ("aaaa.png") through the file "); // Add the genie to the scene addChild (sprite); // obtain the window size CCSize winSize = CCDirector: shareddire()-> getWinSize (); // set the position of the display genie (set to display the genie in the middle of the scene) sprite-> setPosition (CCPoint (winSize. width/2, winSize. height/2); return true;

Put the code in the bool Sprite: init () under Sprite. cpp to compile and run the program. The execution result is as follows:



2. Create genie through texture

Process: Call CCLayer: init () -> Create a texture-> Create a Sprite through a texture-> Add the sprite to the scene-> get the size of the window-> set the position where the Sprite is displayed in the scene.

Program code:

<Span style = "color: #000000;"> // call the init function CCLayer: init () of the parent class first; // create the texture CCTexture2D * texture = CCTextureCache :: sharedTextureCache ()-> addImage ("aaaa.png"); // create the sprite CCSprite * sprite = CCSprite: createWithTexture (texture) through textures ); // Add the genie to the scene addChild (sprite); // obtain the window size CCSize winSize = CCDirector: shareddire()-> getWinSize (); // set the position of the display genie (set to display the genie in the middle of the scene) sprite-> setPosition (CCPoint (winSize. width/2, winSize. height/2); return true; </span>


Put the code in the bool Sprite: init () under Sprite. cpp to compile and run the program. The execution result is as follows:



3. Create an genie through the genie Frame

Process: Call CCLayer: init () -> Create a texture-> get the texture size-> Create a Sprite frame-> Create a Sprite-> Add the sprite to the scene-> get the size of the window->> set the position where the genie is displayed in the scene

Program code:

// Call the init function of the parent class CCLayer: init (); // create the texture CCTexture2D * texture = CCTextureCache: sharedTextureCache ()-> addImage ("aaaa.png "); // get the texture size CCSize size = texture-> getContentSize (); // create the sprite frame CCSpriteFrame * frame = CCSpriteFrame: createWithTexture (texture, CCRectMake (size. width/4, size. height/4, size. width/2, size. height/2); // create the sprite CCSprite * sprite = CCSprite: createWithSpriteFrame (frame); // Add the sprite to the scene addChild (sprite ); // obtain the window size. CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the position of the display genie (set to display the genie in the middle of the scene) sprite-> setPosition (CCPoint (winSize. width/2, winSize. height/2); return true


Put the code in the bool Sprite: init () under Sprite. cpp to compile and run the program. The execution result is as follows:


4. Create an genie through the sprite frame Cache

Process: Call CCLayer: init () -> get the size of the window-> Create a texture-> get the texture size-> Create a Sprite frame through a texture-> Create a Sprite frame cache through a Sprite frame-> create a genie through the sprite frame cache-> Add the genie to the scene-> get the size of the window-> set the position displayed in the sprite scene

Program code:

// Call the init function of the parent class CCLayer: init (); // obtain the window size CCSize winSize = CCDirector: shareddire()-> getWinSize (); // create texture CCTexture2D * texture = CCTextureCache: sharedTextureCache ()-> addImage ("aaaa.png"); // get the sprite size CCSize size = texture-> getContentSize (); // create the genie frame CCSpriteFrame * frame = CCSpriteFrame: createWithTexture (texture, CCRectMake (size. width/4, size. height/4, size. width/2, size. height/2); // create the sprite frame cache CCSpriteFrameCache: sharedSpriteFrameCache ()-> addSpriteFrame (frame, "aaaa.png"); // create the sprite CCSprite * sprite = CCSprite :: createWithSpriteFrameName ("aaaa.png"); // set the sprite position sprite-> setPosition (CCPoint (winSize. width/2, winSize. height/2); // Add the sprite to the scene addChild (sprite); // set the position of the display sprite (set to display the sprite in the middle of the scene) sprite-> setPosition (CCPoint (winSize. width/2, winSize. height/2); return true;

Put the code in the bool Sprite: init () under Sprite. cpp to compile and run the program. The execution result is as follows:




How to Use the YY channel creation wizard

The YY channel creation wizard can quickly create a YY channel according to the TXT document and adjust the frequency to quickly create a completely automatic

Cocos2d-x create Genie, I now have a picture, how to set this picture to Genie, then locate on canvas

With a cocos2d-html5?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.