cocos2dx Demo

本文档介绍了使用cocos2d-x进行游戏开发时,如何创建加载界面、进度条、场景切换及相关的游戏元素。强调了在loading界面中预先加载资源以提升游戏性能的重要性,并提供了部分代码示例。
摘要由CSDN通过智能技术生成

其实一直蛮想写一个做项目的博文教程,但目前觉得自己能力还不够,最近公司的项目又比较紧,一直都抽不出时间来写关于项目的博文(明天我要上班呢\(^o^)/~)。对于新手朋友(其实我也是新手lol) 们来说,我觉得动手写东西是一件提高水平的最好办法了,有很多功能,总觉得自己有想法,很容易实现,但真正要实现起来会碰到各种各样的问题,这些问题都是你之前没有想到的,所以我在这里还是建议新手朋友们一定要多多动手练习,只有在动手练习中才能真正的提高自己的能力。

关于cocos2d-x的基础知识,网上已经有一大堆博文了,而且那些大哥大姐们都写得非常好,所以我也不需要重复造轮子了,我尽量能让这篇博文能让大家学习到在其他博文里学不到的东西,这也是我写这篇博文的初衷,本博文涉及到内容还蛮多的,肯定是不能一次性写完,所以只能有时间的时候再更新了。


本博文涉及到的知识:

1.定义场景

2.loading界面

3.菜单回调函数

4.进度条

5.场景切换(特效切换)

6.精灵动画

7.滚动背景

8.声音管理


下面就先上几张效果

图吧:





 (⊙o⊙)… 十一点多了,明天还要上班呢,有时间再更新吧~~(~﹃~)~zZ


2013年7月25日21:58:22

继续

我们先来走一个简单的loading界面吧,l加载资源的操作我们最好都放在loading界面中加载,这样在后续需要资源时可以直接从内存中读取,加快游戏的相应速度,我就不说那么多了,直接上代码吧:

h文件:

include "cocos2d.h"
//#include "SoundManager.h"
#include "Box2D/Box2D.h"
#include "SimpleAudioEngine.h"
//#include "cocos-ext.h"

USING_NS_CC;
//USING_NS_CC_EXT ;

/************************************************************************/
/*
	loading 界面
	 by 深圳-xo
*/
/************************************************************************/
class HelloWorld : public cocos2d::CCLayer
{
public:

    virtual bool init();  

    static cocos2d::CCScene* scene();
    
    void menuCloseCallback(CCObject* pSender);
	
	//进度条
	void progressAnimation();
	void progressIsDone();
private:
	//进度条所需
	CCSprite* loading ;
	CCSprite* loadingbg;
	CCProgressTimer* pro;
	CCProgressTo* to ;
	CCFiniteTimeAction* action;

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {

        CC_BREAK_IF(! CCLayer::init());
		CCSprite* bg = CCSprite::create("menu/flash.png");
		bg->setPosition(CCPointZero);
		bg->setAnchorPoint(CCPointZero);
		this->addChild(bg,-1);
		this->progressAnimation();
        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

/**进度条动画的定义*/
void HelloWorld::progressAnimation()
{

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	loading = CCSprite::create("loading.png");
	pro = CCProgressTimer::create(loading);
//	pro->setScale(0.5f);
	//设置进度条显示样式
	pro->setType(kCCProgressTimerTypeBar);
	pro->setMidpoint(ccp(0,0));
	pro->setBarChangeRate(ccp(1,0));
	to = CCProgressTo::create(1,100);
	action = CCSequence::create(to,CCCallFunc::create(this,callfunc_selector(HelloWorld::progressIsDone)),NULL);
	loadingbg = CCSprite::create("loadbg.png");
//	loadingbg->setScale(0.5f);
	pro->setPosition(ccp(size.width/2,loadingbg->getContentSize().height-20));
	loadingbg->setPosition(ccp(size.width/2,loadingbg->getContentSize().height-20));
	this->addChild(loadingbg);
	this->addChild(pro);
	
	pro->runAction(action);
	
	//schedule(schedule_selector(HelloWorld::progressIsDone),1);
	
}

void HelloWorld::progressIsDone()
{
	if(to->isDone()||action->isDone())
	{
		//场景切换
		CCTransitionScene* mScene = NULL ;
		CCScene* s = MainMenu::scene();
		float t = 1.2f ;
		mScene = CCTransitionJumpZoom::create(t,s);
		CCDirector::sharedDirector()->replaceScene(mScene);
	}
}



















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值