第八章动作和动画-间隔动作
来源:互联网 发布:网络借贷细则 编辑:程序博客网 时间:2024/05/23 02:26
Cocos2d-x学习笔记
间隔动作
间隔动作执行完成需要一定的事件,可以设置duration属性来设置动作的执行时间。间隔动作的基类是ActionInterval。
实例
HelloWorld.h文件
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "SettingScene.h"//枚举类型作为标签typedef enum{ kMoveTo = 100, kMoveBy, kJumpTo, kJumpBy, kBezierBy, kScaleTo, kScaleBy, kRotateTo, kRotateBy, kBlink, kTintTo, kTintBy, kFadeTo, kFadeIn, kFadeOut}ActionTypes;class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void onClickMenu(cocos2d::Ref * pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp文件
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){ // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// //背景 auto bg = Sprite::create("bg.png"); bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); this->addChild(bg); //XxxTo是运动到指定位置,XxxBy是运动到相对位置 auto pItemLabel1 = Label::createWithBMFont("fonts/fnt2.fnt", "MoveTo"); auto pItemMenu1 = MenuItemLabel::create(pItemLabel1, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu1->setTag(kMoveTo); auto pItemLabel2 = Label::createWithBMFont("fonts/fnt2.fnt", "MoveBy"); auto pItemMenu2 = MenuItemLabel::create(pItemLabel2, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu2->setTag(kMoveBy); auto pItemLabel3 = Label::createWithBMFont("fonts/fnt2.fnt", "JumpTo"); auto pItemMenu3 = MenuItemLabel::create(pItemLabel3, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu3->setTag(kJumpTo); auto pItemLabel4 = Label::createWithBMFont("fonts/fnt2.fnt", "JumpBy"); auto pItemMenu4 = MenuItemLabel::create(pItemLabel4, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu4->setTag(kJumpBy); auto pItemLabel5 = Label::createWithBMFont("fonts/fnt2.fnt", "BezierBy"); auto pItemMenu5 = MenuItemLabel::create(pItemLabel5, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu5->setTag(kBezierBy); auto pItemLabel6 = Label::createWithBMFont("fonts/fnt2.fnt", "ScaleTo"); auto pItemMenu6 = MenuItemLabel::create(pItemLabel6, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu6->setTag(kScaleTo); auto pItemLabel7 = Label::createWithBMFont("fonts/fnt2.fnt", "ScaleBy"); auto pItemMenu7 = MenuItemLabel::create(pItemLabel7, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu7->setTag(kScaleBy); auto pItemLabel8 = Label::createWithBMFont("fonts/fnt2.fnt", "RotateTo"); auto pItemMenu8 = MenuItemLabel::create(pItemLabel8, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu8->setTag(kRotateTo); auto pItemLabel9 = Label::createWithBMFont("fonts/fnt2.fnt", "RotateBy"); auto pItemMenu9 = MenuItemLabel::create(pItemLabel9, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu9->setTag(kRotateBy); auto pItemLabel10 = Label::createWithBMFont("fonts/fnt2.fnt", "Blink"); auto pItemMenu10 = MenuItemLabel::create(pItemLabel10, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu10->setTag(kBlink); auto pItemLabel11 = Label::createWithBMFont("fonts/fnt2.fnt", "TintTo"); auto pItemMenu11 = MenuItemLabel::create(pItemLabel11, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu11->setTag(kTintTo); auto pItemLabel12 = Label::createWithBMFont("fonts/fnt2.fnt", "TintBy"); auto pItemMenu12 = MenuItemLabel::create(pItemLabel12, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu12->setTag(kTintBy); auto pItemLabel13 = Label::createWithBMFont("fonts/fnt2.fnt", "FadeTo"); auto pItemMenu13 = MenuItemLabel::create(pItemLabel13, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu13->setTag(kFadeTo); auto pItemLabel14 = Label::createWithBMFont("fonts/fnt2.fnt", "FadeIn"); auto pItemMenu14 = MenuItemLabel::create(pItemLabel14, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu14->setTag(kFadeIn); auto pItemLabel15 = Label::createWithBMFont("fonts/fnt2.fnt", "FadeOut"); auto pItemMenu15 = MenuItemLabel::create(pItemLabel15, CC_CALLBACK_1(HelloWorld::onClickMenu, this)); pItemMenu15->setTag(kFadeOut); auto mn = Menu::create(pItemMenu1, pItemMenu2, pItemMenu3, pItemMenu4, pItemMenu5, pItemMenu6, pItemMenu7, pItemMenu8, pItemMenu9, pItemMenu10, pItemMenu11, pItemMenu12, pItemMenu13, pItemMenu14, pItemMenu15, NULL); mn->alignItemsInColumns(3, 3, 3, 3, 3, NULL); this->addChild(mn); return true;}void HelloWorld::onClickMenu(Ref * pSender){ MenuItem * nmitem = (MenuItem *)pSender; //不使用Setting::createScene()创建,因为这样不能传递参数 auto sc = Scene::create(); auto layer = Setting::create(); layer->setTag(nmitem->getTag()); sc->addChild(layer); auto reScene = TransitionSlideInR::create(1.0f, sc); Director::getInstance()->replaceScene(reScene);}
Setting.h文件
#ifndef __SETTING_SCENE_H__#define __SETTING_SCENE_H__#include "cocos2d.h"#include "HelloWorldScene.h"class Setting : public cocos2d::Layer{ cocos2d::Sprite * sprite;public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void goMenu(cocos2d::Ref * pSender); void backMenu(cocos2d::Ref * pSender); // implement the "static create()" method manually CREATE_FUNC(Setting);};#endif // __SETTINGSCENE_SCENE_H__
Setting.cpp文件
#include "SettingScene.h"USING_NS_CC;Scene* Setting::createScene(){ // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = Setting::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}bool Setting::init(){ ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// //背景 auto bg = Sprite::create("Background800x480.png"); bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(bg); sprite = Sprite::create("hero.png"); sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(sprite); auto backMenuItem = MenuItemImage::create("Back-up.png", "Back-down.png", CC_CALLBACK_1(Setting::backMenu, this)); backMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(120, 100))); auto goMenuItem = MenuItemImage::create("Go-up.png", "Go-down.png", CC_CALLBACK_1(Setting::goMenu, this)); goMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(visibleSize.width / 2, 100))); Menu * mn = Menu::create(backMenuItem, goMenuItem, NULL); mn->setPosition(Vec2::ZERO); this->addChild(mn); return true;}void Setting::backMenu(cocos2d::Ref * pSender){ auto sc = HelloWorld::createScene(); auto reScene = TransitionSlideInL::create(1.0f, sc); Director::getInstance()->replaceScene(reScene);}void Setting::goMenu(cocos2d::Ref * pSender){ Size size = Director::getInstance()->getVisibleSize(); Vec2 p = Vec2(CCRANDOM_0_1() * size.width, CCRANDOM_0_1() * size.height); ccBezierConfig bezier;//贝塞尔曲线结构体 switch (this->getTag()) { //MoveTo,MoveBy是移动函数 case kMoveTo: sprite->runAction(MoveTo::create(2, Vec2(size.width - 50, size.height - 50))); break; case kMoveBy: sprite->runAction(MoveBy::create(2, Vec2(-50, -50))); break; //JumpTo,JumpBy是跳动函数,第一个参数是持续的时间(s),第二个参数是跳动到的位置,第三个参数是跳动的高度,第四个参数是跳动的次数 case kJumpTo: sprite->runAction(JumpTo::create(2, Vec2(150, 50), 30, 5)); break; case kJumpBy: sprite->runAction(JumpBy::create(2, Vec2(100, 100), 30, 5)); break; //贝赛尔曲线 case kBezierBy: bezier.controlPoint_1 = Vec2(0, size.height / 2);//第一个控制点 bezier.controlPoint_2 = Vec2(300, -size.width / 2);//第二个控制点 bezier.endPosition = Vec2(100, 100);//结束点 break; //缩放动作 case kScaleTo: sprite->runAction(ScaleTo::create(2, 0.5));//(时间,缩放比例) break; case kScaleBy: sprite->runAction(ScaleBy::create(2, 0.5));//(时间,缩放比例) break; //旋转动作函数 case kRotateTo: sprite->runAction(RotateTo::create(2, 180));//(时间,旋转角度) break; case kRotateBy: sprite->runAction(RotateBy::create(2, -180));//(时间,旋转角度) break; //闪烁动作函数 case kBlink: sprite->runAction(Blink::create(3, 5));//(时间,闪烁次数) break; //染色动作函数 case kTintTo: sprite->runAction(TintTo::create(2, 255, 0, 0));//(时间, R, G, B) break; case kTintBy: sprite->runAction(TintBy::create(0.5, 0, 255, 255));//(时间, R, G, B) break; //不透明度变换函数 case kFadeTo: sprite->runAction(FadeTo::create(1, 80));//(时间,不透明度)80% break; //淡入淡出函数0~255,0为完全透明,255为完全不透明 case kFadeIn: sprite->runAction(FadeIn::create(1));// break; case kFadeOut: sprite->runAction(FadeOut::create(1)); break; default: break; }}
0 0
- 第八章动作和动画-间隔动作
- 第八章动作和动画-瞬时动作
- 第八章动作和动画-组合动作
- 第八章动作和动画-动作速度的控制
- 第八章动作和动画-动画
- IOS_Cocos2D_间隔动作_帧动画_纹理相册_进度条_瞬时动作_SpriteBatchNode
- 动作和动画的使用
- Lua_间隔动作
- 间隔动作函数
- cocos2d-x动作、特效和动画(一)移动动作
- cocos2d-x动作、特效和动画(二)缩放动作
- cocos2d-x动作、特效和动画(三)扭曲动作
- 动作,动画
- cocos2d-Lua06动作和动画切换
- Cocos2d-JS 动作、特效和动画
- 第8章动画和封装
- cocos2d-x动作、特效和动画(三)旋转和跳跃动作
- Cocos2d-x 图像渲染和动画——动作
- Kafka学习,官网笔记(1)~
- spring bean的生命周期(4)
- R与ArcGIS连接的R语言包“arcgisbinding”
- java2 day02 注解 Junit
- doker上运行tensorflow
- 第八章动作和动画-间隔动作
- Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架(注解模式哦)
- 很久很久前初学Java时的笔记--日期+集合
- JNI DETECTED ERROR IN APPLICATION: can't call void XXX on instance of java.lang.Class <XXX>解决方案
- spark core 2.0 BlockManager dropFromMemory
- Eclipse报错 "Selection does not contain a main Type"
- dos嵌套调用
- Springboot 之 静态资源路径配置
- Codeforces 755-C. PolandBall and Forest(交互式题)