[常见错误 及 解决方案系列]USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
2015-04-10 11:43
585 查看
原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的 常见错误 及 解决方案 系列]
博客地址:http://blog.csdn.net/qq446569365
Cocos2d-x 3.5的lua项目生成后,变成了MVC模式,并且,加入了一个全局变量的检测功能。也就是说,你不小心用了全局变量,他会提示你出错!
比如
local temp = 1
temp = 10
print(temp)
你写成了
local temp = 1
tepm = 10 --这里写错了
print(temp)
然后,print结果就会不同,同时你还会创建一个 全局的 tepm 永远不会被释放。这种问题,在lua中很容易遇到(虽然现在有代码提示 还是不太容易出现,但是谁说的准呢!)。
所以,C2d加入了一个全局命名空间的检测,通过setmetatable的方式。该函数在
src/cocos/framework/init.lua
有效的帮助开发者避免上述错误!
那么如果我一定要用到全局变量才行呢?例如 require "socket" 的luasocket库
这个问题有若干种解决办法,
1、最简单粗暴的就是直接注释掉着一块。
2、将CC_DISABLE_GLOBAL设置为False
3、在mian.lua的 require "cocos.init"之前 require 所需的库和全局的变量
但是 如果是开发者自己需要使用全局变量。而不是其他库需要用到全局变量的话!
请使用使用官方推荐的方法cc.exports.* 来充当全局变量!
博客地址:http://blog.csdn.net/qq446569365
Cocos2d-x 3.5的lua项目生成后,变成了MVC模式,并且,加入了一个全局变量的检测功能。也就是说,你不小心用了全局变量,他会提示你出错!
比如
local temp = 1
temp = 10
print(temp)
你写成了
local temp = 1
tepm = 10 --这里写错了
print(temp)
然后,print结果就会不同,同时你还会创建一个 全局的 tepm 永远不会被释放。这种问题,在lua中很容易遇到(虽然现在有代码提示 还是不太容易出现,但是谁说的准呢!)。
所以,C2d加入了一个全局命名空间的检测,通过setmetatable的方式。该函数在
src/cocos/framework/init.lua
-- disable create unexpected global variable function cc.disable_global() setmetatable(__g, { __newindex = function(_, name, value) error(string.format("USE \" cc.exports.%s = value \" INSTEAD OF SET GLOBAL VARIABLE", name), 0) end }) end if CC_DISABLE_GLOBAL then cc.disable_global() end<span style="font-family:Arial;font-size:18px;color:#333333;"><span style="line-height: 26px;"> </span></span>
有效的帮助开发者避免上述错误!
那么如果我一定要用到全局变量才行呢?例如 require "socket" 的luasocket库
这个问题有若干种解决办法,
1、最简单粗暴的就是直接注释掉着一块。
2、将CC_DISABLE_GLOBAL设置为False
3、在mian.lua的 require "cocos.init"之前 require 所需的库和全局的变量
但是 如果是开发者自己需要使用全局变量。而不是其他库需要用到全局变量的话!
请使用使用官方推荐的方法cc.exports.* 来充当全局变量!
相关文章推荐
- [常见错误 及 解决方案系列]USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
- [常见错误 及 解决方案系列]USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
- lua常见异常 USE " cc.exports. = value " INSTEAD OF SET GLOBAL VARIABLE"
- USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
- USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
- Tomcat 常见 "The value for the useBean class attribute is invalid" 错误
- tesseract-ocr 出现 错误 Please make sure the TESSDATA_PREFIX environment variable is set to the parent d irectory of your "tessdata" directory.解决方案
- 嵌入式 常见错误Werror=unused-but-set-variable解决方案
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec"
- linux异常系列:Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
- "The value for the useBean class attribute is invalid" 错误。
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- matlab 编写程序时出现"out of memory"错误的 解决方案
- Unable to find a value for "Sex" in object of class ...错误
- Java mail包出现"MimeMessage contains object of type javax.mail.util.SharedByteArrayInputStream instead of MimeMultipart"错误的修正办法
- mysql:Variable 'character_set_client' can't be set to the value of 'NULL'解决
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" task.
- 解决QT:forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget" 等莫名奇妙错误
- Scala编程中常见错误:Error:(24, 29) value foreach is not a member of java.util.Set[String]