遊戲login的時候必需要求玩家輸入用戶名、密碼,還要能夠刪除。json
cocostudio畫一個textfield,直接讀入好了;ui
textField類,繼承讀取的widget。lua
local textField = class("textField", function GUIReader:shareReader():widgetFromJsonFile(jsonPath) end)spa
--得到textField組件code
function testField:ctor()繼承
self.mTextField_input = tolua.cast(Helper:seekWidgetByName(self, "textField_m"), "ccui.TextField")遊戲
self:addCallback()事件
endip
--註冊回調get
function testField:addCallback()
local keyListener = cc.EventListenerKeyBoard:create()
keyListener:registerScriptHandler(handler(self, self.onkeyPressed), cc.Handler.EVENT_KEYBOARD_PRESSED)
local currentScene = l_command.getCurrentScene()
currentScene:getEventDispatcher():addEventListenerWithSceneGraphPrioprity(keyListener, currentScene)
end
--刪除事件,刪除字母
function testField:onkeyPressed(keycode, event)
if keycode == cc.Keycode.KEYBACKSPACE then
local str = self.mTextField_input:getStringValue()
str = string.sub(str, 0, string.len(str) - 1)
self.mTextField_input:setText(str)
end
end