美文网首页
cocos-lua中的UI使用方式

cocos-lua中的UI使用方式

作者: 东方小白 | 来源:发表于2019-03-13 14:26 被阅读0次

    -----------------------------LayerColor--------------------------------
    local layer = cc.LayerColor:create(cc.c4b(0, 0, 0, 180))
    layer:addChild(layer)

    ------------------------------Label1--------------------------------
    local textContent = cc.Label:createWithTTF("", "2.ttf", fontSize) --字符串、ttf字体文件、字体大小
    --ttfLabel = cc.Label:create("", activeLayer:GetTtfFont(), 22)
    textContent:setTextVerticalAlignment(cc.TextVAlignment.CENTER) --垂直对齐方式
    textContent:setTextHorizontalAlignment(cc.TextHAlignment.LEFT) --水平对齐方式
    textContent:setDimensions(520, 520)
    textContent:enableOutline(cc.c4b(87, 0, 0, 255), 1.5) --描边
    textContent:setAnchorPoint(cc.p(0,0.5)) --锚点
    --中间(0.5f, 0.5f);
    --左下角(0.0f, 0.0f);
    --左上角(0.0f, 1.0f);
    --右下角(1.0f, 0.0f);
    --右上角(1.0f, 1.0f);
    --右中间(1.0f, 0.5f);
    --左中间(0.0f, 0.5f);
    --上中间(0.5f, 1.0f);
    --下中间(0.5f, 0.0f);
    textContent:setPosition(620, 300)
    layer:addChild(textContent)
    ------------------------------Label2--------------------------------
    local textContent = cc.Label:create("", "2.ttf", fontSize) --字符串、ttf字体文件、字体大小
    textContent:setAlignment(cc.TEXT_ALIGNMENT_LEFT,cc.VERTICAL_TEXT_ALIGNMENT_CENTER)
    -- textContent:setHorizontalAlignment(1)
    -- textContent:setVerticalAlignment(1)
    textContent:setDimensions(520, 520)
    textContent:setTextColor(cc.c4b(0xff, 0xf6, 0x00, 0xff))
    textContent:setVisible(false)
    textContent:enableBold()
    textContent:setSkewX(15)
    textContent:setAdditionalKerning(-2)
    textContent:enableOutline(cc.c4b(87, 0, 0, 255), 1.5) --描边
    textContent:setAnchorPoint(cc.p(0,0.5)) --锚点
    textContent:enableShadow(cc.c4b(0, 0, 0, 255), cc.size(0, -1),0)
    --中间(0.5f, 0.5f);
    --左下角(0.0f, 0.0f);
    --左上角(0.0f, 1.0f);
    --右下角(1.0f, 0.0f);
    --右上角(1.0f, 1.0f);
    --右中间(1.0f, 0.5f);
    --左中间(0.0f, 0.5f);
    --上中间(0.5f, 1.0f);
    --下中间(0.5f, 0.0f);
    textContent:setPosition(620, 300)
    layer:addChild(textContent)

    ------------------------------LabelAtlas--------------------------------
    local label = cc.LabelAtlas:_create("HelloWorld", "fonts/tuffy_bold_italic-charmap.png", 48, 66, string.byte(" "))
    label:setPosition(cc.p(size.width/2 - label:getContentSize().width / 2,size.height - label:getContentSize().height))
    layer:addChild(label, 1)

    ------------------------------LabelBMFont--------------------------------
    local label = cc.LabelBMFont:create("HelloWorld", "fonts/BMFont.fnt")
    label:setPosition(cc.p(size.width/2,size.height - label:getContentSize().height))
    layer:addChild(label, 1)
    ------------------------------Sprite--------------------------------
    local frameCache = cc.SpriteFrameCache:getInstance()
    frameCache:addSpriteFramesWithFile("SpirteSheet.plist")

    --第一种
    local bgSprite = cc.Sprite:create("facebooklike.png")--从本地获取
    bgSprite:initWithFile(activeLayer:addImgFile(string.format("%sac_xz_%d.png", activePath,id)))
    bgSprite:setPosition(630, 280)
    btCue1:setLocalZOrder(1)
    btCue1:setBrightStyle(0)--只有0和1、-1 NONE = -1,NORMAL = 0,HIGHLIGHT = 1
    mainLayer:addChild(bgSprite)

    --第二种
    local tree1 = cc.Sprite:create("tree1.png",cc.rect(604, 38, 302, 295))
    tree1:setPosition(cc.p(200,230))
    layer:addChild(tree1,0)

    --第三种
    local cache = cc.Director:getInstance():getTextureCache():addImage("tree1.png")
    local tree2 = cc.Sprite:create()
    tree2:setTexture(cache)
    tree2:setTextureRect(cc.rect(73, 72,182,270))
    tree2:setPosition(cc.p(500,200))
    layer:addChild(tree2,0)

    --第四种
    local hammerSprite = cc.Sprite:createWithSpriteFrameName("cr_hammer_icon_b.png")--从plist文件获取
    --hammerSprite:initWithSpriteFrameName("ac_everyday_public_bg.png")
    hammerSprite:setPosition(150, 220)
    imgIcon:addChild(hammerSprite, 5)

    --第五种
    local frameCrop = cc.SpriteFrame:create("crop.png", cc.rect(0, 0, 105, 95))
    local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop);
    spriteCrop:setPosition(210, 40 )
    layerFarm:addChild(spriteCrop)

    --第六种
    local heroSpriteFrame = frameCache:getSpriteFrameByName("hero1.png")
    local hero1 = cc.Sprite:createWithSpriteFrame(heroSpriteFrame)
    hero1:setPosition(cc.p(800,200))
    layer:addChild(hero1,0)

    ------------------------------MenuItemSprite--------------------------------
    local function callback()
    --实现内容
    end
    local normalSp = cc.Sprite:create("facebooklike_normal.png")
    local pressSp = cc.Sprite:create("facebooklike_press.png")
    local disableSp = cc.Sprite:create("facebooklike_press.png")
    --或者
    -- local normalSp = cc.Scale9Sprite:createWithSpriteFrameName("facebooklike_normal.png")--可以从本地获取也可以从plist文件获取
    -- local pressSp = cc.Scale9Sprite:createWithSpriteFrameName("facebooklike_normal.png")
    -- local normalSp:setContentSize(cc.size(170, 60))
    -- local pressSp:setContentSize(cc.size(170, 60))
    -- btReg:setNormalImage(btn_normal)
    -- btReg:setSelectedImage(btn_normal)
    -- btReg:setDisabledImage(btn_normal)
    -- btReg:setEnabled(false)
    local btReg = cc.MenuItemSprite:create(normalSp,pressSp,disableSp)
    btReg:registerScriptTapHandler(callback)
    local btMenu = cc.Menu:create(btReg)
    btMenu:setPosition(720, 75)
    layer:addChild(btMenu, 2)
    ------------------------------MenuItemImage--------------------------------
    local menuPopupItem = cc.MenuItemImage:create("menu2.png", "menu2.png")
    menuPopupItem:setPosition(0, 0)
    menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
    menuPopup = cc.Menu:create(menuPopupItem)
    menuPopup:setPosition(480.320)
    menuPopup:setVisible(false)
    layer:addChild(menuPopup)

    local function menuCallbackClosePopup()
    --实现内容
    end

    ------------------------------MenuItemFont、MenuItemLabel--------------------------------
    cc.MenuItemFont:setFontName("Times New Roman")
    cc.MenuItemFont:setFontSize(86)

    local item1 = cc.MenuItemFont:create("Start")
    local function menuItem1Callback(sender)
    cclog("Touch Start Menu Item.")
    end
    item1:registerScriptTapHandler(menuItem1Callback)

    local labelAtlas = cc.LabelAtlas:create("Help","menu/tuffy_bold_italic-charmap.png", 48, 65, string.byte(' '))
    local item2 = cc.MenuItemLabel:create(labelAtlas)
    local function menuItem2Callback(sender)
    cclog("Touch Help Menu Item.")
    end
    item2:registerScriptTapHandler(menuItem2Callback)

    local mn = cc.Menu:create(item1, item2)
    mn:alignItemsVertically()
    layer:addChild(mn)

    ------------------------------MenuItemToggle------------------------------
    -- 音效
    local soundOnMenuItem = cc.MenuItemImage:create("menu/on.png", "menu/on.png")
    local soundOffMenuItem = cc.MenuItemImage:create("menu/off.png", "menu/off.png")
    local soundToggleMenuItem = cc.MenuItemToggle:create(soundOnMenuItem, soundOffMenuItem)
    soundToggleMenuItem:setPosition(director:convertToGL(cc.p(818, 220)))
    local function menuSoundToggleCallback(sender)
    cclog("Sound Toggle.")
    end
    soundToggleMenuItem:registerScriptTapHandler(menuSoundToggleCallback)

    -- 音乐
    local musicOnMenuItem = cc.MenuItemImage:create("menu/on.png", "menu/on.png")
    local musicOffMenuItem = cc.MenuItemImage:create("menu/off.png", "menu/off.png")
    local musicToggleMenuItem = cc.MenuItemToggle:create(musicOnMenuItem, musicOffMenuItem)
    musicToggleMenuItem:setPosition(director:convertToGL(cc.p(818, 362)))
    local function menuMusicToggleCallback(sender)
    cclog("Music Toggle.")
    end
    musicToggleMenuItem:registerScriptTapHandler(menuMusicToggleCallback)

    -- Ok按钮
    local okMenuItem = cc.MenuItemImage:create(
    "menu/ok-down.png",
    "menu/ok-up.png")
    okMenuItem:setPosition(director:convertToGL(cc.p(600, 510)))

    local mn = cc.Menu:create(soundToggleMenuItem, musicToggleMenuItem,okMenuItem)
    mn:setPosition(cc.p(0, 0))
    layer:addChild(mn)

    ------------------------------EditBox--------------------------------
    local sprite = ccui.Scale9Sprite:create(activeLayer:addImgFile(string.format("%sad_activity_codebg.png", activePath)))
    local codeInput = ccui.EditBox:create(cc.size(350, 50), sprite,sprite,sprite)
    codeInput:setPlaceholderFont(activeLayer:GetTtfFont(), 24)
    codeInput:setFont(activeLayer:GetTtfFont(), 24)
    if appLoc ~= 1 and appLoc ~= 16 then
    codeInput:setPlaceholderFont(activeLayer:GetTtfFont(), 22)
    codeInput:setFont(activeLayer:GetTtfFont(), 22)
    end
    codeInput:setPlaceHolder(getLocalString("input_tip", appLoc));
    codeInput:setFontColor(cc.c3b(255, 255, 255))
    codeInput:setMaxLength(24)
    codeInput:setPosition(cc.p(cPosX,150))
    bgSprite:addChild(codeInput)

    local function editboxEventHandler(eventType)
    if eventType == "began" then --编辑框开始编辑时调用
    strFmt = string.format("editBox %p DidBegin !", codeInput)
    print(strFmt)
    elseif eventType == "ended" then --编辑框完成时调用
    strFmt = string.format("editBox %p DidEnd !", codeInput)
    print(strFmt)
    elseif eventType == "return" then --编辑框return时调用
    strFmt = string.format("editBox %p was returned !",codeInput)
    --判断是哪个编辑框,在多个编辑框同时绑定此函数时 需判断时哪个编辑框
    if codeInput == EditName then
    --当编辑框EditName 按下return 时到此处
    elseif codeInput == EditPassword then
    --当编辑框EditPassword 按下return 时到此处
    elseif codeInput == EditEmail then
    --当编辑框EditEmail 按下return 时到此处
    end
    print(strFmt)
    elseif eventType == "changed" then --编辑框内容改变时调用
    strFmt = string.format("editBox %p TextChanged, text: %s ", codeInput, codeInput:getText())
    print(strFmt)
    end
    end
    codeInput:registerScriptEditBoxHandler(editboxEventHandler)

    ------------------------------Button--------------------------------
    local bt = ccui.Button:create()
    bt:loadTextures("cr_btn_gr_n.png", "cr_btn_gr_p.png", "", 1) --1:从plist文件中获取 0:从本地获取资源
    bt:setCascadeOpacityEnabled(true)--开启透明度
    bt:setScale9Enabled(true)
    bt:setContentSize(cc.size(280, 60)) --按钮的大小
    bt:loadTextureNormal("132.png",1)
    bt:setPosition(cc.p(180, 270 - i * 90))
    bt:setTouchEnabled(false)
    bt:setEnabled(false)
    bt:setBright(false)
    bt:setTitleText("123")--按钮上的字
    bt:setTitleFontName("Arial")
    bt:setTitleFontSize(22)
    layer:addChild(bt)
    bt:addTouchEventListener(
    function(sender, eventType)
    if eventType == ccui.TouchEventType.ended then
    --实现内容
    end
    end
    )

    -- bt:addTouchEventListener(upBtnCallback)
    -- local function upBtnCallback(sender, eventType)
    -- if eventType == ccui.TouchEventType.ended then
    -- --实现内容
    -- end
    -- end
    ------------------------------ImageView--------------------------------
    local imgIcon = ccui.ImageView:create()
    imgIcon:loadTexture("cr_gem_tag.png", 1)
    imgIcon:setPosition(cc.p(30, 30))
    imgBk:setScale9Enabled(true)
    imgBk:setCapInsets(cc.rect(15,15,1,1))
    imgBk:setContentSize(cc.size(360, 360))
    imgIcon:setTouchEnabled(false)
    imgIcon:setOpacity(200)
    layer:addChild(imgIcon)

    ------------------------------Text--------------------------------
    local num = ccui.Text:create()
    num:setFontName(appFont)
    num:setFontSize(22)
    num:setColor(cc.c3b(255, 255, 255))
    num:setString(tostring(btText.num))
    num:setTextAreaSize(cc.size(240, 60))
    num:setPosition(cc.p(65, 30))
    num:setTouchEnabled(false)
    num:ignoreContentAdaptWithSize(false) --自动换行
    num:setContentSize(cc.size(560, 460))
    num:setTextHorizontalAlignment(cc.TEXT_ALIGNMENT_LEFT)--水平对齐方式
    num:setTextVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_CENTER)--垂直对齐方式
    num:getVirtualRenderer():enableShadow(cc.c4b(0, 0, 0, 150), cc.size(2,-2)) --num:getVirtualRenderer()为Label类型
    layer:addChild(num)

    ------------------------------tableView--------------------------------
    tableView = cc.TableView:create(cc.size(810, 440))
    tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)
    tableView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN)
    tableView:setColor(cc.c3b(255,255,255))
    tableView:setPosition(cc.p(10, 10))
    tableView:setDelegate()
    imgBk:addChild(tableView)
    tableView:registerScriptHandler(function(tableView)
    return 1
    end, cc.NUMBER_OF_CELLS_IN_TABLEVIEW)

    tableView:registerScriptHandler(function(tableView, idx)
    return 960, 640
    end, cc.TABLECELL_SIZE_FOR_INDEX)

    tableView:registerScriptHandler(function(tableView, idx)
    local pDelCell = tableView:dequeueCell()
    while (pDelCell ~= nil) do
    pDelCell:removeAllChildren()
    pDelCell:removeFromParent()
    pDelCell = tableView:dequeueCell()
    end
    local cell = tableView:dequeueCell()
    if cell == nil then
    cell = cc.TableViewCell:create()
    local cellBg = ccui.ImageView:create("ac_bk.png", 1)
    cellBg:setPosition(cc.p(cellWidth / 2.0, cellHeight / 2.0 + 5))
    cellBg:setScale9Enabled(true)
    cellBg:setContentSize(cc.size(cellWidth - 10,90))
    cell:addChild(cellBg)
    end
    return cell
    end, cc.TABLECELL_SIZE_AT_INDEX)
    tableView:reloadData()

    -- tableView:registerScriptHandler(CueResultLayer.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL)
    -- tableView:registerScriptHandler(CueResultLayer.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM)
    -- tableView:registerScriptHandler(CueResultLayer.tableCellTouched,cc.TABLECELL_TOUCHED)
    -- tableView:registerScriptHandler(CueResultLayer.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX)
    -- tableView:registerScriptHandler(CueResultLayer.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX)
    -- tableView:registerScriptHandler(CueResultLayer.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW)
    -- function CueResultLayer.scrollViewDidScroll(view)
    -- end

    -- function CueResultLayer.scrollViewDidZoom(view)
    -- end

    -- function CueResultLayer.tableCellTouched(table,cell)
    -- end

    -- function CueResultLayer.cellSizeForTable(table,idx)
    -- return 960,640
    -- end

    -- function CueResultLayer.tableCellAtIndex(table, idx)

    -- local cell = table:dequeueCell()
    -- while ( cell ~= nil ) do
    -- cell:removeFromParent()
    -- cell = table:dequeueCell()
    -- end

    -- if nil == cell then
    -- cell = cc.TableViewCell:new()

    -- local propId, propgrade = CueResultLayer:analysisInfo(CueResultLayer.reslutList[idx + 1])

    -- local cellBg = ccui.Scale9Sprite:createWithSpriteFrameName("Gray_Bg.png")
    -- cellBg:setContentSize(cc.size(CELL_WIDTH, CueResultLayer._cellHeight[idx]))
    -- cellBg:setPosition(cc.p(CELL_WIDTH/2, CueResultLayer._cellHeight[idx] / 2))
    -- cell:addChild(cellBg)
    -- end

    -- return cell
    -- end

    -- function CueResultLayer.numberOfCellsInTableView(table)
    -- return 1
    -- end
    ------------------------------ListView--------------------------------
    local listView = ccui.ListView:create()
    listView:setContentSize(cc.size(500,95))
    listView:setPosition(5, 50)
    listView:setDirection(ccui.ScrollViewDir.vertical)
    listView:setGravity(ccui.ListViewGravity.centerVertical)
    listView:setBounceEnabled(true)
    listView:setItemsMargin(25)
    listView:setScrollBarEnabled(false)
    listView:pushBackDefaultItem()
    layer:addChild(listView)

    for i = 1, 3 do
    local custom_item = ccui.Layout:create()
    custom_item:setContentSize(cc.size(580, 140))
    listView:pushBackCustomItem(custom_item)

    local spBg = cc.Sprite:create(activeLayer:addImgFile(activePath .. "row_show.png"))
    spBg:setAnchorPoint(cc.p(0,0))
    custom_item:addChild(spBg);
    

    end

    ------------------------------LoadingBar--------------------------------
    local progress = ccui.LoadingBar:create()
    progress:loadTexture("ac_pross.png", 1)
    progress:setPercent(0)
    progress:setPosition(627, 345)
    layer:addChild(progress,2)

    ------------------------------RichText--------------------------------
    local richText = ccui.RichText:create()
    richText:ignoreContentAdaptWithSize(true)
    richText:setAnchorPoint(cc.p(0.5,0.5))
    local re1 = ccui.RichElementText:create(1, cc.c3b(0xa3, 0xa5, 0xe6), 255, "123", "Arial", 20)
    local re2 = ccui.RichElementText:create(2, cc.c3b(0x22,0x9d,0x60), 255, "123", "Arial", 20)
    richText:pushBackElement(re1)
    richText:pushBackElement(re2)
    layer:addChild(richText)

    ------------------------------TextField--------------------------------
    local codeInput = ccui.TextField:create(getLocalString("input_tip", appLoc),activeLayer:GetTtfFont(), 24)
    codeInput:setTouchAreaEnabled(true)
    codeInput:setTouchSize(cc.size(350,60))
    codeInput:setMaxLengthEnabled(true)
    codeInput:setTouchEnabled(true)
    codeInput:setCursorEnabled(true)
    codeInput:setColor(cc.c3b(255, 255, 255))
    codeInput:setMaxLength(24)
    codeInput:setScaleX(1/1.4)
    codeInput:setAnchorPoint(cc.p(0,0.5))
    codeInput:setPosition(cc.p(0,22))
    codeInput:addEventListener(function(sender, eventType)
    if eventType == 0 then --键盘弹出
    if codeInput:getStringLength() <= 2 then
    codeInput:setAnchorPoint(cc.p(0.5,0.5))
    end
    if codeInput:getString() ~= "" then
    codeInput:setAnchorPoint(cc.p(0,0.5))
    end
    elseif eventType == 1 then --键盘隐藏
    if codeInput:getString() == "" then
    codeInput:setString("")
    codeInput:setAnchorPoint(cc.p(0,0.5))
    end
    elseif eventType == 2 then --输入文本
    codeInput:setAnchorPoint(cc.p(0,0.5))
    elseif eventType == 3 then --删除文本
    codeInput:setAnchorPoint(cc.p(0,0.5))
    end
    end)
    layer:addChild(codeInput)

    ------------------------------CocosStudio--------------------------------
    --获取widget
    local rootWidget = ccs.GUIReader:getInstance():widgetFromJsonFile("ChristmasLayer.ExportJson") --cocos studio
    layer:addChild(rootWidget)
    --获取对象
    --第一种方式 父类获取子类
    local labelTitle = rootWidget:getChildByName("Label_title")
    labelTitle:setFontName("..ttf") --字体名称
    labelTitle:setFontSize(18)--字体大小
    labelTitle:getVirtualRenderer():enableOutline(cc.c4b(255, 255, 0, 255), 1.0)--描边
    --第二种方式
    local labelTitle = ccui.Helper:seekWidgetByName(rootWidget,"Label_title")
    --注意,从CocosStudio获取对象,如果是文本框(Text)、输入框(EditBox)、按钮(Button)、图片(ImageView)

    ------------------------------动画Animate--------------------------------
    --蛋裂开动画
    local spriteFrame = cc.SpriteFrameCache:getInstance()
    spriteFrame:addSpriteFramesWithFile("run.plist") --添加plist文件
    local animation = cc.Animation:create()
    for i = 1, 25 do
    --第一种
    -- local frameName = string.format("h%d.png",i)
    -- cclog("frameName = %s",frameName)
    -- local spriteFrame = spriteFrame:getSpriteFrameByName(frameName)
    -- animation:addSpriteFrame(spriteFrame)

    --第二种
    local frameName = string.format("fire/campFire%02d.png",i)
    cclog("frameName = %s",frameName)
    animation:addSpriteFrameWithFile(frameName)
    

    end

    animation:setDelayPerUnit(1. / 25.0)
    animation:setRestoreOriginalFrame(true)
    local action = cc.Animate:create(animation)

    local breakSprite = cc.Sprite:createWithSpriteFrameName("egg_" .. c .. " (1).png")
    breakSprite:setPosition(87, 191)
    imgIcon:addChild(breakSprite, 4, 4)
    breakSprite:runAction(cc.Sequence:create(cc.DelayTime:create(0.3), action, cc.RemoveSelf:create()))

    --第二种
    local frameWidth = 105
    local frameHeight = 95

    -- create dog animate
    local textureDog = cc.Director:getInstance():getTextureCache():addImage("dog.png")
    local rect = cc.rect(0, 0, frameWidth, frameHeight)
    local frame0 = cc.SpriteFrame:createWithTexture(textureDog, rect)
    rect = cc.rect(frameWidth, 0, frameWidth, frameHeight)
    local frame1 = cc.SpriteFrame:createWithTexture(textureDog, rect)

    local spriteDog = cc.Sprite:createWithSpriteFrame(frame0)
    spriteDog:setPosition(self.origin.x, self.origin.y + self.visibleSize.height / 4 * 3)
    spriteDog.isPaused = false

    local animation = cc.Animation:createWithSpriteFrames({frame0,frame1}, 0.5)
    local animate = cc.Animate:create(animation);
    spriteDog:runAction(cc.RepeatForever:create(animate))
    ------------------------------XML动画Armature--------------------------------
    ccs.ArmatureDataManager : getInstance() : addArmatureFileInfo(appPath .. "xiaochou01.png",appPath .. "xiaochou01.plist", appPath .. "xiaochou01.xml")
    local armature = ccs.Armature : create("xiaochoudonghua")
    armature : getAnimation() : playWithIndex(0)
    armature : setAnchorPoint(cc.p(0.5, 0.5))
    armature : setPosition(170, 200)
    armature : getAnimation() : setSpeedScale(20 / 80)
    armature : setScale(0.8)
    layer : addChild(armature)

    ------------------------------粒子特效--------------------------------
    local particleSystem = cc.ParticleSystemQuad:create("snow.plist")
    particleSystem:setPosition(cc.p(size.width/2, size.height + 50))
    layer:addChild(particleSystem)

    ------------------------------动作Action--------------------------------
    local action1 = cc.MoveTo:create(0.5, cc.p(480, -320))
    local callfunc = cc.CallFunc:create(HideLayerFinishCallBack)
    local action2 = cc.EaseExponentialOut:create(action1)
    local action = cc.Sequence:create(action2, callfunc)
    layer:runAction(action)

    local function CallBack1()
    sprite:runAction(cc.TintBy:create(0.5, 255, 0, 255))
    end
    function MyActionScene:OnCallFunc()
    local acf = cc.CallFunc:create(CallBack1)
    sprite:runAction(acf)
    end

    local function CallBack2(pSender)
    local sp = pSender
    sp:runAction(cc.TintBy:create(1, 255, 0, 255))
    end
    function MyActionScene:OnCallFuncN()
    local acf = cc.CallFunc:create(CallBack2)
    sprite:runAction(cc.Sequence:create(acf))
    end

    local function CallBack3(pSender, table)
    local sp = pSender
    cclog("CallBack3 %d", table[1])
    sp:runAction(cc.TintBy:create(table[1], table[2], table[3], table[4]))
    end
    function MyActionScene:OnCallFuncND()
    local acf = cc.CallFunc:create(CallBack3, {1, 255, 0, 255})
    sprite:runAction(acf)
    end
    ------------------------------遮罩ClippingNode--------------------------------
    local stencil = cc.Sprite:create()
    stencil:setTextureRect(cc.rect(0, 0, 420, 220))
    stencil:setColor(cc.c3b(255,255,255))
    local clippnode = cc.ClippingNode:create(stencil)
    clippnode:setPosition(600, 140)
    clippnode:setInverted(false);
    layer:addChild(clippnode)

    ------------------------------定时器scheduler--------------------------------
    --自定义定时器
    local function updateLeftTime(f)
    --实现内容
    end

    local schedule_updateLeftTime = nil
    local scheduler = cc.Director:getInstance():getScheduler() --
    schedule_updateLeftTime = scheduler:scheduleScriptFunc(updateLeftTime, 1.0, false) --时间调度器

    local function onNodeEvent(event) --退出事件停止调用定时器
    --有五个状态 enter、enterTransitionFinish、exit、exitTransitionStart、cleanup
    if event == "exitTransitionStart" then
    if schedule_updateLeftTime then
    scheduler:unscheduleScriptEntry(schedule_updateLeftTime)
    schedule_updateLeftTime = nil
    end
    end
    end
    --创建退出时候监听 某个节点退出
    node:registerScriptHandler(onNodeEvent)
    updateLeftTime(0)

    --默认定时器
    local function update(delta)
    local x,y = label:getPosition()
    label:setPosition(cc.p(x + 2, y - 2))
    end

    --开始游戏调度
    layer:scheduleUpdateWithPriorityLua(update, 0)

    function onNodeEvent(tag)
    if tag == "exit" then
    --开始游戏调度
    layer:unscheduleUpdate()
    end
    end
    layer:registerScriptHandler(onNodeEvent)

    ------------------------------单点触摸事件--------------------------------
    --注册触摸事件
    local function onTouchBegin(touch, event)
    return true
    end

    local function onTouchMoved(touch, event)
    local location = touch:getLocation()
    cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
    end

    local function onTouchEnded(touch, event)
    -- 获取事件所绑定的 node
    local node = event:getCurrentTarget()
    local locationInNode = node:convertToNodeSpace(touch:getLocation())
    local s = imgBk:getContentSize()
    local rect = cc.rect(150, 150, s.width-20, s.height)

    -- 点击范围判断检测
    if cc.rectContainsPoint(rect, locationInNode) == false then
    
    
    end
    

    end

    local listener = cc.EventListenerTouchOneByOne:create()
    listener:registerScriptHandler(onTouchBegin, cc.Handler.EVENT_TOUCH_BEGAN )
    listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED )
    listener:setSwallowTouches(true)
    local eventDispatcher = layer:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)

    -- local listener2 = listener1:clone()
    -- eventDispatcher:addEventListenerWithSceneGraphPriority(listener2, boxB)
    -- local listener3 = listener1:clone()
    -- eventDispatcher:addEventListenerWithSceneGraphPriority(listener3, boxC)

    相关文章

      网友评论

          本文标题:cocos-lua中的UI使用方式

          本文链接:https://www.haomeiwen.com/subject/prozpqtx.html