cocos2dx3.5 studio c++简单功能实现

这次用c++实现按钮切换场景


studio 2.2.1

framework 3.5

cocos2d-x 3.5

vs2012



在studio里创建一个项目叫Bu吧,当然是c++的

在MainScene.csd上加一个按钮,名称改为play吧


然后发布到vs,拿Vs打开


在src目录下会有HelloWorldScene.cpp和HelloWorldScene.h

打开HelloWorldScene.h头文件

我们要使用cocos2d::ui::Button,及其监听事件,完整的#include和namespace就是这样的

#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "editor-support/cocostudio/CCSGUIReader.h"

using namespace cocos2d::ui;
using namespace cocostudio;

在其下定义函数时还要加入点击按钮之后的回调函数

virtual void touchevent(Ref* ref, Widget::TouchEventType touchType);

完整代码如下:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "editor-support/cocostudio/CCSGUIReader.h"

using namespace cocos2d::ui;
using namespace cocostudio;



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();

    virtual void touchevent(Ref* ref, Widget::TouchEventType touchType);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

再来看HelloWorldScene.cpp

假设我们要切换到的场景是SecondScene,当然也有其cpp和h

我们在HelloWorldScene.cpp中加入

#include "SecondScene.h"

在init里面要加入获取studio中创建的按钮,并加上监听事件

Button* btn=static_cast<Button*>(rootNode->getChildByName("play"));
	btn->addTouchEventListener(this,toucheventselector(HelloWorld::touchevent));
上面第一行是获取Studio中创建的按钮,我们给按钮定义的名称是play,第二行是添加监听事件
最后的touchevent是我们自定义的回调函数,写在init前即可

void HelloWorld::touchevent(Ref* ref, Widget::TouchEventType touchType){
	switch (touchType)
	{
	case cocos2d::ui::Widget::TouchEventType::BEGAN:
		break;
	case cocos2d::ui::Widget::TouchEventType::MOVED:
		break;
	case cocos2d::ui::Widget::TouchEventType::ENDED:
		Director::getInstance()->replaceScene(SecondScene::createScene());
		break;
	case cocos2d::ui::Widget::TouchEventType::CANCELED:
		break;
	default:
		break;
	}
}

这里TouchEventType有四个,其实我们这个例子真正用的是第三个ENDED

里面的

Director::getInstance()->replaceScene(SecondScene::createScene());
就是切换到场景SecondScene

看HelloWorld.cpp整个代码

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "SecondScene.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

void HelloWorld::touchevent(Ref* ref, Widget::TouchEventType touchType){
	switch (touchType)
	{
	case cocos2d::ui::Widget::TouchEventType::BEGAN:
		break;
	case cocos2d::ui::Widget::TouchEventType::MOVED:
		break;
	case cocos2d::ui::Widget::TouchEventType::ENDED:
		Director::getInstance()->replaceScene(SecondScene::createScene());
		break;
	case cocos2d::ui::Widget::TouchEventType::CANCELED:
		break;
	default:
		break;
	}
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto rootNode = CSLoader::createNode("MainScene.csb");

    addChild(rootNode);

	Button* btn=static_cast<Button*>(rootNode->getChildByName("play"));
	btn->addTouchEventListener(this,toucheventselector(HelloWorld::touchevent));

    return true;
}

运行
实现了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值