Cocos2d-X中的动作展示《二》

Cocos2d-X中的动作展示《二》,第1张

概述由于Cocos2d-X中的动作较多,我将所有的动作制作成了一个滚动视图,每个滚动视图上都有动作名,单击滚动视图就可以展示相应的动作 程序效果图: 使用滚动视图实现动作切换 动作展示 首先创建一个ActionMore类 ActionMore.h中的代码 #ifndef _ActionMore_H_#define _ActionMore_H_#include "cocos2d.h" #inc 由于Cocos2d-X中的动作较多,我将所有的动作制作成了一个滚动视图,每个滚动视图上都有动作名,单击滚动视图就可以展示相应的动作

程序效果图:

使用滚动视图实现动作切换


动作展示


首先创建一个ActionMore类

ActionMore.h中的代码

#ifndef _ActionMore_H_#define _ActionMore_H_#include "cocos2d.h"  #include "cocos-ext.h"  USING_NS_CC;  USING_NS_CC_EXT;class ActionMore : public cclayer{public:    static CCScene* scene();    bool init();    CREATE_FUNC(ActionMore);        bool cctouchBegan(CCtouch*,CCEvent*);	voID cctouchended(CCtouch*,CCEvent*);	CCNode* _c;	voID testAction(int IDx,cclayercolor*);};#endif

ActionMore.cpp中的代码

#include "ActionMore.h"static const char* _actionname[] ={	"CCEaseBounceIn","CCEaseBounceOut","CCEaseBounceInOut","CCEaseBackIn","CCEaseBackOut","CCEaseBackInOut","CCEaseElasticIn","CCEaseElasticOut","CCEaseElasticInOut","CCEaseExponentialin","CCEaseExponentialOut","CCEaseExponentialinOut","CCEaseIn","CCEaSEOut","CCEaseInOut","CCEaseSineIn","CCEaseSineOut","CCEaseSineInOut","CCSpeed","CCSpawn","CCSequence","CCRepeat","CCRepeatForever"};CCScene* ActionMore::scene(){    CCScene* scene = CCScene::create();    ActionMore* layer = ActionMore::create();    scene->addChild(layer);    return scene;}bool ActionMore::init(){    cclayer::init();        CCSize winSize = CCDirector::sharedDirector()->getWinSize();    //创建Node结点用于ScrollVIEw    CCNode* c = CCNode::create();	_c = c;    //ScrollVIEw中展示的动作的个数	int actionCount = sizeof(_actionname) / sizeof(*_actionname);	    //使用for循环创建视图用于展示动作	for (int i = 0; i < actionCount; i++)	{		cclayercolor* layer;		        //        if (i % 2 == 0)		{            //创建带有颜色的背景层(背景层的颜色为深灰色)			layer = cclayercolor::create(ccc4(192,192,255),winSize.wIDth,winSize.height);		}		else		{            //创建带有颜色的背景层(背景层的颜色为浅灰色)			layer = cclayercolor::create(ccc4(128,128,winSize.height);		}		c->addChild(layer);		layer->setposition(ccp(i*winSize.wIDth,0));        //保存动作的名字		const char* Title = _actionname[i];        //创建标签用于显示动作的名字        cclabelTTF* label = cclabelTTF::create(Title,"Arial",36);		layer->addChild(label);        //设置标签的位置		label->setposition(ccp(winSize.wIDth / 2,winSize.height - 80));	}    //创建滚动视图	CCScrollVIEw* vIEw = CCScrollVIEw::create(winSize,c);	    //设置滚动视图的滚动方向为水平滚动    vIEw->setDirection(kCCScrollVIEwDirectionHorizontal);    //设置滚动视图的大小	vIEw->setContentSize(CCSize(winSize.wIDth*actionCount,winSize.height));	addChild(vIEw);	//能触摸	settouchEnabled(true);	settouchMode(kCCtouchesOneByOne);	return true;}bool ActionMore::cctouchBegan(CCtouch*,CCEvent*){	return true;}voID ActionMore::testAction(int IDx,cclayercolor* layer){	CCSize winSize = CCDirector::sharedDirector()->getWinSize();	    //得到用户创建的精灵    CCSprite* sprite = (CCSprite*)layer->getUserObject();    //当没有精灵的时候	if (sprite == NulL)	{       //创建一个新的精灵		sprite = CCSprite::create("Closenormal.png");		layer->addChild(sprite);        //设置精灵的关联对象        layer->setUserObject(sprite);	}    //保存用户选择的动作	const char* an = _actionname[IDx];	    //动作类    CCAction* action;	    //设置精灵的位置    sprite->setposition(ccp(winSize.wIDth / 2,winSize.height / 2));    CCMoveBy* moveBy = CCMoveBy::create(4,ccp(0,sprite->getContentSize().height / 2 - winSize.height / 2));    action= NulL;	if (an == "CCEaseBounceIn")//让目标动作具有反d效果,从起点反d	{		action = CCEaseBounceIn::create(moveBy);	}	if (an == "CCEaseBounceOut")//让目标动作具有反d效果,从终点反d	{		action = CCEaseBounceOut::create(moveBy);	}	if (an == "CCEaseBounceInOut")//让目标动作具有反d效果,起点终点都反d	{        action = CCEaseBounceInOut::create(moveBy);	}	if (an == "CCEaseBackIn")//让目标动作具有回力效果,起点作为回力点	{		action = CCEaseBackIn::create(moveBy);	}	if (an == "CCEaseBackOut")//让目标动作具有回力效果,终点作为回力点	{		action = CCEaseBackOut::create(moveBy);	}	if (an == "CCEaseBackInOut")//让目标动作具有回力效果,起点终点都作为回力点	{	}	if (an == "CCEaseElasticIn")//让目标动作具有d性效果,起点具有d性	{		action = CCEaseElasticIn::create(moveBy);	}	if (an == "CCEaseElasticOut")//让目标动作具有d性效果,终点具有d性	{		action = CCEaseElasticOut::create(moveBy);	}	if (an == "CCEaseElasticInOut")//让目标动作具有d力效果,起点终点都具有d性	{		action = CCEaseElasticInOut::create(moveBy);	}	if (an == "CCEaseExponentialin")//让目标动作缓慢开始	{		action = CCEaseExponentialin::create(moveBy);	}	if (an == "CCEaseExponentialOut")//让目标动作缓慢结束	{		action = CCEaseExponentialOut::create(moveBy);	}	if (an == "CCEaseExponentialinOut")//让目标动作缓慢开始并缓慢结束	{		action = CCEaseExponentialinOut::create(moveBy);	}	if (an == "CCEaseIn")//让目标动作由慢到快(速度线性变化)	{		action = CCEaseIn::create(moveBy,10.0f);	}	if (an == "CCEaSEOut")//让目标动作由快到慢(速度线性变化)	{		action = CCEaSEOut::create(moveBy,10.f);	}	if (an == "CCEaseInOut")//让目标动作由慢到快再到慢(速度线性变化)	{		action = CCEaseInOut::create(moveBy,10.f);	}	if (an == "CCEaseSineIn")//让目标动作由慢到快(速度非线性变化)	{		action = CCEaseSineIn::create(moveBy);	}	if (an == "CCEaseSineOut")//让目标动作由快到慢(速度非线性变化)	{		action = CCEaseSineOut::create(moveBy);	}	if (an == "CCEaseSineInOut")//让目标动作由慢到快再到慢(速度非线性变化)	{		action = CCEaseSineInOut::create(moveBy);	}	if (an == "CCSpeed")//为目标动作速度翻倍(加速)	{		action = CCSpeed::create(moveBy,10);	}	if (action)    {		sprite->runAction(action);    }}voID ActionMore::cctouchended(CCtouch* t,CCEvent*){    //得到按下鼠标时的位置    CCPoint ptStart = t->getStartLocation();	    //得到松开鼠标时的位置    CCPoint ptEnd = t->getLocation();	    //如果两个位置的距离的平方小于或者等于25    if(ptStart.getdistanceSq(ptEnd) <= 25)	{		// click		// 点中了哪个子窗口				// 转换ptStart为ScrollVIEw中的Container的坐标		// 再判断被点击的Layercolor		//将鼠标点下的时候的位置的坐标转换成结点坐标        CCPoint ptInContainer = _c->convertToNodeSpace(ptStart);		        //创建一个数组用于保存Layercolor        CCArray* arr = _c->getChildren();// 所有的layercolor		        //用于寻找点中的Layercolor        for (int i = 0; i < sizeof(_actionname) / sizeof(*_actionname); i++)		{            //			cclayercolor* layer = (cclayercolor*)arr->objectAtIndex(i);			if (layer->boundingBox().containsPoint(ptInContainer))			{				testAction(i,layer);				break;			}		}	}}
              总结       

以上是内存溢出为你收集整理的Cocos2d-X中的动作展示《二》全部内容,希望文章能够帮你解决Cocos2d-X中的动作展示《二》所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1007306.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存