Lua游戏中常用到的一些动作

--动态数字
--@txt 文本节点
--@old 初始的数字
--@count 要增加的数字
--@callback 数字调完之后的回调函数
--@concat 单位
function UICommon.addNumEffect(txt,old,count,callback,concat)
  if count == 0 then
    txt:setString(old..(type(concat) == "string" and concat or ""))
    return
  end
  local time = 50
  local nowTime = 0
  local targetNum = old + count
  local perNum = nil

  local mod = count % time
  local isSub = count < 0
  if isSub then
    count = count + (time - (mod == 0 and time or mod))
    perNum = math.min(-1, math.floor(count / time))
  else
    count = count - mod
    perNum = math.max(1, math.floor(count / time))
  end

  local function func()
    old = old + perNum
    txt:setString(old..(type(concat) == "string" and concat or ""))
    if targetNum == old then
      txt:unscheduleUpdate() 
      if callback then callback() end
      return
    end
    
    nowTime = nowTime + 1
    if nowTime >= time then 
      if isSub then
        old = old - (time - (mod == 0 and time or mod))
      else
        old = old + mod
      end
      txt:setString(old..(type(concat) == "string" and concat or ""))
      txt:unscheduleUpdate() 
      if callback then callback() end
    end
  end
  txt:scheduleUpdateWithPriorityLua(func, 0)
end


--二级窗口弹出和关闭放大缩小的特效
function UICommon.openDialogAction(item,_scale,_dur)
    if item == nil then return end
    local scale =_scale or 0.9
    local dur =_dur or 0.2
    item:setScale(scale)
    local act1 = cc.EaseBackOut:create(cc.ScaleTo:create(dur, 1))
    item:runAction(act1)
end
function UICommon.closeDialogAction(item,callback)
    if item == nil then return end
    local act1 = cc.EaseBackIn:create(cc.ScaleTo:create(0.2, 0.9))
    local act2 = cc.CallFunc:create(callback)
    local seq = cc.Sequence:create(act1, act2)
    item:runAction(seq)
end


-- 进度条动画
function UICommon.loadingTo(item,per,speedScale,reverse,callback)
    if item == nil then return end
    local perOld = item:getPercent()
    if reverse == nil then reverse = false end
    local flag = (reverse and perOld <= per) or (reverse == false and  perOld >= per)  -- 是否要再加载一遍
    local speed = (per==perOld and 100 or math.abs(per-perOld))/60 * (speedScale or 1)
    local function add()
        perOld = perOld + speed
        if flag then
            if perOld >= 100 then
                perOld = -speed
                flag = false
            end
        else
            if perOld >= per or perOld >= 100 then
                item:unscheduleUpdate()
                if callback then callback() end
                perOld = per
            end
        end
        item:setPercent(perOld)
    end
    local function sub()
        perOld = perOld - speed
        if flag then
            if perOld <= 0 then
                perOld = 100 + speed
                flag = false
            end
        else
            if perOld <= per or perOld <= 0 then
                item:unscheduleUpdate()
                if callback then callback() end
                perOld = per
            end
        end
        item:setPercent(perOld)
    end
    if reverse then     -- 反向
        item:scheduleUpdateWithPriorityLua(sub, 1)
    else
        item:scheduleUpdateWithPriorityLua(add, 1)
    end
end

--输入框
--输入框统一通用
function UICommon.createEditBox(pnl,txt,color,holder,fontSize,maxNum,typeset,isSysFont,placeholderColor,offsetY)
    local inputSize = pnl:getContentSize()    
    local editMes = ccui.EditBox:create(inputSize,"assetsRes/res/beijing/beijing130.png")
    
    local function getTextNum()
        local strFmt = tools.filterSpecChars(editMes:getText())
        return strFmt
    end
    local function editBoxTextEventHandle(strEventName,pSender)
        if strEventName == "began" then
        elseif strEventName == "ended" then
        elseif strEventName == "return" then
            editMes:setText(getTextNum())
        elseif strEventName == "changed" then
            getTextNum()
        end
    end
    editMes:setText(txt)
    editMes:registerScriptEditBoxHandler(editBoxTextEventHandle)    
    editMes:setPosition(cc.p(inputSize.width/2, inputSize.height/2 + (offsetY and offsetY or 0)))
    if not isSysFont then
        editMes:setFontName(UICommon.getTTFName())
    end
    editMes:setFontSize(fontSize)
    editMes:setFontColor(color)
    editMes:setName("editMes")
    if placeholderColor then
        editMes:setPlaceholderFontColor(placeholderColor)
    end
    editMes:setPlaceholderFontSize(fontSize)
    editMes:setPlaceHolder(holder)
    if maxNum then
        editMes:setMaxLength(maxNum)
    end
    editMes:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE)
    pnl:addChild(editMes)
end

-- 重复放大缩小
function UICommon.repeScale(item,args1,args2)
    args1 = UICommon.checkValid(args1)
    args2 = UICommon.checkValid(args2)
    local act1 = cc.ScaleTo:create(args1.time, args1.x,args1.y)
    local act2 = cc.ScaleTo:create(args2.time, args2.x,args2.y)
    local seq  = cc.Sequence:create(act1, act2)
    local action = cc.RepeatForever:create(seq)
    item:runAction(action)
end

-- 重复渐隐和移动
function UICommon.repeatFadeAndMove(ltag, rtag)
    if not ltag or not rtag then return end
    local act1 = cc.MoveBy:create(1, {x=30})
    local act11 = act1:reverse()
    local act2 = cc.FadeOut:create(1)
    local act3 = cc.MoveBy:create(0, {x=-30})
    local act31 = act3:reverse()
    local act4 = cc.FadeIn:create(0.5)
    local seq1=cc.RepeatForever:create(cc.Sequence:create(cc.Spawn:create(act1,act2),act3,act4))
    local seq2=cc.RepeatForever:create(cc.Sequence:create(cc.Spawn:create(act11,act2),act31,act4))
    ltag:runAction(seq2:clone())
    rtag:runAction(seq1:clone())
end
-- 重复旋转
function UICommon.repeatRotateCircle(item)
    if item == nil then return end
    local repeatAction = cc.RepeatForever:create(cc.RotateBy:create(12, 360))
    item:runAction(repeatAction)
end
-- 重复变灰
function UICommon.repeatTintToGray(item)
    if item == nil then return end
    local action1 = cc.TintBy:create(0.5, -70, -70, -70)
    local action1Back = action1:reverse()
    local seq = cc.Sequence:create( action1, action1Back)
    local action = cc.RepeatForever:create(seq)
    item:runAction(action)
end
-- 停止变灰并恢复
function UICommon.stopTintAction(item)
    if item == nil then return end
    transition.stopTarget(item)
    item:runAction(cc.TintTo:create(0, 255, 255, 255))
end
-- 向下移动并慢慢显示
function UICommon.moveDownAndFadeIn(item,delay)
    if item == nil then return end
    item:setOpacity(0)
    item:moveBy({time = 0.6, y = -50})
    item:fadeIn({time = 0.6})
end
-- 重复来回透明度 
function UICommon.repeatFade(item)
    if item == nil then return end
    item:setOpacity(127)
    local action1 = cc.FadeTo:create(0.5,255)
    local action1Back = cc.FadeTo:create(0.5,127)
    local seq = cc.Sequence:create( action1, action1Back)
    local action = cc.RepeatForever:create(seq)
    item:runAction(action)
end
-- 停止来回透明度 
function UICommon.stopRepeatFade(item)
    if item == nil then return end
    transition.stopTarget(item)
    item:setOpacity(0)
end
-- 盖章效果
function UICommon.stampAction(item,cb,delayTime)
    if item == nil then return end
    local scaleNum = item:getScale()
    item:setScale(3 * scaleNum)
    item:setVisible(true)
    item:scaleTo({time = 0.2, scale = scaleNum, onComplete = function() delayCall(function() king:command("game.all.sealsomething") end, 50)  end})
    local delay = cc.DelayTime:create(math.max(delayTime or 0.25))
    local seal = cc.CallFunc:create(function()
        --音效
        if cb~=nil then
          cb();
        end
    end)
    local seq  = cc.Sequence:create(delay, seal)
    item:runAction(seq)
end
-- 向上移动并消失
function UICommon.moveUpAndFadeOut(item,delay)
    if item == nil then return end
    local act0 = cc.FadeIn:create(0)
    local act1 = cc.MoveBy:create(0.8, {y = 150})
    local act2 = cc.FadeOut:create(0.4)
    local act3 = cc.MoveBy:create(0, {y = -150})
    local seq  = cc.Sequence:create(act0, act1, act2, act3)
    local act  = transition.create(seq, {delay=delay or 0})
    item:runAction(act)
end
-- 渐隐消失
function UICommon.delayAndFadeOut(item,delay)
    if item == nil then return end
    local act0 = cc.FadeIn:create(0)
    local act1 = cc.DelayTime:create(delay)
    local act2 = cc.FadeOut:create(0.8)
    local act3 = cc.CallFunc:create(function()
        item:removeSelf()
    end)
    local seq  = cc.Sequence:create(act0, act1, act2, act3)
    item:runAction(seq)
end
-- 重复上下移动
function UICommon.repeatUpAndDown(item,spd,dis)
    if item == nil then return end
    local act1 = cc.MoveBy:create(spd or 1.2, {y = dis or 15})
    local seq  = cc.Sequence:create(act1, act1:reverse())
    local action = cc.RepeatForever:create(seq)
    item:runAction(action)
end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua游戏服务器框架是一种用于开发多人在线游戏(MMORPG)的框架。其一个常见的Lua游戏服务器框架是Giraffe,它是基于crossover框架开发的。Giraffe框架可以帮助开发者快速搭建类似bigworld引擎的多进程的MMORPG集群架构,也可以轻松地搭建其他游戏类型的集群架构。 你可以通过以下步骤来使用Giraffe框架搭建Lua游戏服务器: 1. 首先,你需要安装Lua和crossover框架。你可以在https://github.com/galenho/Giraffe.git上找到Giraffe框架的代码。 2. 下载Giraffe框架的代码,并将其添加到你的项目。 3. 在你的Lua代码,引入Giraffe框架的模块,并使用它提供的功能来构建你的游戏服务器。 4. 使用Giraffe框架提供的API来处理游戏逻辑、网络通信、数据库访问等功能。 5. 根据你的需求,配置Giraffe框架的集群架构,以支持多进程和分布式部署。 下面是一个简单的示例代码,展示了如何使用Giraffe框架创建一个简单的Lua游戏服务器: ```lua local giraffe = require("giraffe") -- 创建服务器实例 local server = giraffe.createServer() -- 处理客户端连接事件 server:on("connection", function(client) print("New client connected:", client.id) -- 处理客户端消息事件 client:on("message", function(message) print("Received message from client:", message) -- 处理游戏逻辑 -- 发送消息给客户端 client:send("Hello, client!") end) -- 处理客户端断开连接事件 client:on("disconnect", function() print("Client disconnected:", client.id) end) end) -- 启动服务器 server:listen(8080) print("Server started on port 8080") ``` 通过上述代码,你可以创建一个简单的Lua游戏服务器,并处理客户端的连接、消息和断开连接事件。你可以根据自己的需求,进一步扩展和定制服务器的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值