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

因为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, 192, 255), winSize.width, winSize.height);
		}
		else
		{
            //创建带有颜色的背景层(背景层的颜色为浅灰色)
			layer = CCLayerColor::create(ccc4(128, 128, 128, 255), winSize.width, 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")//让目标动作具有反弹效果,从起点反弹
	{
		action = CCEaseBounceIn::create(moveBy);
	}
	if (an == "CCEaseBounceOut")//让目标动作具有反弹效果,从终点反弹
	{
		action = CCEaseBounceOut::create(moveBy);
	}
	if (an == "CCEaseBounceInOut")//让目标动作具有反弹效果,起点终点都反弹
	{
        action = CCEaseBounceInOut::create(moveBy);
	}
	if (an == "CCEaseBackIn")//让目标动作具有回力效果。起点作为回力点
	{
		action = CCEaseBackIn::create(moveBy);
	}
	if (an == "CCEaseBackOut")//让目标动作具有回力效果,终点作为回力点
	{
		action = CCEaseBackOut::create(moveBy);
	}
	if (an == "CCEaseBackInOut")//让目标动作具有回力效果,起点终点都作为回力点
	{
	}
	if (an == "CCEaseElasticIn")//让目标动作具有弹性效果,起点具有弹性
	{
		action = CCEaseElasticIn::create(moveBy);
	}
	if (an == "CCEaseElasticOut")//让目标动作具有弹性效果,终点具有弹性
	{
		action = CCEaseElasticOut::create(moveBy);
	}
	if (an == "CCEaseElasticInOut")//让目标动作具有弹力效果,起点终点都具有弹性
	{
		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;
			}
		}
	}

}



posted on 2017-07-30 17:09  ljbguanli  阅读(234)  评论(0编辑  收藏  举报