Cocos2d-x中瞬时动作

前端之家收集整理的这篇文章主要介绍了Cocos2d-x中瞬时动作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

瞬时动作就是不等待立即执行的动作,瞬时动作的基类是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 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;
	}

}
以上代码就完成了瞬时动作中的翻转,隐藏等一系列瞬时动作的实现,具体还是必须要自己操作过后才能体会到 原文链接:https://www.f2er.com/cocos2dx/343786.html

猜你在找的Cocos2d-x相关文章