2.cocos2dx 3.2中语法的不同之处,lambada表达式的使用和function和bind函数的使用

简介: 1        打开建好的T32  Cocos2dx-3.2的一个项目 2        设置Cocos显示窗口的位置是在AppDelegate.cpp中: 3  设置自适应窗口大小的代码是在上面的代码后面紧接着就添加: glview->setDesignResolutionSize(480,320, ResolutionPolicy::EXACT_FIT);

1        打开建好的T32  Cocos2dx-3.2的一个项目


2        设置Cocos显示窗口的位置是在AppDelegate.cpp中:


3  设置自适应窗口大小的代码是在上面的代码后面紧接着就添加:

glview->setDesignResolutionSize(480,320, ResolutionPolicy::EXACT_FIT);

 

3        cocos2d-x-3.2项目案例(3.2版本之后都去掉了CC前缀)

4        项目目录结构如下:


编写公共的头文件T32.h

#ifndef _T32_H__

#define _T32_H__

#include "cocos2d.h"

USING_NS_CC;

 

#define winSize Director::getInstance()->getWinSize()

 

//因为3.2版本中输出日志不建议使用CCLog,而是使用log,为了还想

//使用原来风格的CCLog做如下定义

#define CCLog cocos2d::log

 

#endif // !_T32_H__

编写:TBack.h

#ifndef __TBack_H__

#define __TBack_H__

 

#include "T32.h"

 

//注意这时候不是CCLayer了,而是Layer了

class TBack :public Layer

{

public:

    CREATE_FUNC(TBack);

    bool init();

};

 

#endif

编写TBack.cpp

#include "TBack.h"

 

bool TBack::init()

{

    Layer::init();

    //设置zorder

    setLocalZOrder(100);

 

    Menu* menu = Menu::create();

 

    MenuItemImage* item = MenuItemImage::create("CloseNormal.png","CloseSelected.png",

        [](Ref*){

        Director::getInstance()->popScene();

    });

 

    menu->addChild(item);

    //注意,这里的没有回调函数了,而是用lambada表达是来替换掉了。

    item->setPosition(winSize.width / 2 -item->getBoundingBox().size.width / 2,

        item->getBoundingBox().size.height / 2 -winSize.height / 2);

 

    addChild(menu);

 

    return true;

}

编写:TMenu.h

#ifndef __TMenu_H__

#define __TMenu_H__

 

#include "T32.h"

 

class TMenu : public Layer

{

public:

    CREATE_FUNC(TMenu);

 

    bool init();

 

    bool TouchBegan(Touch*, Event*);

};

 

#endif

编写TMenu.cpp

#include "TMenu.h"

#include "TBack.h"

#include "T01CPP11.h"

 

static constchar* title[] = {

    "T01CPP11",

};

 

bool TMenu::init()

{

    Layer::init();

 

    Menu* menu = Menu::create();

    addChild(menu);

 

    for (inti = 0; i < sizeof(title) / sizeof(*title); ++i)

    {

        MenuItemFont* item = MenuItemFont::create(title[i], [](Ref*sender){

 

            MenuItem* item = (MenuItem*)sender;

            int i = item->getTag() - 1000;

            Layer* l = NULL;

            if (title[i] =="T01CPP11")

            {

                l = T01CPP11::create();

            }

 

            if (l)

            {

                TBack*b = TBack::create();

                Scene*s = Scene::create();

                s->addChild(b);

                s->addChild(l);

                Director::getInstance()->pushScene(s);

            }

        });

        menu->addChild(item);

        item->setTag(1000 +i);

    }

 

    menu->alignItemsVertically();

 

    // 触摸

    auto ev = EventListenerTouchOneByOne::create();

#if 0

    ev->onTouchBegan = [](Touch*,Event*){

        return true;

    };

#endif

 

    //下面两行代码是相同的

    //ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2);

    ev->onTouchBegan =CC_CALLBACK_2(TMenu::TouchBegan,this);

 

    ev->onTouchMoved = [&](Touch*touch, Event*){

        setPositionY(getPositionY() +touch->getDelta().y);

    };

    _eventDispatcher->addEventListenerWithSceneGraphPriority(ev,this);

 

    return true;

}

 

bool TMenu::TouchBegan(/*TMEnu* this, */Touch*,Event*)

{

    return true;

}

编写:T01CPP11.h

#ifndef __T01CPP11_H__

#define __T01CPP11_H__

 

#include "T32.h"

 

class T01CPP11:public Layer

{

public:

    CREATE_FUNC(T01CPP11);

 

    bool init();

 

    void mFoo();

};

 

#endif

编写:T01CPP11.cpp(主要介绍lambada表达式)

#include "T01CPP11.h"

 

void foo()

{

    CCLog("foo is called\n");

}

 

void funArg3(int n,charc,float f)

{

    CCLog("%d,%c,%f",n,c,f);

}

 

void T01CPP11::mFoo()

{

    CCLog("mFoo is called");

}

 

//关于lambda表达式

bool T01CPP11::init()

{

    Layer::init();

    {

        auto func = []{return 1; };

        int i = func();

        CCLog("i = %d",i);

    }

    //最简单的lambada表达式是只要一个中括号和一个大括号

    //[]捕获列表

    //{}函数体

    //1.捕获列表,可以放变量名,这里可以用来传递函数体内定义的变量

    {

        int v = 100;

        auto func = [v]{returnv; };

        int x = func();

    }

 

    //2.捕获列表,可以捕获多个变量

    {

        int p = 100, q = 200;

        auto func = [p, q]{returnp + q; };

        int s = func();

    }

 

    // 3.捕获列表,有引用和传值两种方式,传值不可以改变,引用可以改变,并且改变外部的变量值

    {

        int p = 100, q = 200;

        auto func = [p, &q]{q++; return p + q; };

        int s = func();

    }

 

    //4.捕获列表,可以定义mutable类型的lambada,能改变传值的捕获参数,

    //但是不能改变外部的变量值

    {

        int p = 100, q = 200;

        auto func = [p, q]()mutable{p++;q++; return p + q; };

        int s = func();

        CCLog("p = %d,q = %d,s = %d",p, q, s);

    }

 

    //5.捕获列表,可以用=或者&捕获所有变量,=指传值,&表示引用

    {

        int p = 100, q = 200;

        //用&的时候,所有的都可以调用了,[&,p]:表示除了p不能被使用,其它的都可以被使用

        auto func = [&]{

            return p + q;

        };

    }

 

    //稍微复杂点的lambda表达式

    {

        auto add = [](int v1,int v2){returnv1 + v2; };

        auto a = add(1 , 2);

    }

 

    //小括号中的是参数列表,参数列表和捕获列表区别在于,参数列表的参数由调用方决定,

    //捕获列表由定义方决定,所以更加灵活

 

    //更加复杂的lambada表达是,有返回值,返回值一般都省略

    {

        //->int表示返回值是int类型的

        auto add = [](int v1,int v2)->int{returnv1 + v2; };

    }

 

    //总结:auto func = [](){}

    {

        auto func = [](){};

    }

 

    return true;

}

// T01CPP11.cpp中使用使用function和bind函数的案例:

#include "T01CPP11.h"

 

void foo()

{

    CCLog("foo is called\n");

}

 

void funArg3(int n,charc,float f)

{

    CCLog("%d,%c,%f",n,c,f);

}

 

void T01CPP11::mFoo()

{

    CCLog("mFoo is called");

}

 

//关于lambda表达式

bool T01CPP11::init()

{

    Layer::init();

   

    //std::function;

    //std::bind

 

    //函数指针类型

    std::function<void()>func = foo;

    func();

 

    //成员函数指针的赋值和调用

    {

        //注意在::域作用符后面加上*

        void(T01CPP11::*FuncPtr)() = &T01CPP11::mFoo;

        //调用成员函数的时候加上this

        (this->*FuncPtr)();

    }

 

    //bind的功能,就是把一个具体函数,编程std::function对象

    //bind可以把具体函数和std::function形式完全改变,比如参数数量的改变

    {

        std::function<void()>func = std::bind(funArg3, 100,'c', 0.1f);

        func();

    }

 

    //也可以改变参数顺序

    {

        //其中

        //_1:表示function<void(float, char, int)>括号中的第一个参数

        //_2:表示function<void(float, char, int)>括号中的第二个参数

        //_3:表示function<void(float, char, int)>括号中的第三个参数

        //后面3个占位符分别在funArg3中的顺序,而又用标记来代表上面括号中参数的的位置

        std::function<void(float,char, int)> func = std::bind(funArg3,

            std::placeholders::_3,std::placeholders::_2,std::placeholders::_1);

        func(1.0f, 'd', 2000);

    }

 

    // 也可以同时改变参数个数和顺序

    {

        std::function<void(float,char)> func = std::bind(funArg3,

            100, std::placeholders::_2,std::placeholders::_1);

        func(4.0f, 'x');

    }

 

    //也可以绑定成员函数

    {

        std::function<void()>func = std::bind(&T01CPP11::mFoo,this);

        func();

    }


 

    //下面的运行结果是:lambada is called

    {

        std::function<void()> func = [](){};

        std::function<void(int)> func1 = std::bind([](int, int){

            CCLog("lambada iscalled");

        },10,std::placeholders::_1);

        func1(100);

    }


 

    return true;

}

修改AppDelegate.cpp

A添加头文件:

#include "TMenu.h"

#include "TBack.h"

 

B:修改:applicationDidFinishLaunching()截图如下:


 

目录
相关文章
|
3月前
|
消息中间件 Kubernetes NoSQL
c++ 11 中 function,lambda,bind
c++ 11 中 function,lambda,bind
c++11新特性——function和bind
c++11新特性——function和bind
|
3月前
|
C++
C++高级开发之可调用对象、function、bind(2)
std::bind 绑定器   要使用这个函数模板,在 cpp文件前面要包含如下头文件#include<funcitonal>   std::bind能够将对象以及相关的参数绑定到一起,绑定完成后可以直接调用,也可以用
30 0
|
18天前
|
资源调度 Serverless 计算机视觉
高斯函数 Gaussian Function
**高斯函数,或称正态分布,以数学家高斯命名,具有钟形曲线特征。关键参数包括期望值μ(决定分布中心)和标准差σ(影响分布的宽度)。当μ=0且σ²=1时,分布为标准正态分布。高斯函数广泛应用于统计学、信号处理和图像处理,如高斯滤波器用于图像模糊。其概率密度函数为e^(-x²/2σ²),积分结果为误差函数。在编程中,高斯函数常用于创建二维权重矩阵进行图像的加权平均,实现模糊效果。
14 1
|
1月前
|
算法 Serverless C语言
CMake函数和宏(function和macro):使用函数和宏提高代码可读性
CMake函数和宏(function和macro):使用函数和宏提高代码可读性
30 1
|
1月前
|
存储 安全 编译器
【C++ 包装器类 std::function 和 函数适配器 std::bind】 C++11 全面的std::function和std::bind的入门使用教程
【C++ 包装器类 std::function 和 函数适配器 std::bind】 C++11 全面的std::function和std::bind的入门使用教程
32 0
|
1月前
|
SQL Oracle 关系型数据库
Flink的表值函数(Table-Valued Function,TVF)是一种返回值是一张表的函数
【2月更文挑战第17天】Flink的表值函数(Table-Valued Function,TVF)是一种返回值是一张表的函数
20 1
|
3月前
|
存储 SQL 安全
函数(Function)和存储过程(Stored Procedure)的区别(小白情感版)
函数(Function)和存储过程(Stored Procedure)的区别(小白情感版)
31 0
|
3月前
|
缓存
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
92 0
|
3月前
C++11实用技术(二)std::function和bind绑定器
C++11实用技术(二)std::function和bind绑定器
26 0

热门文章

最新文章