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

Fuwei SEO-Large Website SEO Implementation Plan

Fuwei SEO-Large Website SEO Implementation Plan T...

Solve the iOS memory crash problem caused by Flutter

background If your Flutter version number is less...

How to attract the core users you want!

The core users we are talking about probably refe...

Analysis of Xiaohongshu’s category operations!

I have been dissecting Xiaohongshu accounts recen...

Can Toutiao surpass Baidu and WeChat in the era of self-media?

The launch of WeChat public accounts has led to t...

Congratulations to Chen Dong, Liu Yang, and Cai Xuzhe!

CPC Central Committee State Council Central Milit...

Meizu M6 hands-on experience: Smartphones under 1,000 yuan can also perform well

Since the advent of the Meizu brand, the Meizu No...

8 years of Double 11: What else can we see besides pessimism and doubt?

120.7 billion, a year-on-year increase of 32.3%. ...

For a sprained joint, should you apply heat or cold?

In daily life, various joint sprains are common, ...