【Cocos2D-X 游戏引擎】初窥门径(1) 制作一个动态的精灵

使用Cocos2d-x 开发3D游戏
原理:
Cocos2D中有个导演控制整个游戏流程,导演将场景添加到屏幕上,场景中有各种各样的演员。

先通过显示一张图片来看看Cocos2D游戏的流程:
AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching()
{
// 初始化导演
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

// 打开FPS
pDirector->setDisplayStats(true);

// 设置FPS the default value is 1.0/60 if you don’t call this
pDirector->setAnimationInterval(1.0 / 60);

// 创建一个场景
CCScene *pScene = GameScence::scene();

// 运行此场景
pDirector->runWithScene(pScene);
return true;

}
上边的代码添加了一个场景GameScence,下面看看具体实现:
GameScence.h
#include “cocos2d.h”
#include “Box2D/Box2D.h”

class GameScence : public cocos2d::CCLayer
{
public :
bool init();
//必须重写scene()
static cocos2d::CCScene* scene();
//相当于create函数,是重写了CCLayer里的create函数
CREATE_FUNC(GameScence);
};
GameScence.cpp
#include “GameScene.h”

using namespace cocos2d;

CCScene* GameScence::scene()
{
CCScene * scene = NULL;
do
{
scene=CCScene::create();

	GameScence* gameScene=GameScence::create();
	scene->addChild(gameScene);
}while(0);

return scene;

};

bool GameScence::init()
{
bool bRet = false;
do
{
//从图片创建一个精灵
CCSprite* pSprite = CCSprite::create(“bg.png”);
//获取屏幕大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
// 设置精灵在场景中的位置,坐标从左下角0,0
pSprite->setPosition(ccp(size.width/2, size.height/2));
// 添加精灵到场景中
this->addChild(pSprite, 0);
}while(0);

bRet=true;

return bRet;

};

代码中有注释,就不在重复叙述了,
下面是效果图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PyPS8XyZ-1618022376569)(https://img-my.csdn.net/uploads/201304/10/1365571606_7425.png)]

怎么让其显示一个动态的精灵呢,下面是修改后的init()
bool GameScene::init()
{
bool bRet = false;
do
{
CCSprite* pMap = CCSprite::create(“bg.png”);
CCSize size = CCDirector::sharedDirector()->getWinSize();
pMap->setPosition(ccp(size.width/2, size.height/2));
this->addChild(pMap, 0);

	CCSprite* sprite ;
	CCArray* pSpriteArray=CCArray::createWithCapacity(4);
	for(int i=0;i<4;i++)
	{
		//截取frame
		CCSpriteFrame* pFrame =CCSpriteFrame::create(“role.png”,CCRectMake(i*82,62*2,82,62));
		pSpriteArray->addObject(pFrame);

		//将精灵添加到场景,默认第一帧图片
		if(i==0){
			sprite= CCSprite::createWithSpriteFrame(pFrame);
			sprite->setPosition(ccp(200,100));
			addChild(sprite);
		}
	}
	//从array创建动画
	CCAnimation *splitAnimation=CCAnimation::createWithSpriteFrames(pSpriteArray,0.1f);
	sprite->runAction(CCRepeatForever::create(CCAnimate::create(splitAnimation)));

}while(0);

bRet=true;

return bRet;

};

使用Cocos2d-x 开发3D游戏

制作动态的图片麻烦,就直接贴图了:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rg5sBSrZ-1618022376571)(https://img-my.csdn.net/uploads/201304/10/1365583756_4857.png)]

转载请注明出处:http://blog.csdn.net/Vestigge

文章来源:http://blog.csdn.net/pomme_qixiaohu/article/details/8782334

TP:7a79a42a

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值