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

If you want to please these users, try these 5 methods

Elevator, subway, taxi. Newspapers, Weibo, and &q...

12 Work Reporting Skills from Workplace Experts

Baidu Netdisk download location: m1-21-12 Work Re...

Douban’s methods and strategies for obtaining traffic!

Not long ago, Pinduoduo's market value surpas...

Girls assume you can sleep with her? How can I test whether she will let me go?

In life, women express their emotions differently...

How to configure when renting and hosting a server?

Server leasing means that the server provider pro...