在quick中调用tcp连接的文章不是很多,我也搜了一下,就那么几篇。按文章内容拿回来测试是不好用的,于是自己调试(我lua也不熟悉),终于弄通了!这里要感谢群里的LY大侠的耐心指点,现分享出来!调试环境用的是一个网络调试助手(下载地址:http://cmsoft.cn:8000/downloads/cmsoft/NetAssist.rar)其它具体说明在源代码里面有不多解释了,老规矩上代码先!
-------------------------------------------------------------------------------------MyApp.lua
-- 这个文件在我的目录是 C:\cocos\quick-cocos2d-x-3.2rc1\quick\samples\coinflip\src\app
-- 版本大家都知道了吧?我就是基于这个例子改的,就改了这一个例子文件其它没动,累赘文件我就不多发了
-- 非常感谢群(8952580)里的YL大侠的耐心指点
require("config")
require("framework.init")
require("framework.shortcodes")
require("framework.cc.init")
local MyApp = class("MyApp", cc.mvc.AppBase)
function MyApp:onStatus(__event)
print("------回调通了可以发数-----")
print("socket status: %s", __event.name)
socket:send("socket 发数喽!!!!!!!!!!!!!")
end
function MyApp:onData(__event)
print("------zzz2-----")
print("socket status: %s, data:%s", __event.name, ByteArray.toString(__event.data))
end
function MyApp:ctor()
--------核心的几行示例代码呀!
cc.net = require("framework.cc.net.init")
socket = cc.net.SocketTCP.new()
socket:addEventListener(cc.net.SocketTCP.EVENT_CONNECTED, handler(self, self.onStatus))
socket:addEventListener(cc.net.SocketTCP.EVENT_CLOSE, handler(self, self.onStatus))
socket:addEventListener(cc.net.SocketTCP.EVENT_CLOSED, handler(self, self.onStatus))
socket:addEventListener(cc.net.SocketTCP.EVENT_CONNECT_FAILURE, handler(self, self.onStatus))
socket:addEventListener(cc.net.SocketTCP.EVENT_DATA, handler(self, self.onData))
socket:connect("192.168.1.113", 8888, false)
---------示例end
MyApp.super.ctor(self)
self.objects_ = {}
end
function MyApp:run()
print("------2---------")
cc.FileUtils:getInstance():addSearchPath("res/")
display.addSpriteFrames(GAME_TEXTURE_DATA_FILENAME, GAME_TEXTURE_IMAGE_FILENAME)
-- preload all sounds
for k, v in pairs(GAME_SFX) do
audio.preloadSound(v)
end
self:enterMenuScene()
end
function MyApp:enterMenuScene()
print("------2---------")
self:enterScene("MenuScene", nil, "fade", 0.6, display.COLOR_WHITE)
end
function MyApp:enterMoreGamesScene()
print("------3---------")
socket:send("socket 开什么始呀?????例子而已!")
end
function MyApp:enterChooseLevelScene()
print("------4---------")
socket:send("socket 选什么关呀?????例子而已!")
end
function MyApp:playLevel(levelIndex)
print("------5---------")
socket:send("socket 玩什么呀?????例子而已!")
end
appInstance = MyApp
return MyApp
-------------------------------------------------------------------------------------
还是老调的结尾,,,欢迎有游戏梦想的朋友加群交流(368638695)