A simple implementation method of "callback feature" under Cocos2d-x3.5

A simple implementation method of "callback feature" under Cocos2d-x3.5

Cocos2d-x3.5 has been released for a while. Careful students will find that this version adds a reloaded version for loading cocos resources:

Node* CSLoader::createNode(const std::string &filename, const ccNodeLoadCallback &callback)

Compared to the other version, this one has an additional callback function parameter.

What does the framework do with this thing?

By reading the source code, we can know that after loading a node, the framework will simply call back the node to this function (except the root node). In this way, using this parameter, we can do some modification to the node being loaded during the resource loading process.

This article will talk about how to use this thing to easily implement the previously somewhat complicated function: the callback feature.

text:

First, using the above functions, we can get each node object when loading, and secondly, we need to get the parameters filled in Cocos.

How to obtain it specifically?

First, look for the original implementation of the callback feature in the source code of CSLoader:

Widget* widget = dynamic_cast

if (widget)

{

std::string callbackName = widget->getCallbackName();

std::string callbackType = widget->getCallbackType();

bindCallback(callbackName, callbackType, widget, _rootNode);

}

The above code is some special processing for callback characteristics when CSLoader::createNode loads a Widget type node. It takes out the two callback characteristic fields we filled in Cocos, connects the widget, and passes _rootNode to bindCallback for binding.

The two functions above: getCallbackName, getCallbackType, retrieve the callback parameter information we filled in in Cocos.

So far, we have filled in the callback feature nodes and corresponding parameters.

Next, how to achieve it?

We can consider a similar approach to the above:

CSLoader::createNode(root_path,[this](Node *node){//node is the currently loaded node

Widget* widget = dynamic_cast

if (widget){

//Get information about properties related to callback characteristics.

std::string callbackName = widget->getCallbackName();

std::string callbackType = widget->getCallbackType();

//Bind according to the above information.

this.bindCallback(callbackName, callbackType, widget);

}

});

Then implement a bindCallback function in the current class, use callbackName and callbackType to perform some string comparison operations, find a suitable function, and pass it to the widget for monitoring, for example:

void MyScene::bindCallback(const std::string &callbackName,const std::string &callbackType,widget){

if(callbackName == "animal1" && callbackType == "Click"){//Assume there is an interface of playAnimal1

widget->addClickEventListener(CC_CALLBACK_2(MyScene::playAnimal1,this));//

}

else if(callbackName == "animal2" && callbackType == "Click"){//Assume there is an interface of playAnimal2

widget->addClickEventListener(CC_CALLBACK_2(MyScene::playAnimal2,this));

}

}

postscript:

1. So far, the two major script engines of Cocos2d-x do not provide support for callback features, including the newly overloaded createNode function, which is not exported to the two script engines for use.

However, the two functions getCallbackName and getCallbackType are exported. We can traverse the loaded root nodes by ourselves and use these two functions to obtain the callback feature related information filled in Cocos, and bind them ourselves based on this information.

2. Cocos2.2 began to provide a "user data" interface. The data set by this interface can be obtained through Cocos2d::Node::getUserData. What you can do with this thing depends on your imagination.

<<:  Tencent VS Alibaba's Internet+ War

>>:  Cocos practical case: Experts analyze how to play 3D in "Fishing Master 3"

Recommend

Don’t ask! Milk tea is drunk in the afternoon, people go crazy at night

Who is the most popular beverage in the beverage ...

Strategy! Answers to the 4 most common Baidu search ads problems

There are too many display styles for Baidu searc...

Why are we attracting new users but no highly active users are left?

Some operators may feel puzzled. Why do they spen...

Clinical Application of Movement Assessment System Course Series

Introduction to the clinical application series o...

vivo Hawking Experiment Platform Design and Practice-Platform Product Series 02

1. Introduction After experiencing the wild growt...

Android Date Time Picker

[[185911]] Many applications have date pickers, s...

How can brands market to young people?

This year's 618 mid-year sale kicked off earl...

10 product details analysis to show you how big manufacturers design

As designers, we are dealing with various applica...

2020 Pinduoduo entry process and operation promotion strategy!

Recently, everyone is basically staying at home, ...

Google launches startup accelerator to help startups develop mobile products

According to foreign media VentureBeat, Google ha...

Add tags to pictures

Source code introduction: add tags, edit, and del...