Cocos2d-x中瞬时动作
2015-03-26 09:44
267 查看
瞬时动作就是不等待立即执行的动作,瞬时动作的基类是ActionInstant ,具体类图参见cocos2d的文档
我们通过一个实例来学习cocos2d-x中的瞬时动作
首先在HelloWorld.h头文件中添加枚举,用来作为选择的标识
typedef enum ActionTypes
{
PLACE_TAG = 102,
FLIPX_TAG,
FLIPY_TAG,
HIDE_SHOW_TAG,
TOGGLE_TAG
};
然后在init中添加菜单选项,并关联回调函数
bool 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("Background800x480.png");
bg->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height / 2
));
this->addChild(bg);
auto placeLable = Label::createWithBMFont("fonts/fnt2.fnt", "Place");
auto placeMenu = MenuItemLabel::create(placeLable, CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
placeMenu->setTag(PLACE_TAG);
auto flipXlabel = Label::createWithBMFont("fonts/fnt2.fnt", "FlipX");
auto flipXmenu = MenuItemLabel::create(flipXlabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
flipXmenu->setTag(FLIPX_TAG);
auto filpYlabel = Label::createWithBMFont("fonts/fnt2.fnt", "FlipY");
auto flipYmenu = MenuItemLabel::create(filpYlabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
flipYmenu->setTag(FLIPY_TAG);
auto hideLabel = Label::createWithBMFont("fonts/fnt2.fnt", "Hide and Show");
auto hideMenu = MenuItemLabel::create(hideLabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));;
hideMenu->setTag(HIDE_SHOW_TAG);
auto toggleLabel = Label::createWithBMFont("fonts/fnt2.fnt", "toggle");
auto toggleMenu = MenuItemLabel::create(toggleLabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
toggleMenu->setTag(TOGGLE_TAG);
auto mn = Menu::create(placeMenu, flipXmenu, flipYmenu, hideMenu, toggleMenu, NULL);
mn->alignItemsVertically();
this->addChild(mn);
return true;
}
void HelloWorld::OnclickMenu(cocos2d::Ref *pSender){
MenuItem *mnItem = (MenuItem*)pSender;
auto sc = Scene::create();
auto Layer = MyAction::create();
Layer->setTag(mnItem->getTag());
sc->addChild(Layer);
auto reScene = TransitionSlideInR::create(1.0f, sc);
Director::getInstance()->replaceScene(reScene);
}
以上就完成了对菜单选项的设置、
然后我们就需要自定义层MyActionScene文件,并在其中添加MyAction类
#ifndef __MYACTION_SCENE_H__
#define __MYACTION_SCENE_H__
#include "cocos2d.h"
#include "HelloWorldScene.h"
class MyAction : public cocos2d::Layer
{
bool hiddenFlag;
cocos2d::Sprite *sprite;
public:
static cocos2d::Scene* createScene();
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(MyAction);
// a selector callback
void goMenu(cocos2d::Ref* pSender);
void backMenu(cocos2d::Ref* pSender);
};
#endif // __MYACTION_SCENE_H__
#include "MyActionScene.h"
USING_NS_CC;
Scene* MyAction::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = MyAction::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool MyAction::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
auto bg = Sprite::create("Background800x480.png");
bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(bg);
sprite = Sprite::create("Plane.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(MyAction::backMenu, this));
backMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(120, 100)));
auto goMenuItem = MenuItemImage::create("Go-up.png", "Go-down.png", CC_CALLBACK_1(MyAction::goMenu, this));
goMenuItem->setPosition(visibleSize.width / 2, 100);
Menu* mn = Menu::create(backMenuItem, goMenuItem, NULL);
mn->setPosition(Vec2::ZERO);
this->addChild(mn);
this->hiddenFlag = true;//精灵隐藏
return true;
}
void MyAction::backMenu(Ref* pSender)
{
auto sc = HelloWorld::createScene();
auto reScene = TransitionSlideInL::create(1.0f, sc);
Director::getInstance()->replaceScene(reScene);
}
void MyAction::goMenu(Ref* pSender)
{
log("Tag = %i", this->getTag());
Size size = Director::getInstance()->getVisibleSize();
Vec2 p = Vec2(CCRANDOM_0_1() * size.width, CCRANDOM_0_1() * size.height);
switch (this->getTag()) {
case PLACE_TAG:
sprite->runAction(Place::create(p));
break;
case FLIPX_TAG:
sprite->runAction(FlipX::create(true));
break;
case FLIPY_TAG:
sprite->runAction(FlipY::create(true));
break;
case HIDE_SHOW_TAG:
if (hiddenFlag) {
sprite->runAction(Hide::create());
hiddenFlag = false;
}
else {
sprite->runAction(Show::create());
hiddenFlag = true;
}
break;
case TOGGLE_TAG:
sprite->runAction(ToggleVisibility::create());
break;
default:
break;
}
}
以上代码就完成了瞬时动作中的翻转,隐藏等一系列瞬时动作的实现,具体还是必须要自己操作过后才能体会到
我们通过一个实例来学习cocos2d-x中的瞬时动作
首先在HelloWorld.h头文件中添加枚举,用来作为选择的标识
typedef enum ActionTypes
{
PLACE_TAG = 102,
FLIPX_TAG,
FLIPY_TAG,
HIDE_SHOW_TAG,
TOGGLE_TAG
};
然后在init中添加菜单选项,并关联回调函数
bool 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("Background800x480.png");
bg->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height / 2
));
this->addChild(bg);
auto placeLable = Label::createWithBMFont("fonts/fnt2.fnt", "Place");
auto placeMenu = MenuItemLabel::create(placeLable, CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
placeMenu->setTag(PLACE_TAG);
auto flipXlabel = Label::createWithBMFont("fonts/fnt2.fnt", "FlipX");
auto flipXmenu = MenuItemLabel::create(flipXlabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
flipXmenu->setTag(FLIPX_TAG);
auto filpYlabel = Label::createWithBMFont("fonts/fnt2.fnt", "FlipY");
auto flipYmenu = MenuItemLabel::create(filpYlabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
flipYmenu->setTag(FLIPY_TAG);
auto hideLabel = Label::createWithBMFont("fonts/fnt2.fnt", "Hide and Show");
auto hideMenu = MenuItemLabel::create(hideLabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));;
hideMenu->setTag(HIDE_SHOW_TAG);
auto toggleLabel = Label::createWithBMFont("fonts/fnt2.fnt", "toggle");
auto toggleMenu = MenuItemLabel::create(toggleLabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
toggleMenu->setTag(TOGGLE_TAG);
auto mn = Menu::create(placeMenu, flipXmenu, flipYmenu, hideMenu, toggleMenu, NULL);
mn->alignItemsVertically();
this->addChild(mn);
return true;
}
void HelloWorld::OnclickMenu(cocos2d::Ref *pSender){
MenuItem *mnItem = (MenuItem*)pSender;
auto sc = Scene::create();
auto Layer = MyAction::create();
Layer->setTag(mnItem->getTag());
sc->addChild(Layer);
auto reScene = TransitionSlideInR::create(1.0f, sc);
Director::getInstance()->replaceScene(reScene);
}
以上就完成了对菜单选项的设置、
然后我们就需要自定义层MyActionScene文件,并在其中添加MyAction类
#ifndef __MYACTION_SCENE_H__
#define __MYACTION_SCENE_H__
#include "cocos2d.h"
#include "HelloWorldScene.h"
class MyAction : public cocos2d::Layer
{
bool hiddenFlag;
cocos2d::Sprite *sprite;
public:
static cocos2d::Scene* createScene();
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(MyAction);
// a selector callback
void goMenu(cocos2d::Ref* pSender);
void backMenu(cocos2d::Ref* pSender);
};
#endif // __MYACTION_SCENE_H__
#include "MyActionScene.h"
USING_NS_CC;
Scene* MyAction::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = MyAction::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool MyAction::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
auto bg = Sprite::create("Background800x480.png");
bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(bg);
sprite = Sprite::create("Plane.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(MyAction::backMenu, this));
backMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(120, 100)));
auto goMenuItem = MenuItemImage::create("Go-up.png", "Go-down.png", CC_CALLBACK_1(MyAction::goMenu, this));
goMenuItem->setPosition(visibleSize.width / 2, 100);
Menu* mn = Menu::create(backMenuItem, goMenuItem, NULL);
mn->setPosition(Vec2::ZERO);
this->addChild(mn);
this->hiddenFlag = true;//精灵隐藏
return true;
}
void MyAction::backMenu(Ref* pSender)
{
auto sc = HelloWorld::createScene();
auto reScene = TransitionSlideInL::create(1.0f, sc);
Director::getInstance()->replaceScene(reScene);
}
void MyAction::goMenu(Ref* pSender)
{
log("Tag = %i", this->getTag());
Size size = Director::getInstance()->getVisibleSize();
Vec2 p = Vec2(CCRANDOM_0_1() * size.width, CCRANDOM_0_1() * size.height);
switch (this->getTag()) {
case PLACE_TAG:
sprite->runAction(Place::create(p));
break;
case FLIPX_TAG:
sprite->runAction(FlipX::create(true));
break;
case FLIPY_TAG:
sprite->runAction(FlipY::create(true));
break;
case HIDE_SHOW_TAG:
if (hiddenFlag) {
sprite->runAction(Hide::create());
hiddenFlag = false;
}
else {
sprite->runAction(Show::create());
hiddenFlag = true;
}
break;
case TOGGLE_TAG:
sprite->runAction(ToggleVisibility::create());
break;
default:
break;
}
}
以上代码就完成了瞬时动作中的翻转,隐藏等一系列瞬时动作的实现,具体还是必须要自己操作过后才能体会到
相关文章推荐
- cocos2d-x 瞬时动作
- Cocos2d-x 动作之瞬时动作
- Cocos2D-X2.2.3学习笔记12(瞬时动作)
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-瞬时动作
- COCOS2D-X 动作类 瞬时动作CCActionInstant
- cocos2d-android基础——瞬时动作和延时动作
- cocos2d之动作概要、瞬时动作
- iOS开发篇——cocos2d-x 瞬时动作延时动作
- cocos2d-x基础知识(二)瞬时动作/延时动作
- cocos2d-x-lua:API-瞬时动作
- cocos2d-x 瞬时动作/延时动作
- cocos2d 瞬时动作
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-瞬时动作
- IOS_Cocos2D_间隔动作_帧动画_纹理相册_进度条_瞬时动作_SpriteBatchNode
- cocos2d-x基本知识点(6)---动作系列(end)
- cocos2d笔记 (3)cocos2d里的各种动作
- cocos2d-x初探学习笔记(3)--动作(CCAction)
- Cocos2d-x 之复合动作
- cocos2d-x学习笔记10:动作3:补间动作
- cocos2d-x-3.0rc1 学习笔记5 (几个最近用的多的动作) lua 写的