Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation method of "callback feature" under Cocos2d-x3.5

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the implementation of the "callback feature" under Cocos2d-x3.5. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Cocos2d-x3.5 has been released for a short time, and careful students will find that this version adds an overloaded version that loads cocos resources:

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

This has one more parameter for the callback function than the other version.

What did the frame do with this thing?

By reading the source code, you can see that after loading a node, the framework simply calls back the node to this function (except for the root node). In this way, with this parameter, we can do some modification to the loading node during the process of loading the resource.

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

Text:

First of all, using the above function, we can get each node object when loading, and then we need to get the parameters filled in Cocos.

How do you get it exactly?

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 handling for the callback feature when CSLoader::createNode loads a Widget type node. That is, take out the fields of the two callback features we filled in in Cocos, and pass China Unicom widget,_rootNode to bindCallback for binding.

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

At this point, we have filled in the callback node and the 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 node currently loaded

Widget* widget = dynamic_cast

If (widget) {

/ / get the information about the attributes related to the callback feature.

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, perform some string comparison operations with callbackName and callbackType, find an appropriate function, and throw it to widget for listening, such as:

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

If (callbackName = = "animal1" & & callbackType = = "Click") {/ / assume there is an interface for playAnimal1

Widget- > addClickEventListener (CC_CALLBACK_2 (MyScene::playAnimal1,this))

}

Else if (callbackName = = "animal2" & & callbackType = = "Click") {/ / assume there is an interface for playAnimal2

Widget- > addClickEventListener (CC_CALLBACK_2 (MyScene::playAnimal2,this))

}

}

1. So far, neither of Cocos2d-x 's two major scripting engines provides support for callback features, including the newly overloaded createNode function, nor has it been exported to both scripting engines.

However, both getCallbackName and getCallbackType functions are exported. We can traverse the loaded root node by ourselves, use these two functions to obtain the callback feature information entered in Cocos, and bind it ourselves according to 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, with this thing can do exactly depends on your imagination.

The above is the implementation of the "callback feature" under Cocos2d-x3.5 shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report