---------------------------------------------------
--創(chuàng)建新手文本彈出框
--@param pos 要顯示的位置 錨點(diǎn)(0.5,0.5)
--@param text 要顯示的文字
--@param textSize 文字的字號(hào)大小
--@param width 行寬
--@param img 背景圖片
--@param dim 模糊程度 取值范圍 0 - 1
function createNewbieTextDialog(pos, text, textSize, width, img, dim, lineNum)
assert(Utility:isNotEmptyStr(text),"the information text is not set")
assert(textSize ~= 0,"the textSize is wrong")
assert(width ~= 0,"the width is wrong")
assert(pos,"the pos is nil")
assert(img,"the img is nil")
assert(lineNum,"the lineNum is nil")
local textFonts = {} -- 字符暫時(shí)存儲(chǔ)的地方
local charArrayFonts = {} --字符存放的地方
local startIndex = 1
local endIndex = 1
local isOk = 0
local charIndex = 1 --字符數(shù)組下標(biāo)
-- 添加字符串?dāng)?shù)組(判斷其中是否有中文字符)
for i = 1, string.len(text) do
local curByte = string.byte(text, i)
local byteCount = 1 -- Utf8 編碼最大有4個(gè)字節(jié)的情況
-- 判斷編碼類型
if curByte > 0 and curByte <= 127 then
byteCount = 1
elseif curByte >= 192 and curByte < 223 then
byteCount = 2
elseif curByte >= 224 and curByte < 239 then
byteCount = 3
elseif curByte >= 240 and curByte <= 247 then
byteCount = 4
end
endIndex = i + byteCount - 1
textFonts[i] = string.sub(text, startIndex, endIndex)
startIndex = endIndex + 1
if byteCount ==3 then
charArrayFonts[charIndex] = textFonts[i]
print("pppppp"..charArrayFonts[charIndex])
charIndex = charIndex + 1
isOk = 2
elseif byteCount == 1 then
if isOk <= 0 then
charArrayFonts[charIndex] = textFonts[i]
print("HHHHHH"..charArrayFonts[charIndex])
charIndex = charIndex + 1
end
isOk = isOk - 1
end
-- 講字符串?dāng)?shù)組傳入正確的字節(jié)數(shù)和值
--print("##########"..textFonts[i].."@@@@@@@@@"..startIndex.."^^^^^^^"..byteCount)
end
local popLayer = nil --彈出層
local function onTouchBegan(touch,event)
return true
end
--移除彈出層
local function onTouchEnded(touch,event)
cc.Director:getInstance():getRunningScene():removeChild(popLayer,true)
return true
end
--注冊觸摸事件
local function registerTouchEvent()
local listener = cc.EventListenerTouchOneByOne:create();
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
listener:setSwallowTouches(true)
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,popLayer);
end
local bgimv=ccui.ImageView:create(img)
bgimv:setContentSize(cc.size(width+50,lineNum * textSize+50)) --50 像素為底部背景比文字大出來的邊框
bgimv:setScale9Enabled(true)
bgimv:setPosition(pos)
popLayer = cc.LayerColor:create(cc.c4b(0,0,0,255*dim%255))
popLayer:setPosition(cc.p(0,0))
popLayer:addChild(bgimv)
-- 彈板上的文字
local layerHeight = nil
local tipLable = {}
local i = 1
local j = 0 -- 可能轉(zhuǎn)到第二行
-- 調(diào)度函數(shù)
local function scheduleUpdate(dt)
tipLable = cc.Label:createWithSystemFont(charArrayFonts[i],"fonts/Marker Felt.ttf",textSize,cc.size(width,-1))
tipLable:setColor(cc.c3b(255,255,255))
layerHeight = tipLable:getContentSize().height
tipLable:setAnchorPoint(cc.p(0.5,0.5))
tipLable:setPosition(pos)
tipLable:setPositionX(pos.x + i * textSize)
if pos.x + i * textSize >= 1.5 * width then -- 自動(dòng)換行,最多兩行
tipLable:setPositionY(pos.y - textSize)
tipLable:setPositionX(pos.x + j * textSize)
j = j + 1
end
i = i + 1
print(i.."##########"..charArrayFonts[i])
popLayer:addChild(tipLable) -- 添加文字
if i >= #charArrayFonts then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(Schedule.id)
end
end
--創(chuàng)建調(diào)度者
Schedule.id = cc.Director:getInstance():getScheduler():scheduleScriptFunc(scheduleUpdate, 0.1, false)
registerTouchEvent() -- 注冊觸摸監(jiān)聽事件
cc.Director:getInstance():getRunningScene():addChild(popLayer) -- 在當(dāng)前場景添加層
end