cocos 3.1.1 lua常有控件

require?"Cocos2d"??

require"Cocos2dConstants"??


--?cclog??

cclog?=?function(...)??

????print(string.format(...))??

end??


--for?CCLuaEngine?traceback??

function?__G__TRACKBACK__(msg)??

cclog("----------------------------------------")??

cclog("LUA?ERROR:?"?..?tostring(msg)?..?"\n")??

????cclog(debug.traceback())??

cclog("----------------------------------------")??

return?msg??

end??


local?function?main()??

collectgarbage("collect")??

????--?avoid?memory?leak??

collectgarbage("setpause",?100)??

collectgarbage("setstepmul",?5000)??

cc.FileUtils:getInstance():addSearchResolutionsOrder("src");??

cc.FileUtils:getInstance():addSearchResolutionsOrder("res");??

????--support?debug??

????local?targetPlatform?=?cc.Application:getInstance():getTargetPlatform()??

if?(cc.PLATFORM_OS_IPHONE?==?targetPlatform)?or?(cc.PLATFORM_OS_IPAD?==?targetPlatform)?or???

???????(cc.PLATFORM_OS_ANDROID?==?targetPlatform)?or?(cc.PLATFORM_OS_WINDOWS?==?targetPlatform)?or??

???????(cc.PLATFORM_OS_MAC?==?targetPlatform)?then??

cclog("result?is?")??

--require('debugger')()??


????end??


????---------------??


????local?visibleSize?=?cc.Director:getInstance():getVisibleSize()??

????local?origin?=?cc.Director:getInstance():getVisibleOrigin()??



????--?create?Layer??

????local?function?createUILayer()??

????????local?UILayer?=?cc.Layer:create()??


????????--圖片精靈??

local?bg?=?cc.Sprite:create("bg.jpg")??

????????bg:setPosition(origin.x?+?visibleSize.width?/?2?+?80,?origin.y?+?visibleSize.height?/?2)??

????????UILayer:addChild(bg)??


????????---文字標(biāo)簽??

local?txt?=?cc.LabelTTF:create("zhong中文123456780",?"Arial",?20)????

????????txt:setColor(?cc.c3b(?255,?255,?255?)?)????

????????txt:setAnchorPoint(cc.p(0,0))??

????????txt:setPosition(?cc.p(?350,??20?)?)??

????????UILayer:addChild(txt)????????


????????--純色精靈??

????????local?boxSprite?=?cc.Sprite:create()??

????????boxSprite:setTextureRect(cc.rect(0,0,80,80))??

????????boxSprite:setAnchorPoint(cc.p(0,0))??

????????boxSprite:setPosition(?cc.p(?350,??40?)?)??

????????--cocosdx自動(dòng)默認(rèn)是白色??

????????--boxSprite:setColor(cc.c3b(255,255,255))??

????????UILayer:addChild(boxSprite,8)??

????????--?boxSprite:setGlobalZOrder(8)??


????????--?菜單按鈕??

????????local?menuTools??

????????local?function?menuCallbackOpenPopup()??

cclog("ttttttttt")??

????????end??


--?add?the?left-bottom"tools"?menu?to?invoke?menuPopup??

local?menuToolsItem?=?cc.MenuItemImage:create("back.png",?"back2.png")??

????????menuToolsItem:setPosition(0,?0)??

????????menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)??

????????menuTools?=?cc.Menu:create(menuToolsItem)??

????????local?itemWidth?=?menuToolsItem:getContentSize().width??

????????local?itemHeight?=?menuToolsItem:getContentSize().height??

????????menuTools:setPosition(350,?80)??

????????UILayer:addChild(menuTools,15)??

????????--?menuTools:setlocalZOrder(5)??

????????--?menuTools??



????????--?Scale9Sprite?9宮格精靈??

local?temp=cc.Sprite:create("btntest0.png");??

????????local?size=temp:getContentSize();??

????????local?fullRect=cc.rect(0,0,?size.width,?size.height)??

????????local?insetRect?=?cc.rect(20,20,size.width-40,?size.height-40);??

local?backGround?=?cc.Scale9Sprite:create("btntest0.png",?fullRect,?insetRect?)??

????????backGround:setPreferredSize(cc.size(100,?50))??

????????backGround:setPosition(cc.p(350,?130))??

????????backGround:setAnchorPoint(cc.p(0,0))??

????????UILayer:addChild(backGround)??



????????--進(jìn)度條精靈??

????????--?創(chuàng)建并初始化進(jìn)度,第一個(gè)參數(shù)是duration持續(xù)時(shí)間,100為進(jìn)度??

????????local?to1?=?cc.ProgressTo:create(2,?100)??

????????local?to2?=?cc.ProgressTo:create(1,?100)??

????????--創(chuàng)建進(jìn)度條精靈??

local?progressSprite=cc.Sprite:create("bgtime2.png")??

????????progress1=cc.ProgressTimer:create(progressSprite)??

????????--設(shè)置進(jìn)度條類型為BAR????

progress1:setType(cc.PROGRESS_TIMER_TYPE_BAR)--enum??Type?{?RADIAL,?BAR?}??

????????progress1:setPosition(cc.p(200,?150))??


????????--進(jìn)度動(dòng)畫運(yùn)動(dòng)方向,可以多試幾個(gè)值,看看效果??

--進(jìn)度條寬高變化//從左到右??

????????progress1:setMidpoint(cc.p(0,?0))--注意這里是cc.p??c++中是Vec2??

????????progress1:setBarChangeRate(cc.p(1,?0))??


--進(jìn)度條寬高變化//從下到上???

????????--?progress1:setMidpoint(cc.p(0,?0))??

????????--?progress1:setBarChangeRate(cc.p(0,?1))??


--//進(jìn)度條寬高變化//從左到右????

????????--?progress1:setMidpoint(cc.p(1,?0))??

????????--?progress1:setBarChangeRate(cc.p(1,?0))??


--//????//進(jìn)度條寬高變化//從上到下???

????????--?progress1:setMidpoint(cc.p(1,?0))??

????????--?progress1:setBarChangeRate(cc.p(0,?1))??

????????progress1:setPercentage(0)--默認(rèn)是零,可以自行修改??

????????UILayer:addChild(progress1,?1)???

????????--進(jìn)度條效果設(shè)置?方這里提供了兩種??

????????--方案1:執(zhí)行動(dòng)作??

????????--需要有這個(gè)runAction才會(huì)有動(dòng)作效果??

????????--?progress1:runAction(cc.RepeatForever:create(to1))??

????????--方案2:定時(shí)器+setPercentage完成(見后面定時(shí)器中可見)??


????????--圓形的進(jìn)度條1?順時(shí)針??

local?progress3Sprite?=?cc.Sprite:create("menu1.png")??

????????progress3?=?cc.ProgressTimer:create(progress3Sprite)??

????????progress3:setType(cc.PROGRESS_TIMER_TYPE_RADIAL)--設(shè)置類型為圓形????

????????progress3:setPosition(cc.p(250,?200))??

????????progress3:setScale(0.8)????

progress3:setReverseDirection(false)?--?這是默認(rèn)的順時(shí)針??

????????progress3:setPercentage(0)--默認(rèn)是零,可以自行修改??

????????--?progress3:runAction(cc.RepeatForever:create(to1))??

????????UILayer:addChild(progress3,?1)??

????????--如果沒有runAction是可以直接removeFromParent??如果有runAction需要先停掉??

????????----相同的action會(huì)導(dǎo)致報(bào)錯(cuò)??最好用不同的action??

????????--?progress3:stopAction(cc.RepeatForever:create(to1))??

--?progress3:removeFromParent(true)??




????????--圓形的進(jìn)度條2?逆時(shí)針??

local?progress2Sprite?=?cc.Sprite:create("menu1.png")??

????????progress2?=?cc.ProgressTimer:create(progress2Sprite)??

????????progress2:setType(cc.PROGRESS_TIMER_TYPE_RADIAL)--設(shè)置類型為圓形??

????????progress2:setPosition(cc.p(200,?200))??

????????progress2:setScale(0.8)??

progress2:setReverseDirection(true)?--?默認(rèn)是順時(shí)針?這里設(shè)置反向逆時(shí)針??

????????progress2:runAction(cc.RepeatForever:create(to2))??

????????UILayer:addChild(progress2,?1)??


????????--[[??

????????這里有兩個(gè)方法可能是比較容易疑惑的:??

????????progress:setMidpoint(cc.p(0,?0))??

????????setMidpoint()函數(shù)是設(shè)置進(jìn)度條的起始點(diǎn),(0,y)表示最左邊,(1,y)表示最右邊,(x,1)表示最上面,(x,0)表示最下面。??

????????progress:setBarChangeRate(cc.p(1,?0))??

????????setBarChangeRate()函數(shù)是用來設(shè)置進(jìn)度條動(dòng)畫方向的,(1,0)表示橫方向,(0,1)表示縱方向??

????????]]??


????????--?????cc.Label:createWithTTF??--?添加標(biāo)簽??cocos?ide中可以用???xcode中會(huì)報(bào)錯(cuò)??

????????--?--?通過初始化文本、字體,字體大小來創(chuàng)建一個(gè)Label??

--?local?label?=?cc.Label:createWithTTF("New?Renderer",?"fonts/arial.ttf",?36)??

????????--?label:setColor(cc.c3b(255,255,0))?--?設(shè)置顏色為黃色??

????????--?UILayer:addChild(label,?1,?10000)?--?第一個(gè)參數(shù)為node,第二個(gè)參數(shù)為zorder,第三個(gè)參數(shù)是tag??

????????--?label:setPosition(?cc.p(80,?80))--?設(shè)置位置top,center??


????????--創(chuàng)建一個(gè)標(biāo)簽,顯示進(jìn)度??

local?numsTTF?=?cc.LabelTTF:create("0",?"Thonburi",?18)??

????????numsTTF:setPosition(cc.p(250,?150))??

????????UILayer:addChild(numsTTF,?1)??


????????--Lua?實(shí)現(xiàn)定時(shí)器功能??

????????local?scheduler?=?CCDirector:sharedDirector():getScheduler()??

????????local?run_logic?=?nil??

????????local?x=0??

????????--函數(shù)定義??

????????local?function?Update()??

????????????--?print(11)??

if?x<100?then??

????????????????x=x+5??

????????????????progress1:setPercentage(x)--這里以設(shè)置進(jìn)度條1作為例子??

????????????????progress3:setPercentage(x)??

numsTTF:setString(""..x.."%")??--這里以設(shè)置文字numsTTF的內(nèi)容作為例子,注意這里的x是數(shù)字,需要轉(zhuǎn)換為字符??

????????????end??

????????end??

????????--開始倒計(jì)時(shí)?每1秒調(diào)用一次Update方法??

run_logic?=?scheduler:scheduleScriptFunc(Update,1,false)??--第一個(gè)是調(diào)用的函數(shù)?第二個(gè)參數(shù)是時(shí)間??


????????--退出界面?卸載定時(shí)器??

????????local?function?onNodeEvent(event)??

if?"exit"?==?event?then??

???????????????cc.Director:getInstance():getScheduler():unscheduleScriptEntry(run_logic)??

???????????end??

????????end??

????????UILayer:registerScriptHandler(onNodeEvent)??

[plain]?view plain?copy

??

[plain]?view plain?copy

????????--?輸入框??

????????EditName?=?cc.EditBox:create(cc.size(180,?40),?cc.Scale9Sprite:create("btntest0.png"))??

????????--???EditName?=?cc.EditBox:create(editBoxSize,?cc.Scale9Sprite:create("btntest0.png"))??

????????EditName:setPosition(240,?100)??

????????--?--密碼輸入模式??

????????--?EditName:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)??

????????--?--單行輸入??

????????--?--EditName:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE)??

????????UILayer:addChild(EditName)??

????????--?????????--?開啟委托??

????????--?????????--?EditName:setDelegate(UILayer)??

????????--?--?local?targetPlatform?=?cc.Application:getInstance():getTargetPlatform()??

????????--?--?if?kTargetIphone?==?targetPlatform?or?kTargetIpad?==?targetPlatform?then??

????????--?--????EditName:setFontName("Paint?Boy")??

????????--?--?else??

????????--?--?????EditName:setFontName("fonts/Paint?Boy.ttf")??

????????--?--?end??

????????--?--輸入字符個(gè)數(shù)??

????????--?--?EditName:setMaxLength(10)??

????????--?--?EditName:setMaxLength(6)??

????????--?--輸入字體尺寸??

????????EditName:setFontSize(25)??

????????--?EditName:setFontSize(50)??

????????--?--輸入字體顏色??

????????--?--?EditName:setFontColor(cc.c3b(255,0,0))??

????????--?EditName:setFontColor(cc.c3b(0,255,0))??

????????--?--?EditName:setFontColor(cc.c3b(5,?4,?10))??

????????--?--設(shè)置editBox輸入為空時(shí)的顯示狀態(tài)??

????????--?EditName:setPlaceHolder("Nameiiiiiiiiii:")??

????????--?--?EditName:setPlaceHolder("點(diǎn)擊輸入姓名")???

????????--?--輸入前默認(rèn)顯示字體顏色??

????????--?EditName:setPlaceholderFontColor(cc.c3b(255,255,255))??

????????--?--?EditName:setMaxLength(8)??

????????--?EditName:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE?)??

????????--?--?--Handler--事件響應(yīng)??

????????--?--?EditName:registerScriptEditBoxHandler(editBoxTextEventHandle)??




????????return?UILayer??

????end??


????--?run場景?add圖層??

????local?sceneGame?=?cc.Scene:create()??

????sceneGame:addChild(createUILayer())??



????if?cc.Director:getInstance():getRunningScene()?then??

?????????cc.Director:getInstance():replaceScene(sceneGame)??

????else??

????????cc.Director:getInstance():runWithScene(sceneGame)??

????end??

end??


local?status,?msg?=?xpcall(main,?__G__TRACKBACK__)??

if?not?status?then??

????error(msg)??

end??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容