由于Cocos2d-X中的动作较多,我将所有的动作制作成了一个滚动视图,每个滚动视图上都有动作名,单击滚动视图就可以展示相应的动作
程序效果图:
使用滚动视图实现动作切换
动作展示
程序代码:
首先创建一个ActionShow类
在ActionShow.h中的代码
#ifndef _ActionShow_H_ #define _ActionShow_H_ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class ActionShow : public CCLayer { public: static CCScene* scene(); bool init(); CREATE_FUNC(ActionShow); bool ccTouchBegan(CCTouch*,CCEvent*); void ccTouchEnded(CCTouch*,CCEvent*); CCNode* _c; void testAction(int idx,CCLayerColor*); void func(); void funcN(CCNode*); void funcND(CCNode*,void*); }; #endif
ActionShow.cpp中的代码
#include "ActionShow.h" static const char* _actionName[] = { "CCMoveBy","CCMoveTo","CCRotateBy","CCRotateTo","CCScaleBy","CCScaleTo","CCSkewBy","CCSkewTo","CCJumpBy","CCJumpTo","CCBezierBy","CCBezierTo","CCFadeIn","CCFadeOut","CCTintTo","CCTintBy","CCBlink","CCDelayTime","CCOrbitCamera","CCCardinalSplineTo","CCCardinalSplineBy","CCCatmullRomTo","CCCatmullRomBy","CCFollow","CCCallFunc","CCCallFuncN","CCCallFuncND" }; CCScene* ActionShow::scene() { CCScene* scene = CCScene::create(); ActionShow* layer = ActionShow::create(); scene->addChild(layer); return scene; } bool ActionShow::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 ActionShow::ccTouchBegan(CCTouch*,CCEvent*) { return true; } void ActionShow::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)); if (an == "CCMoveTo")//运动 { action = CCMoveTo::create(2,ccp(100,100)); } if (an == "CCMoveBy")//运动 { action = CCMoveBy::create(2,100)); } if (an == "CCRotateBy")//旋转 { action = CCRotateBy::create(2,250); } if (an == "CCRotateTo")//旋转 { action = CCRotateTo::create(2,250); } if (an == "CCScaleBy")//放大或缩小 { //2表示放大两倍 //-2表示缩小两倍 action = CCScaleBy::create(2,2); } if (an == "CCScaleTo")//放大或缩小 { action = CCScaleTo::create(2,-2); } if (an == "CCSkewBy")//扭曲 { //第一个参数:完成扭曲所花的时间 //第二个参数:x轴方向扭曲的值 //第三个参数:y轴方向扭曲的值 action = CCSkewBy::create(2,35,87); } if (an == "CCSkewTo")//扭曲 { action = CCSkewTo::create(2,87); } if (an == "CCJumpBy")//跳跃 { //第一个参数:完成跳跃所花的时间 //第一个参数:跳跃到的位置 //第一个参数:跳跃的高度 //第一个参数:跳跃的次数 action = CCJumpBy::create(3,100),50,6); } if (an == "CCJumpTo")//跳跃 { action = CCJumpTo::create(3,6); } if (an == "CCBezierBy")//贝塞尔曲线(相对) { //设置控制点 ccBezierConfig c; //控制点1 c.controlPoint_1 = ccp(400,400); //控制点2 c.controlPoint_2 = ccp(250,360); //终点 c.endPosition = ccp(100,100); //第一个参数:时间 //第二个参数:控制点 action = CCBezierBy::create(3,c); } if (an == "CCBezierTo")//贝塞尔曲线(绝对) { ccBezierConfig c; c.controlPoint_1 = ccp(400,400); c.controlPoint_2 = ccp(250,360); c.endPosition = ccp(100,100); action = CCBezierTo::create(3,c); } if (an == "CCFadeIn")//淡入(通过修改透明度实现) { action = CCFadeIn::create(3); } if (an == "CCFadeOut")//淡出(通过修改透明度实现) { action = CCFadeOut::create(3); } if (an == "CCTintTo")//在精灵上混合一个颜色 { action = CCTintTo::create(3,255,0); } if (an == "CCTintBy")//在精灵上混合一个颜色 { action = CCTintBy::create(3,23,47,37); } if (an == "CCBlink")//精灵闪烁 { action = CCBlink::create(3,10); } if (an == "CCDelayTime")//精灵停顿一会儿 { //2秒钟内精灵移动到(100,100) CCMoveBy* move = CCMoveBy::create(2,100)); //停顿1秒钟 CCDelayTime* delay = CCDelayTime::create(1); //精灵返回 CCFiniteTimeAction* back = move->reverse(); //动作的顺序封装(先执行move后执行delay) action = CCSequence::create(move,delay,back,NULL); } if (an == "CCOrbitCamera")//根据球面坐标轨迹旋转 { //参数1:旋转的时间 //参数2:起始半径 //参数3:半径差 //参数4:起始z角 //参数5:旋转z角 //参数6:起始x角 //参数7:旋转x角 action = CCOrbitCamera::create(5,80,13,55,24); } if (an == "CCCardinalSplineTo")//样条曲线动作 { //创建样条曲线动作 CCPointArray* arr = CCPointArray::create(20); //创建围城曲线框的四个点 arr->addControlPoint(ccp(200,200)); arr->addControlPoint(ccp(200,100)); arr->addControlPoint(ccp(100,200)); action = CCCardinalSplineTo::create(5,arr,5.0f); } if (an == "CCCardinalSplineBy")//样条曲线动作 { CCPointArray* arr = CCPointArray::create(20); arr->addControlPoint(ccp(200-50,200-50)); arr->addControlPoint(ccp(200-50,100-50)); arr->addControlPoint(ccp(100-50,200-50)); action = CCCardinalSplineBy::create(5,5.0f); } if (an == "CCCatmullRomTo")//云性曲线 { CCPointArray* arr = CCPointArray::create(20); arr->addControlPoint(ccp(200,200)); action = CCCatmullRomTo::create(5,arr); } if (an == "CCCatmullRomBy")//云性曲线 { CCPointArray* arr = CCPointArray::create(20); arr->addControlPoint(ccp(200,200)); action = CCCatmullRomBy::create(5,arr); } if (an == "CCFollow")//镜头跟着目标走,超过范围就结束。 { CCSprite* sprite2 = CCSprite::create("green_edit.png"); layer->addChild(sprite2); sprite2->setPosition(ccp(winSize.width / 2,winSize.height / 2)); sprite2->runAction(CCJumpBy::create(100,ccp(800,800),800,60)); action = CCFollow::create(sprite2); } if (an == "CCCallFunc")//创建一个回调动作(不带参数) { action = CCCallFunc::create(this,callfunc_selector(ActionShow::func)); } if (an == "CCCallFuncN")//创建一个回调动作(传调用者为参数) { action = CCCallFuncN::create(this,callfuncN_selector(ActionShow::funcN)); } if (an == "CallFuncND")//创建一个回调动作(带2个参数) { void* p = NULL; action = CCCallFuncND::create(this,callfuncND_selector(ActionShow::funcND),p); } sprite->runAction(action); } void ActionShow::func() { } void ActionShow::funcN(CCNode*) { } void ActionShow::funcND(CCNode*,void*) { } void ActionShow::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; } } } }
原文链接:https://www.f2er.com/cocos2dx/346479.html