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

How to call the callback function of the JS layer through the JS_CallFunctionValue function.

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

Share

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

This article will explain in detail how the key callback of the callback function in cocos2d-x is, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Callback function is the key to interface interaction and access to a variety of third-party SDK, because the callback function C++ code can not be automatically generated, everything needs to be handwritten.

The good thing is that the Cocos2d-x engine provides a complete wrapper mechanism for callback functions. All we need to do is understand this mechanism and use it. Learn from the engine's own code examples, you can quickly and accurately use this mechanism.

First, we created a new cross-platform JS project in Cocos2d-x 3.0 beta using his own project creation tool. By convention, this is a helloworld project. While XCode is running, we can see the callback button in the lower right corner. Let's see how he did it. To do it in two processes:

1. Bind callback function process

First, we need to find the binding code of the callback function JS. In myApp.js, in the init function, you can see the following code:

12 3 4 5 6 7 8 9 10 11 12//

Add a "close" icon to exit the progress. It's an autorelease objectvar closeItem

= cc.MenuItemImage.create ("res/CloseNormal.png", "res/CloseSelected.png", function ()

{cc.log ("close

Button was clicked. ");}, this); closeItem.setAnchorPoint (cc.p (0.5)

0.5)); var menu

= cc.Menu.create (closeItem); menu.setPosition (cc.p (0)

0)); this.addChild (menu

1); closeItem.setPosition (cc.p (size.width

-20, 20)

The third argument to the cc.MenuItemImage.create function is bound to the anonymous callback function. The fourth parameter is the this when the callback function is called (if you don't understand the this mechanism of JS, please read some JS materials first). These are JS code with obvious intention and function, needless to say.

Then, we look at the underlying corresponding execution of the C++ code. In the cocos2d_specifics.cpp file, find the js_cocos2dx_CCMenuItemImage_create function.

12 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46//

"create" in JS//

Cc.MenuItemImage.create (normalImage, selectedImage, [disabledImage], callback_fn, [this] JSBool

Js_cocos2dx_CCMenuItemImage_create (JSContext * cx, uint32_t argc, jsval * vp) {if (argc)

> = 2 & & argc = 3) {thirdArgIsString

= argv [2] .isString (); if (thirdArgIsString)

{arg2.set (argv [2])

Cx); last

= 3;}} cocos2d::MenuItemImage*

Ret = cocos2d::MenuItemImage::create (arg0.get (), arg1.get (), std::string (arg2.get (); if (argc)

> = 3) {if (! thirdArgIsString)

{/ / cc.MenuItemImage.create (

NormalImage, selectedImage, callback_fn, [this]) jsCallback

= argv [last++]; if (argc

= = 4) {jsThis

= argv [last];}} else {/ / cc.MenuItemImage.create (

NormalImage, selectedImage, disabledImage, callback_fn, [this]) if (argc

> = 4) {jsCallback

= argv [last++]; if (argc

= = 5) {jsThis

= argv [last];} JSObject

* obj = bind_menu_item (cx, ret, jsCallback, jsThis); JS_SET_RVAL (cx

Vp, OBJECT_TO_JSVAL (obj); return JS_TRUE;} JS_ReportError (cx, "Invalid)

Number of arguments. Expecting: 2

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