Cocos2d-x Study Notes (14) -- CCTextureCache

Source: Internet
Author: User

Here I will explain CCTextureCache based on a piece of code. The Code is as follows:
...........................
CCSprite * sprite = CCSprite: spriteWithFile ("player.png ");
AddChild (sprite );
......................
This is a commonly used loading wizard for beginners. The following is another method:
........
CCTexture2D * cache = CCTextureCache: sharedTextureCache ()-> addImage ("player.png ");
...........
CCSprite * sprite = CCSprite: spriteWithTexture (cache );
AddChild (sprite );
...........
Some people may think that the second method is highly efficient, but it is actually the same, but the second method is more flexible. In the game, if a background needs to be loaded into many genie, if you start loading the genie only when you are cutting into the background (that is, the first way to use it), once the number of genie is too large, the screen will become choppy and players will feel the game card. If we use the second method to load images to CCTextureCache before the scene is cut in, it will become very fast when calling these images, so there will be no choppy phenomenon. Here, it is worth noting that all the methods for loading images are loaded to CCTexureCache as texture, not just CCSprite. Below I will use an example to describe it.
Step 1: Create an cocos2d-win32 project and name it textureCache;
Step 2: Add the following two classes in HelloWorldScene. h:
[Cpp]
// Method 2
Class TextureCache: public CCLayer
{
Protected:
CCTexture2D * m_cache [20];
Public:
TextureCache ();
Void addSprite (CCObject * pSender );
 
};
 
// The first method for loading Images
Class SpriteLayer: public CCLayer
{
Public:
SpriteLayer ();
};

Step 3: Add the following functions to HelloWorldScene. cpp:
[Cpp]
TextureCache: TextureCache ()
{
CCSize size = CCDirector: shareddire()-> getWinSize ();
 
CCLabelTTF * label = CCLabelTTF: labelWithString ("Click", "Arial", 30 );
CCMenuItemLabel * item = CCMenuItemLabel: itemWithLabel (label,
This, menu_selector (TextureCache: addSprite ));
CCMenu * menu = CCMenu: menuWithItem (item );
AddChild (menu, 1 );
Item-> setPosition (ccp (200,150 ));
Menu-> setPosition (ccp (200,150 ));
 
// Load textures

M_cache [0] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player1.png ");
M_cache [1] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player2.png ");
M_cache [2] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player3.png ");
M_cache [3] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player4.png ");
M_cache [4] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player5.png ");
M_cache [5] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player6.png ");
M_cache [6] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player7.png ");
M_cache [7] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player8.png ");
M_cache [8] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player9.png ");
M_cache [9] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player10.png ");
M_cache [10] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player11.png ");
M_cache [11] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player12.png ");
M_cache [12] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player13.png ");
M_cache [13] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player14.png ");
M_cache [14] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player15.png ");
M_cache [15] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player16.png ");
M_cache [16] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player17.png ");
M_cache [17] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player18.png ");
M_cache [18] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player19.png ");
M_cache [19] = CCTextureCache: sharedTextureCache ()-> addImage ("Images/player1_png ");
 
}
 
 
Void TextureCache: addSprite (CCObject * pSender)
{
CCSize size = CCDirector: shareddire()-> getWinSize ();
 
// Add sprites
CCSprite * background = CCSprite: spriteWithFile ("HelloWorld.png ");
Background-> setPosition (ccp (size. width/2, size. height/2 ));
 
CCSprite * s1 = CCSprite: spriteWithTexture (m_cache [0]);
CCSprite * s2 = CCSprite: spriteWithTexture (m_cache [1]);
CCSprite * s3 = CCSprite: spriteWithTexture (m_cache [2]);
CCSprite * s4 = CCSprite: spriteWithTexture (m_cache [3]);
CCSprite * s5 = CCSprite: spriteWithTexture (m_cache [4]);
CCSprite * s6 = CCSprite: spriteWithTexture (m_cache [5]);
CCSprite * s7 = CCSprite: spriteWithTexture (m_cache [6]);
CCSprite * s8 = CCSprite: spriteWithTexture (m_cache [7]);
CCSprite * s9 = CCSprite: spriteWithTexture (m_cache [8]);
CCSprite * s10 = CCSprite: spriteWithTexture (m_cache [9]);
CCSprite * s11 = CCSprite: spriteWithTexture (m_cache [10]);
CCSprite * s12 = CCSprite: spriteWithTexture (m_cache [11]);
CCSprite * s13 = CCSprite: spriteWithTexture (m_cache [12]);
CCSprite * s14 = CCSprite: spriteWithTexture (m_cache [13]);
CCSprite * s15 = CCSprite: spriteWithTexture (m_cache [14]);
CCSprite * s16 = CCSprite: spriteWithTexture (m_cache [15]);
CCSprite * s17 = CCSprite: spriteWithTexture (m_cache [16]);
CCSprite * s18 = CCSprite: spriteWithTexture (m_cache [17]);
CCSprite * s19 = CCSprite: spriteWithTexture (m_cache [18]);
CCSprite * s20 = CCSprite: spriteWithTexture (m_cache [19]);
 
 
S1-> setPosition (ccp (50, 50 ));
S2-> setPosition (ccp (60, 50 ));
S3-> setPosition (ccp (70, 50 ));
S4-> setPosition (ccp (80, 50 ));
S5-> setPosition (ccp (90, 50 ));
S6-> setPosition (ccp (100, 50 ));
S7-> setPosition (ccp (110, 50 ));
S8-> setPosition (ccp (120, 50 ));
S9-> setPosition (ccp (130, 50 ));
S10-> setPosition (ccp (140, 50 ));
S11-> setPosition (ccp (150, 50 ));
S12-> setPosition (ccp (160, 50 ));
S13-> setPosition (ccp (170, 50 ));
S14-> setPosition (ccp (180, 50 ));
S15-> setPosition (ccp (190, 50 ));
S16-> setPosition (ccp (200, 50 ));
S17-> setPosition (ccp (210, 50 ));
S18-> setPosition (ccp (220, 50 ));
S19-> setPosition (ccp (230, 50 ));
S20-> setPosition (ccp (240, 50 ));
 
 
AddChild (s1 );
AddChild (s2 );
AddChild (s3 );
AddChild (s4 );
AddChild (s5 );
AddChild (s6 );
AddChild (s7 );
AddChild (s8 );
AddChild (s9 );
AddChild (s10 );
AddChild (s11 );
AddChild (s12 );
AddChild (s13 );
AddChild (s14 );
AddChild (s15 );
AddChild (s16 );
AddChild (s17 );
AddChild (s18 );
AddChild (s19 );
AddChild (s20 );
 
}

 
[Cpp]
SpriteLayer: SpriteLayer ()
{

 
 
CCSprite * s1 = CCSprite: spriteWithFile ("Images/player1.png ");
CCSprite * s2 = CCSprite: spriteWithFile ("Images/player2.png ");
CCSprite * s3 = CCSprite: spriteWithFile ("Images/player3.png ");
CCSprite * s4 = CCSprite: spriteWithFile ("Images/player4.png ");
CCSprite * s5 = CCSprite: spriteWithFile ("Images/player5.png ");
CCSprite * s6 = CCSprite: spriteWithFile ("Images/player6.png ");
CCSprite * s7 = CCSprite: spriteWithFile ("Images/player7.png ");
CCSprite * s8 = CCSprite: spriteWithFile ("Images/player8.png ");
CCSprite * s9 = CCSprite: spriteWithFile ("Images/player9.png ");
CCSprite * s10 = CCSprite: spriteWithFile ("Images/player10.png ");
CCSprite * s11 = CCSprite: spriteWithFile ("Images/player11.png ");
CCSprite * s12 = CCSprite: spriteWithFile ("Images/player12.png ");
CCSprite * s13 = CCSprite: spriteWithFile ("Images/player13.png ");
CCSprite * s14 = CCSprite: spriteWithFile ("Images/player14.png ");
CCSprite * s15 = CCSprite: spriteWithFile ("Images/player15.png ");
CCSprite * s16 = CCSprite: spriteWithFile ("Images/player16.png ");
CCSprite * s17 = CCSprite: spriteWithFile ("Images/player17.png ");
CCSprite * s18 = CCSprite: spriteWithFile ("Images/player18.png ");
CCSprite * s19 = CCSprite: spriteWithFile ("Images/player19.png ");
CCSprite * s20 = CCSprite: spriteWithFile ("Images/playerw.png ");
 
 
S1-> setPosition (ccp (50, 50 ));
S2-> setPosition (ccp (60, 50 ));
S3-> setPosition (ccp (70, 50 ));
S4-> setPosition (ccp (80, 50 ));
S5-> setPosition (ccp (90, 50 ));
S6-> setPosition (ccp (100, 50 ));
S7-> setPosition (ccp (110, 50 ));
S8-> setPosition (ccp (120, 50 ));
S9-> setPosition (ccp (130, 50 ));
S10-> setPosition (ccp (140, 50 ));
S11-> setPosition (ccp (150, 50 ));
S12-> setPosition (ccp (160, 50 ));
S13-> setPosition (ccp (170, 50 ));
S14-> setPosition (ccp (180, 50 ));
S15-> setPosition (ccp (190, 50 ));
S16-> setPosition (ccp (200, 50 ));
S17-> setPosition (ccp (210, 50 ));
S18-> setPosition (ccp (220, 50 ));
S19-> setPosition (ccp (230, 50 ));
S20-> setPosition (ccp (240, 50 ));
 
 
AddChild (s1 );
AddChild (s2 );
AddChild (s3 );
AddChild (s4 );
AddChild (s5 );
AddChild (s6 );
AddChild (s7 );
AddChild (s8 );
AddChild (s9 );
AddChild (s10 );
AddChild (s11 );
AddChild (s12 );
AddChild (s13 );
AddChild (s14 );
AddChild (s15 );
AddChild (s16 );
AddChild (s17 );
AddChild (s18 );
AddChild (s19 );
AddChild (s20 );
 
}

Modify the HelloWorld: scene function:
[Cpp]
CCScene * HelloWorld: scene ()
{
CCScene * scene = NULL;
Do
{
// 'Scene 'is an autorelease object
Scene = CCScene: node ();
CC_BREAK_IF (! Scene );
 

TextureCache * layer = new TextureCache ();
// SpriteLayer * layer = new SpriteLayer ();
CC_BREAK_IF (! Layer); www.2cto.com
 
// Add layer as a child to scene
Scene-> addChild (layer );
} While (0 );
 
// Return the scene
Return scene;
}

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.