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

People in this group should be careful when getting the new coronavirus vaccine!

Spring is the peak season for pollen allergies Ca...

How to build a good community? 6 must-read thoughts!

As the growth rate of offline channels and e-comm...

7 Common Marketing Methods Marketers Must Know (I)

Every year there are many good marketing cases . ...

Tips and algorithms for creating popular short videos!

01 Changes in the communication model: from manua...

Unity Awards 2015 is about to start, and good games are coming soon!

Unity Awards 2015 (Game and Application Creativit...

Beijing's heating is extended to March 22! How is the extension fee calculated?

Today we received two pieces of good news. In add...

Google's official MVP sample code reading notes

Unraveling the project structure According to int...

Expert interpretation | Is artificial intelligence a master key to the future?

The fourth industrial revolution is approaching. ...

Meizu Blue has been released, but has Meizu figured it out?

For a good product, why must I rely on low prices...