Cocos Code Research (1) Sprite Learning notes

Source: Internet
Author: User
Tags addchild aliases

Various ways to create sprites and animate
    //Picture Creation method parameter one: Picture resource Path Parameter two: Rect selectionAuto Sprite = Sprite::create ("Images/grossini_dance_atlas.png", Rect (x, Y, -,121));    AddChild (sprite); /************* Ornate split line *************/    //Batchnode-Class texture creation method//parameter one: Texture picture parameter Two: capacity (maximum number of child nodes)Auto Batchnode = Spritebatchnode::create ("Images/grossini_dance_atlas.png", -); //parameter one: Get texture parameter two: Rect selectionAuto Sprite = sprite::createwithtexture (Batchnode->gettexture (), Rect (x, Y, -,121)); Batchnode-AddChild (Sprite);    AddChild (Batchnode); /************* Ornate split line *************/    //Wizard Frame Cache Spriteframecache Creation MethodSpriteframecache::getinstance ()->addspriteframeswithfile ("animations/ghosts.plist","Animations/ghosts.png"); Auto Sprite= Sprite::createwithspriteframename ("Father.gif");    AddChild (sprite); /************* Ornate split line *************/    //Batchnode + Spriteframecache + Sprite combination useSpriteframecache::getinstance ()->addspriteframeswithfile ("animations/ghosts.plist","Animations/ghosts.png"); Auto Batchnode= Spritebatchnode::create ("Animations/ghosts.png"); Auto Sprite= Sprite::createwithspriteframename ("Father.gif"); Batchnode-AddChild (Sprite);    AddChild (Batchnode); /************* Ornate split line *************/    //Texture Creation MethodAuto texture = director::getinstance ()->gettexturecache ()->addimage ("Animations/dragon_animation.png"); Auto Frame= Spriteframe::createwithtexture (texture, Rect ( the*0, the*0, the, the)); Auto Sprite=Sprite::createwithspriteframe (frame); /************* Ornate split line *************/    //Spriteframecache Create animation methodAuto Cache =spriteframecache::getinstance (); Cache->addspriteframeswithfile ("animations/grossini-aliases.plist","Animations/grossini-aliases.png"); Auto Sprite= Sprite::createwithspriteframename ("Grossini_dance_01.png"); Vector<SpriteFrame*> Animframes ( the); Charstr[ -] = {0 };  for(inti =1; I < the; i++) {sprintf (str,"Grossini_dance_%02d.png", i); Auto Frame= cache->getspriteframebyname (str);    Animframes.pushback (frame); } Auto Animation= Animation::createwithspriteframes (Animframes,0.3f); Sprite-runaction (Repeatforever::create (animate::create (animation))); /************* Ornate split line *************/    //Animationcache Loading Animation creating animation methodAuto Animationcache =animationcache::getinstance (); Auto Animation= Animation::createwithspriteframes (Animframes,0.2f); //parameter one: Animation parameter two: binding aliasAnimationcache->addanimation (animation,"Dance_blue"); Auto Dance_blue= Animationcache->getanimation ("Dance_blue"); /************* Ornate split line *************/    //Animationcache loading plist file creation animation methodAuto Framecache =spriteframecache::getinstance (); Framecache->addspriteframeswithfile ("animations/grossini.plist"); Auto Animationcache=animationcache::getinstance (); Animationcache->addanimationswithfile ("animations/animations.plist"); Auto Normal= Animationcache->getanimation ("Dance_1"); /************* Ornate split line *************/    //dynamic Load Texture methodSTD::stringPlist_content;//plist File Data{std::stringFullPath = Fileutils::getinstance ()->fullpathforfilename ("animations/grossini.plist"); Data Data= Fileutils::getinstance ()Getdatafromfile (FullPath); if(!data.isnull ()) Plist_content.assign ( (Const Char*) data.getbytes (), data.getsize ()); } std::stringImage_content;//picture File Data{std::stringFullPath = Fileutils::getinstance ()->fullpathforfilename ("Animations/grossini.png"); Data Data= Fileutils::getinstance ()Getdatafromfile (FullPath); if(!data.isnull ()) Image_content.assign ( (Const Char*) data.getbytes (), data.getsize ());    } image Image; Image.initwithimagedata ((Constuint8_t*) Image_content.c_str (), image_content.size ()); Texture2d* Texture =New(Std::nothrow) texture2d (); Texture->initwithimage (&image); Texture-autorelease (); Auto Cache=spriteframecache::getinstance (); Cache->addspriteframeswithfilecontent (plist_content, texture);
Common API Introduction

Animationcache class

Added to the cache by name and animation.
void Addanimation (Animation *animation, const std::string &name)

Add an animation from the plist file to make sure that the Spriteframecache frame has been loaded before .
void Addanimationswithfile (const std::string &plist)

Returns an animation that was previously added. If it is not found, it will return null.
Animation * getanimation (const std::string &name)

Deletes an animation from the cache.
void Removeanimation (const std::string &name)

Clears the cache, which releases all animated objects and shared instances.
static void Destroyinstance ()

Spriteframecache class

Adding multiple spriteframe from a plist file automatically loads the corresponding texture. When a texture file is not specified in the plist file, the extension of the plist file is replaced with a. png name for the texture file.
void Addspriteframeswithfile (const std::string &plist)

Loads a spriteframe with a texture file using the specified plist file.
void Addspriteframeswithfile (const std::string& plist,const std::string& texturefilename)

Add a spriteframe and specify its name.
void Addspriteframe (Spriteframe *frame, const std::string &framename)

Gets the spriteframe of the specified name.
Spriteframe * Getspriteframebyname (const std::string &name)

Check if a plist file has been loaded.
BOOL Isspriteframeswithfileloaded (const std::string &plist) const

Cleans up the loaded spriteframe.
void Removespriteframes ()

Removes a spriteframe from the cache based on the specified name.
void Removespriteframebyname (const std::string &name)

Frees unused spriteframe. Spriteframe with a reference count of 1 will be deleted. When entering a new scene, you can easily release the spriteframe that you no longer use by calling this method.
void Removeunusedspriteframes ()

Removes the spriteframe from the cache in the specified plist file
void Removespriteframesfromfile (const std::string &plist)

Destroys an instance of Spriteframecache. All instances of Spriteframe and retain are released.
static void Destroyinstance ()

Cocos Code Research (1) Sprite Learning notes

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.