Cocos2d-x中瞬时动作

Cocos2d-x中瞬时动作,第1张

概述瞬时动作就是不等待立即执行的动作,瞬时动作的基类是ActionInstant ,具体类图参见cocos2d的文档 我们通过一个实例来学习cocos2d-x中的瞬时动作 首先在HelloWorld.h头文件中添加枚举,用来作为选择的标识 typedef enum ActionTypes { PLACE_TAG = 102, FLIPX_TAG, FLIPY_TAG, HIDE_SHOW_TAG, T

瞬时动作就是不等待立即执行的动作,瞬时动作的基类是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,this));	flipXmenu->setTag(FliPX_TAG);	auto filpYlabel = Label::createWithBMFont("Fonts/fnt2.fnt","FlipY");	auto flipYmenu = MenuItemLabel::create(filpYlabel,this));	flipYmenu->setTag(FliPY_TAG);	auto hIDeLabel = Label::createWithBMFont("Fonts/fnt2.fnt","HIDe and Show");	auto hIDeMenu = MenuItemLabel::create(hIDeLabel,this));;	hIDeMenu->setTag(HIDE_SHOW_TAG);	auto toggleLabel = Label::createWithBMFont("Fonts/fnt2.fnt","toggle");	auto toggleMenu = MenuItemLabel::create(toggleLabel,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 instancebool 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中瞬时动作所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存