cocos2d-x场景与层的使用

前端之家收集整理的这篇文章主要介绍了cocos2d-x场景与层的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天学习了Cocosd-x中场景和层的使用。@H_404_1@

本次实例有两个场景HelloWord , Setting,在HelloWord的游戏设置中可以切换到Setting场景@H_404_1@

首先我们新建一个Setting类,继承自cocos2d::Layer类@H_404_1@

具体代码如下:@H_404_1@

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp,so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    
    // a selector callback
    //void menuCloseCallback(cocos2d::Ref* pSender);
    //
	void menuItemStartCallBack(cocos2d::Ref *pSender);
	void menuItemSettingCallBack(cocos2d::Ref *pSender);
	void menuItemHelpCallBack(cocos2d::Ref *pSender);
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__


#include"SettingScene.h"

USING_NS_CC;

Scene *Setting::createScene(){

	auto scene = Scene::create();

	auto layer = Setting::create();

	scene->addChild(layer);
	return scene;
}


bool Setting::init(){
	if (!Layer::init()){

		return FALSE;
	}


	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//背景创建
	Sprite *bg = Sprite::create("setting-back.png");
	bg->setPosition(origin.x + visibleSize.width / 2,origin.y + visibleSize.height / 2);
	this->addChild(bg);

	auto soundOnMenuItem = MenuItemImage::create("on.png","on.png");

	auto soundOffMenuItem = MenuItemImage::create("off.png","off.png");

	auto soundToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Setting::menuSoundToggleCallBack,this),soundOnMenuItem,soundOffMenuItem,NULL);

	soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818,220)));

	//音乐设置

	auto musicOnMenuItem = MenuItemImage::create("on.png","on.png");

	auto musicOffMenuItem = MenuItemImage::create("off.png","off.png");

	auto musicToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Setting::menuMusicToggleCallBack,musicOnMenuItem,musicOffMenuItem,NULL);

	musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818,362)));

	//ok按钮

	auto okMenuItem = MenuItemImage::create("ok-down.png","ok-up.png",CC_CALLBACK_1(Setting::menuOkCallBack,this));

	okMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(600,510)));

	Menu* mn = Menu::create(soundToggleMenuItem,musicToggleMenuItem,okMenuItem,NULL);
	mn->setPosition(Vec2::ZERO);
	this->addChild(mn);



	return TRUE;
}


void Setting::menuSoundToggleCallBack(cocos2d::Ref* pSender){

}
void Setting::menuMusicToggleCallBack(cocos2d::Ref* pSender){

}
void Setting::menuOkCallBack(cocos2d::Ref* pSender){
	Director::getInstance()->popScene();
}


以上就完成了对Setting类的封装,下面我们只要在HelloWorld场景中进行场景的切换就可以了@H_404_1@

具体代码如下:@H_404_1@

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//添加背景图片
	Sprite *bg = Sprite::create("background.png");
	bg->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height / 2));
	this -> addChild(bg);
	  
	//开始精灵

	Sprite *StartSpriteNormal = Sprite::create("start-up.png");
	Sprite *StartSpriteSelected = Sprite::create("start-down.png");

	MenuItemSprite *StartMenuItem = MenuItemSprite::create(StartSpriteNormal,StartSpriteSelected,CC_CALLBACK_1(HelloWorld::menuItemStartCallBack,this));
	StartMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(700,170)));
	
	//设置图片菜单
	MenuItemImage *settingMenuItem = MenuItemImage::create(
		"setting-up.png","setting-down.png",CC_CALLBACK_1(HelloWorld::menuItemSettingCallBack,this));

	settingMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(480,400)));

	//设置帮助菜单
	MenuItemImage *helpMenuItem = MenuItemImage::create(
		"help-up.png","help-down.png",CC_CALLBACK_1(HelloWorld::menuItemHelpCallBack,this));
	helpMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(860,480)));

	Menu *mn = Menu::create(StartMenuItem,settingMenuItem,helpMenuItem,NULL);

	mn->setPosition(Vec2::ZERO);
	this->addChild(mn);

    return true;
}

void HelloWorld::menuItemStartCallBack(cocos2d::Ref *pSender){
	MenuItem *item = (MenuItem*)pSender;
	log("Touch Start %p ",item);

}

void HelloWorld::menuItemSettingCallBack(cocos2d::Ref *pSender){

	auto sc = Setting::createScene();
	Director::getInstance()->pushScene(sc);
}

void HelloWorld::menuItemHelpCallBack(cocos2d::Ref *pSender){

}


具体使用到的场景切换函数:@H_404_1@

void pushScene(); //切换到下一个场景,将当前场景挂起到场景堆栈中,然后切换到下一个场景@H_404_1@

void popScene(); //回到上一个场景@H_404_1@


@H_404_1@


@H_404_1@

以及场景过度动画:@H_404_1@


@H_404_1@

auto sc = Setting::createScene();
auto reScene = TransitionJumpZoom::create(2.0f,sc);
Director::getInstance()->pushScene(reScene);
@H_404_1@

关键代码就是第二句动画的设置。具体动画类型去查看过渡动画类TransitionScene类和它的自雷@H_404_1@

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