Home Database Mysql Tutorial Cocos2dx 3.0 过渡篇(十)资源加载进度条Loading...

Cocos2dx 3.0 过渡篇(十)资源加载进度条Loading...

Jun 07, 2016 pm 03:42 PM
load resource transition schedule

http://blog.csdn.net/start530/article/details/19420317 本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。 所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺

http://blog.csdn.net/start530/article/details/19420317


本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。

所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺利通过!


首先,我要说的是:这次我要写的主题是进度条。 额,等等,先收起你手里愤怒的西瓜刀。我也才知道TestCpp也有这个例子啊。不过TestCpp里的只有label的变化,而我的多加了个进度条。
请容我对我的这种手段取个好听的名称:画龙点睛!


恩,步骤如下:
1、创建label和progressTimer;
2、加载资源,每加载一张都调用回调函数;
3、加载完成,进入新的界面。



首先看下头文件:HelloWorld.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
	HelloWorld():m_numSp(20),m_loadedSp(0),loadProgress(NULL){};

    static cocos2d::Scene* createScene();
    virtual bool init();  

	void loadingCallback(Object* pSender);//加载一张图片完成后跳转的毁掉函数

	void gotoNewLayer();//加载完后的跳转函数
    CREATE_FUNC(HelloWorld);

private:
	cocos2d::ProgressTimer* loadProgress;//进度条

	cocos2d::LabelTTF* percentLabel;//加载进度label
	cocos2d::LabelTTF* loadLabel;//显示 loading: 的label

	int m_numSp;//要加载的精灵数目,初始化为 20 张
	int m_loadedSp;//已加载的精灵数目
};

#endif // __HELLOWORLD_SCENE_H__
Copy after login

1、创建
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();

loadLabel = LabelTTF::create("Loading:","Arial",20);//创建显示Loading: 的label
loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30));
this->addChild(loadLabel,1);

percentLabel = LabelTTF::create("0%","Arial",20);//创建显示百分比的label
percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30));
this->addChild(percentLabel,2);

auto loadBg = Sprite::create("sliderTrack.png");//进程条的底图
loadBg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(loadBg,1);

loadProgress = ProgressTimer::create(Sprite::create("sliderProgress.png"));//创建一个进程条
loadProgress->setBarChangeRate(Point(1,0));//设置进程条的变化速率
loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型
loadProgress->setMidpoint(Point(0,1));//设置进度的运动方向
loadProgress->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
loadProgress->setPercentage(0.0f);//设置初始值为0
this->addChild(loadProgress,2);
Copy after login

2、加载图片
//加载20张图片,每加载完一张就调用回调函数:loadingCallback
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld1.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld2.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld3.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld4.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld5.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld6.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld7.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));

Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Copy after login

3、图片加载后的回调函数:
void HelloWorld::loadingCallback(Object* pSender)
{
	++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1

	char buf_str[16];
	sprintf(buf_str,"%d%%",(int)(((float)m_loadedSp / m_numSp) * 100),m_numSp);
	percentLabel->setString(buf_str);//更新percentLabel的值

	float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp)/((float)m_numSp/100);//计算进度条当前的百分比
	//因为加载图片速度很快,所以就没有使用ProgressTo,
	//或者ProgressFromTo这种动作来更新进度条
	loadProgress->setPercentage(newPercent);//更新进度条

	//图片加载完成后
	if(m_loadedSp == m_numSp)
	{
		this->removeChild(loadProgress);//将添加的几个对象删除掉
		this->removeChild(percentLabel);
		this->removeChild(loadLabel);

		//加载完既要跳转到gotoNewLayer,在这里可以
		//创建新的Scene,新的Layer,或者其他什么乱七八糟的
		this->gotoNewLayer();
	}
}
Copy after login

4、进入新的界面
void HelloWorld::gotoNewLayer()
{
	auto size = Director::getInstance()->getWinSize();

	auto sp = Sprite::create("HelloWorld.png");//用之前加载到缓存中的图片,创建一个精灵。
	sp->setPosition(Point(size.width/2,size.height/2));
	this->addChild(sp,1);
}
Copy after login


因为代码里注释都写的挺详细的,所以我也就不说太多废话了。
恩,写完了。这篇是下班后加班写的,外面又下了大雨,我要赶紧冲回去吃饭了。风一般的男纸


http://blog.csdn.net/start530?viewmode=contents


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Error loading plugin in Illustrator [Fixed] Error loading plugin in Illustrator [Fixed] Feb 19, 2024 pm 12:00 PM

When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

CSS transition effect: how to achieve the fade-in and fade-out effect of elements CSS transition effect: how to achieve the fade-in and fade-out effect of elements Nov 21, 2023 am 08:03 AM

CSS transition effect: How to achieve the fade-in and fade-out effect of elements Introduction: In web design, making elements have transition effects is one of the important means to improve user experience. The fade-in-fade-out effect is a common and concise transition effect that can make elements appear from scratch, from shallow to deep. This article will introduce how to use CSS to achieve the fade-in and fade-out effect of elements, and give specific code examples. 1. Use the transition attribute to achieve the fade-in and fade-out effect of the element. The transition attribute of CSS can add elements to the element.

Stremio subtitles not working; error loading subtitles Stremio subtitles not working; error loading subtitles Feb 24, 2024 am 09:50 AM

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

PHP implements infinite scroll loading PHP implements infinite scroll loading Jun 22, 2023 am 08:30 AM

With the development of the Internet, more and more web pages need to support scrolling loading, and infinite scrolling loading is one of them. It allows the page to continuously load new content, allowing users to browse the web more smoothly. In this article, we will introduce how to implement infinite scroll loading using PHP. 1. What is infinite scroll loading? Infinite scroll loading is a method of loading web content based on scroll bars. Its principle is that when the user scrolls to the bottom of the page, background data is asynchronously retrieved through AJAX to continuously load new content. This kind of loading method

How to find resources on 115 network disk How to find resources on 115 network disk Feb 23, 2024 pm 05:10 PM

There will be a lot of resources in the 115 network disk, so how to find resources? Users can search for the resources they need in the software, then enter the download interface, and then choose to save to the network disk. This introduction to the method of finding resources on 115 network disk can tell you the specific content. The following is a detailed introduction, come and take a look. How to find resources on 115 network disk? Answer: Search the content in the software, and then click to save to the network disk. Detailed introduction: 1. First enter the resources you want in the app. 2. Then click the keyword link that appears. 3. Then enter the download interface. 4. Click Save to network disk inside.

How to solve the problem that css cannot be loaded How to solve the problem that css cannot be loaded Oct 20, 2023 am 11:29 AM

The solutions to the problem that CSS cannot be loaded include checking the file path, checking the file content, clearing the browser cache, checking the server settings, using developer tools and checking the network connection. Detailed introduction: 1. Check the file path. First, please make sure the path of the CSS file is correct. If the CSS file is located in a different part or subdirectory of the website, you need to provide the correct path. If the CSS file is located in the root directory, the path should be direct. ; 2. Check the file content. If the path is correct, the problem may lie in the CSS file itself. Open the CSS file to check, etc.

Outlook freezes when inserting hyperlink Outlook freezes when inserting hyperlink Feb 19, 2024 pm 03:00 PM

If you encounter freezing issues when inserting hyperlinks into Outlook, it may be due to unstable network connections, old Outlook versions, interference from antivirus software, or add-in conflicts. These factors may cause Outlook to fail to handle hyperlink operations properly. Fix Outlook freezes when inserting hyperlinks Use the following fixes to fix Outlook freezes when inserting hyperlinks: Check installed add-ins Update Outlook Temporarily disable your antivirus software and then try creating a new user profile Fix Office apps Program Uninstall and reinstall Office Let’s get started. 1] Check the installed add-ins. It may be that an add-in installed in Outlook is causing the problem.

Why did Han Xiaoquan suddenly have no resources? Why did Han Xiaoquan suddenly have no resources? Feb 24, 2024 pm 03:22 PM

Han Xiaoquan is a software that can watch many Korean dramas, so why is there suddenly no resource? This software may have no resources due to network problems, version problems, or copyright issues. This article about the reason why Han Xiaoquan suddenly has no resources can tell you the specific content. The following is a detailed introduction, come and take a look. Why did Han Xiaoquan suddenly have no resources? Answer: Due to network problems, version problems, and copyright issues, detailed introduction: 1. Solution to network problems: You can choose a different network, and then log in to the software again to try. 2. Solution to version problems: Users can download the latest version of this software from the official website. 3. Solutions to copyright issues: Some Korean dramas are removed from the shelves due to copyright issues. You can choose other Korean dramas to watch.

See all articles