// 这里用得时scene场景来试验的,场景代码
local MyServerTest = class("MyServerTest", function()
return cc.Scene:create()
end)
function MyServerTest:create()
local ms = MyServerTest.new()
ms:addChild(ms:init())
return ms
end
function MyServerTest:ctor()
self.winsize = cc.Director:getInstance():getWinSize()
end
function MyServerTest:init()
local layer = cc.Layer:create()
// 连网请求实现
local function item_Menu_CallBack()
// 不知道为什么我的那么坑爹,提示的是XmlHttp,让我错了半个小时,蛋疼,注意,这里是:new()
local xhr = cc.XMLHttpRequest:new()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
xhr:open("GET","http://192.168.0.107/Test.html")
local function onReadyStateChange()
if xhr.readyState==4 and xhr.status>=200 and xhr.status<207 then
local label = cc.Label:create()
layer:addChild(label)
label:setPosition(self.winsize.width/2,200)
label:setString(xhr.response)
end
end
xhr:registerScriptHandler(onReadyStateChange)
xhr:send()
end
-- local lab = cc.Label:createWithSystemFont("My Test","",30)
local lab1 = cc.Label:create()
lab1:setString("Label")
lab1:setSystemFontSize(40)
-- lab:setColor(cc.c3b(255,0,0))
local item = cc.MenuItemLabel:create(lab1)
local menu = cc.Menu:create()
menu:addChild(item)
menu:setPosition(self.winsize.width/2,self.winsize.height/2)
layer:addChild(menu)
item:registerScriptTapHandler(item_Menu_CallBack)
return layer
end
return MyServerTest
end