温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

cocos2dx实战篇——《萝莉快跑》学习心得

发布时间:2020-04-03 14:23:55 来源:网络 阅读:2047 作者:shahdza 栏目:开发技术

【唠叨】

    源码教程请移步:http://blog.csdn.net/iamlazybone/article/details/19612941

    感谢懒骨头提供了这么多的Demo教程,对于初学者的我而言,帮助真的十分大。

    注:本节仅仅记录博主自身学习《萝莉快跑》的心得体会。


【游戏截图】

cocos2dx实战篇——《萝莉快跑》学习心得




【学习心得】


1、游戏主场景分层

    如果将所有的元素全部都写在一个Game类里面,会很混乱,且改动也很麻烦。

    所以应该对游戏的元素进行分层,如:背景层、人物层、怪物层、道具层等。

    然后再将所有的层放入Game场景中,组合成完整的游戏。

    (1)人物层:GameHero.h

    (2)道具层:GameStar.h

    (3)分数层:GameMark.h

    (4)地图层:GameMap.h


2、更换CCMenuItemImage的图片

//
	sp->setNormalImage(CCSprite::create("sound-off-A.png"));   //正常图片
	sp->setSelectedImage(CCSprite::create("sound-off-B.png")); //选中时图片
//


3、精灵背景更换

//
	jump = CCTextureCache::sharedTextureCache()->addImage("s_jump.png"); //跳
	luoli->setTexture(jump);
//


4、跑动作动画

    使用Animation/Animate动画动作。

//
	CCAnimation* anrun = CCAnimation::create();
	
	anrun->addSpriteFrameWithFileName("s_1.png");
	anrun->addSpriteFrameWithFileName("s_2.png");
	anrun->addSpriteFrameWithFileName("s_3.png");
	anrun->addSpriteFrameWithFileName("s_4.png");
	anrun->addSpriteFrameWithFileName("s_5.png");
	anrun->addSpriteFrameWithFileName("s_6.png");
	anrun->addSpriteFrameWithFileName("s_7.png");
	anrun->addSpriteFrameWithFileName("s_8.png");
	
	anrun->setDelayPerUnit(0.1f);         //帧间隔
	anrun->setRestoreOriginalFrame(true); //是否返回初始帧
	
	CCRepeatForever* acrun = CCRepeatForever::create( CCAnimate::create(anrun) );
	luoli->runAction(acrun);
//


5、Game里地图层的sp元素的坐标

    假设:bg1和sp都是以中心为锚点的,AnchorPoint(0.5,0.5),且sp为bg1的子节点。

    要想知道sp元素在屏幕上的坐标,就需要知道bg1在屏幕上的坐标,以及sp相对bg1的坐标。

    因为sp设置的setPosition是相对其父节点bg1的坐标,而不是在屏幕上的实际坐标

//
	//1.获得地图层坐标,p1是相对Game的坐标
	CCPoint p1 = gamemap->getPosition();
	
	//2.获得sp坐标,psp是相对gamemap的坐标
	CCPoint psp = (gamemap->sp)->getPosition();
	
	//3.计算sp在Game中的坐标
	CCPoint pt = ccp(
		(p1.x - gamemap->getContentSize().width/2)  + psp.x ,
		(p2.y - gamemap->getContentSize().height/2) + psp.y );
//


6、背景滚动

    使用两张背景图片,从右到左滚动,超出屏幕就从新设置位置,继续滚动。

    屏幕大小:480 X 320。

//
	//背景1
	bg1 = CCSprite::create("back_1.png");
	bg1->setPosition(ccp(240,160));
	this->addChild(bg1);

	//背景2
	bg2 = CCSprite::create("back_1.png");
	bg2->setPosition(ccp(720,160));
	this->addChild(bg2);
	

	void GameMap::update(float dt)
	{
		//向左移动2个像素
		bg1->setPosition(bg1->getPosition() - ccp(2, 0));
		bg2->setPosition(bg2->getPosition() - ccp(2, 0));

		//超出屏幕,设置坐标,重新滚动
		if (bg1->getPositionX() <= -240) {
			bg1->setPosition(ccp(720, 160));
			//重新加载背景上的道具
			resetStar(star1); 
		}
		if (bg2->getPositionX() <= -240) {
			bg2->setPosition(ccp(720, 160));
			//重新加载背景上的道具
			resetStar(star2);
		}
	}
//



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI