Coco2d-x implementation of loading interface pre-loading resources

Source: Internet
Author: User

First we define 2 C + + files, one is loadingScene.h, the other is LoadingScene.cpp

First, we're in. h, we define our approach.

#include "Cocos2d.h"

using namespace cocos2d;

Class Loadingscene:public Layer

{

Public

int nuberofloadedres;//record the current progress

Create_func (Loadingscene);

BOOL Init ();

Static Scene *createscene ();

void Loadingresource ();//Load Resources

void Loadingcallback (texture2d*obj);//callback for load resource

void update (float t);

};

It's all defined. H, and then we're going to implement these methods

Inside the. cpp. Because my resources are made from Cocostudio.

That's why we're here to get the stuff that Cocostudio made to cocos2d.

#include "loadingScene.h

#include "cocostudio/cocostudio.h"

#include "SimpleAudioEngie.h"//Here is the header file that defines the sound

Using namespace Cocosdenshion//Here is the header file that defines the sound

using namespace Cocostudio;

Scene *loadingscene::createscene ()

{

Auto Scene=scene::create ();

Auto Layer=loadingscene::create ();

Scene->addchild (layer);

return scene;

}


BOOL Loadingscene::init ()

{

if (! Layer::init ())

{

return false;

}

Display scene Editor's good scene

Auto Node=scenereader::getinstance ()->createnodewithscenefile ("

Publish/loadingscene.json//To load the scene

")

Node->settag (1000);

This->addchild (node);

Set the progress bar first

Node *nodescene=this->getchildbytag (1000);//Scene root node

Comrender *render= (comrender*) (Nodescene->getchildbytag (10003))->getcomponent ("GUIComponent"));

Widget *widget= (cocos2d::ui::widget*) (Render->getnode ());//Get the root node of the UI layer

Loading *progressbar= (loadingbar*) (Widget->getchildbytag (7));

Progressbar->setpercent (0);

Below we need to implement in. H inside the method of loading resources, and then we implement it in the Init () method

Implementing the loading of resources

this->numberofloadedres=0;//First initialize the resource we want to load is 0

Loadingscene::loadingresource ();

This->scheduleupdate ();

return true;


}

void Loadingscene::loadingresource ()

{

Simpleaudioengine::getinstance ()->preloadbackgroundmusic (fileutils::getinstance->

Fullpathforfilenmae ("Name of the game music to be added". C_STR ());

numberofloadedres++;

Load all picture frames

Spriteframecache::getinstance ()->addspriteframeswithfile ("Join plist file");

numberofloadedres++;

Texture Cache--all images can be placed in the texture cache

Director::getinstance ()->gettexturecache ()->addimageasync ("Pictures needed to be added",

Cc_callback_1 (Loadingscene::loadingcallback,this));

}

Load callback for Resource

void Loadingscene::loadingcallback (texture2d * obj)

{

numberofloadedres++

Here we use log to play the resources we loaded have no success

Log ("Resource load:%d", numberofloadedres);


}

Here we update our progress bar

void Loadingscene::update (float t)

{

float percent= (float) numberofloadedres/(how many resources) *100;

Node *nodescene=this->getchildbytag (1000);//The root of the scene

Comrender * render= (comrender*) (Nodescene->getchildbytag (10003)

Getcomponent ("Guicomponent"));

Widget *widget= (cocos2d::ui::widget*) (Render->getnode ());

Loadingbar *progressbar= (loadingbar*) (Widget->getchildbytag (7));

Progressbar->setpercent (percent);

if (numberofloadedres== (with how many resources))

{

Here's what you're going to do when you download the resource, jump to the scene or whatever.

}

}

Forget to tell everyone, the above tag inside the number, if the code is not Settag, those tags are cocostudio objects inside the label.









Coco2d-x implementation of loading interface pre-loading resources

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.