Can't call function from EventListenerTouch

Can't call function from EventListenerTouch
0.0 0


I’m using 3.0 and I’m trying to get touch working with EventListenerTouch.

In the release notes it states that we can repond to touches like this:

auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouch(true); listener->onTouchBegan = [](Touch* touch, Event* event) { do_some_thing(); return true; };

This works great, and if I print to the console here I can confirm that it’s working. If, however, I try to call a function of the class I am in from within the curly braces, Xcode gives me the error ‘this’ cannot be implicitly captured in this context’.

The syntax of the example looks a little strange to me with the square brackets, and it’s not a language feature I’m familiar with (I’m coming from cocos2d-iphone, so I’m more of an Objective-C guy). Can anyone explain to me how to get it working?


It’s called lambda expression, you can think of it as block in obj-c

You can also use good old callback function like this
_touchListener->onTouchBegan = CC_CALLBACK_2(MyClass::onTouchBegan, this);


That’s working perfectly, thanks for the link too.