Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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;
	}

}
以上代码就完成了瞬时动作中的翻转,隐藏等一系列瞬时动作的实现,具体还是必须要自己操作过后才能体会到

大佬总结

以上是大佬教程为你收集整理的Cocos2d-x中瞬时动作全部内容,希望文章能够帮你解决Cocos2d-x中瞬时动作所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。