Understanding of cocos2d-x ios game development (5) CCsprite genie, cocos2d-xccsprite
This time I will write several types created by the genie:
1. Create a file:
Add the following code on the original basis:
// 1. Create an genie through a file
CCSprite * bg = CCSprite: create ("map.png ");
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // obtain the screen size.
Bg-> setPosition (ccp (winSize. width/2, winSize. height/2 ));
This-> addChild (bg );
Return true;
Run:
The background image is added ..
2. Create a frame:
// 2. Create a frame
CCSpriteFrame * frame = CCSpriteFrame: create ("Peashooter1.tiff", CCRectMake ));
CCSprite * plant1 = CCSprite: createWithSpriteFrame (frame );
Plant1-> setPosition (ccp (400,400 ));
This-> addChild (plant1 );
Return true;
Run:
You can see that there is a pea on the lawn of the screen...
3. Create a texture:
// 3. Create a texture
CCImage * image = newCCImage ();
Image-> autorelease ();
Image-> initWithImageFile ("Peashooter1.tiff ");
// Create an OpenGL2d texture image from an image, text, or raw data
CCTexture2D * texture = newCCTexture2D ();
Texture-> autorelease ();
Texture-> initWithImage (image );
CCSprite * plant2 = CCSprite: createWithTexture (texture );
Plant2-> setPosition (ccp (500,500 ));
This-> addChild (plant2 );
Return true;
You can see that there is an extra small pea on the background image ..
4. Create an genie through the cache of the sprite frame:
Before creating the software, you must first download Zwoptex. Its function is to combine multiple types of images into one image and a plist file. Open the software and click creat.
Import some images:
Click Layout to automatically Layout
Change size:
Click Publish Settings under file to set the output path and file name.
Click "Done" to save it to the desktop, and then add the two files to the project.
Write code:
// 4. Frame cache Creation
CCSpriteFrameCache: sharedSpriteFrameCache ()-> addSpriteFramesWithFile ("Person. plist ");
CCSpriteFrame * frameCashe = CCSpriteFrameCache: sharedSpriteFrameCache ()-> spriteFrameByName (" .png"); // create a frame
CCSprite * plant3 = CCSprite: createWithSpriteFrame (frameCashe );
Plant3-> setPosition (ccp (600,600 ));
This-> addChild (plant3 );
Return true;
This method is often used to create Genie...
Run:
You can see one more image... This is the most basic creation method created by the genie.
How to Create a CCSprite-inherited class for cocos2d game development and use this class in projects
Create a new file and select CCNode class-based
Then select subclass of CCSprite, And the created class is based on CCSprite. You can directly use this class in the project.
Hope to help you.
When the cocos2d-x-214 uses CCSpriteFrameCache, how should we initialize CCSprite * sprite?
CCSprite * sprite = CCSprite. createWithSpriteFrameName ("grossini_dance_01.png") sprite-> setPosition (CPC (240,160); addChild (sprite );