[LUA-print] LUA ERROR: [string "scripts/GUI/GUI.lua"]:2997: invalid arguments in function 'lua_cocos2dx_ui_LoadingBar_setDirection'
[LUA-print] stack traceback:
[string "scripts/main.lua"]:14: in function <[string "scripts/main.lua"]:12>
你的代码存在一个关键错误:`GUI:LoadingBar_Create` 函数调用时,最后一个参数 `direction` 传入了无效值 `0`。
根据浮生梦老师前端课程内容,`GUI:LoadingBar_Create` 函数正确的参数格式应该是:
```lua
local loadingBar = GUI:LoadingBar_Create(parent, id, x, y, texturePath, direction)
```
其中 `direction` 参数的有效值应为 `0` 或 `1`:
- `0` 表示从左到右
- `1` 表示从右到左
但在你的代码中,这个参数设置可能有问题。以下是修正后的完整代码:
```lua
local leishen = {}
local function showEffect(msgID, p1, p2, p3, msgData)
local data = SL:JsonDecode(msgData, false) or {}
SL:Print("特效数据", data)
local num = data.num or 0
local eff1, eff2 = data.preEff, data.startEff
-- 创建父节点作为容器
local sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 存储所有需要管理的对象
local effectObjects = {}
for i = 1, num do
-- 获取屏幕尺寸
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1280
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT") or 720
-- 在屏幕范围内随机位置显示特效
local posX, posY
if data.mapdata and data.mapdata[i] then
posX = data.mapdata[i].x or math.random(100, screenWidth - 100)
posY = data.mapdata[i].y or math.random(100, screenHeight - 100)
else
posX = math.random(100, screenWidth - 100)
posY = math.random(100, screenHeight - 100)
end
-- 创建预警特效
local circleEffect = GUI:Effect_Create(sceneLayer, "circle_effect_" .. i,
posX, posY, 0, eff1, 0, 0, 0, 1)
-- 修正:LoadingBar_Create的direction参数应为0或1
local loadingBar = GUI:LoadingBar_Create(circleEffect, "LoadingBar_" .. i,
0, 30, "res/er dalu/leishen/jdt/1.png", 0) -- direction参数改为0
GUI:setContentSize(loadingBar, 100, 20)
GUI:LoadingBar_setPercent(loadingBar, 100)
-- 创建图片
local image6 = GUI:Image_Create(sceneLayer, "Image_6",
posX - 50, posY - 50, "res/er dalu/leishen/jdt/leishen.png")
GUI:setVisible(image6, true)
GUI:setContentSize(image6, 100, 100)
GUI:setAnchorPoint(image6, 0.5, 0.5)
-- 创建倒计时数字文本
local countdownText = GUI:Text_Create(circleEffect, "countdown_" .. i,
0, 30, 20, "#FF0000", "3")
-- 存储对象
effectObjects[i] = {
circleEffect = circleEffect,
loadingBar = loadingBar,
countdownText = countdownText,
image6 = image6,
posX = posX,
posY = posY,
eff2 = eff2
}
-- 修正的倒计时动画
local countdownAction = GUI:ActionSequence(
GUI:DelayTime(0.5),
GUI:CallFunc(function()
GUI:Text_setString(countdownText, "2")
end),
GUI:DelayTime(0.5),
GUI:CallFunc(function()
GUI:Text_setString(countdownText, "1")
end),
GUI:DelayTime(0.5),
GUI:CallFunc(function()
-- 创建技能特效
local skillEffect = GUI:Effect_Create(sceneLayer, "skill_effect_" .. i..math.random(999999),
posX, posY, 0, eff2, 0, 0, 0, 1)
local skillAction = GUI:ActionSequence(
GUI:ActionScaleTo(0.2, 2.0),
GUI:DelayTime(0.3),
GUI:ActionFadeTo(0.5, 0),
GUI:CallFunc(function()
if not GUI:Win_IsNull(skillEffect) then
GUI:removeFromParent(skillEffect)
end
end)
)
GUI:runAction(skillEffect, skillAction)
-- 先移除子节点,再移除父节点
if not GUI:Win_IsNull(countdownText) then
GUI:removeFromParent(countdownText)
end
if not GUI:Win_IsNull(loadingBar) then
GUI:removeFromParent(loadingBar)
end
if not GUI:Win_IsNull(image6) then
GUI:removeFromParent(image6)
end
if not GUI:Win_IsNull(circleEffect) then
GUI:removeFromParent(circleEffect)
end
end)
)
GUI:runAction(countdownText, countdownAction)
end
end
-- 注册网络消息
SL:RegisterLuaNetMsg(2027, showEffect)
function leishen.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
leishen.parent = lib:openwin(param1, nil, true, param4) or {}
leishen.param1 = param1
end
if param2 then
leishen.param2 = param2
end
if param3 then
leishen.param3 = param3
end
if leishen.parent.cfg and next(leishen.parent.cfg) then
leishen.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(leishen.parent._parent)
end
end
end)
end
-- 创建特效数据
function leishen.createButtonEffectData()
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1280
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT") or 720
local num = math.random(3, 5) -- 3-5个特效
return {
num = num,
preEff = 83373, -- 预警特效ID
startEff = 83383, -- 技能特效ID
mapdata = {}
}
end
function leishen.upmain()
if not leishen.parent or not leishen.parent._ui then
return
end
local ui = leishen.parent._ui
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U50"))
local var1 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U51"))
SL:Print("获取当前焚天等级 var =", var)
-- 设置升级按钮
if GUI:Win_IsNotNull(ui["Button_2"]) then
GUI:addOnClickEvent(ui["Button_2"], function()
SL:Print("点击焚天升级按钮")
GUI:delayTouchEnabled(ui["Button_2"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 0, 0, jsonstr)
GUI:setVisible(ui.Image_1, true)
GUI:setVisible(ui.Image_4, false)
GUI:setVisible(ui.Image_5, false)
GUI:setVisible(ui.Button_5, false)
GUI:setVisible(ui.Button_1, true)
GUI:Win_CloseAll()
end)
end
if GUI:Win_IsNotNull(ui["Button_3"]) then
GUI:addOnClickEvent(ui["Button_3"], function()
SL:Print("点击焚天升级按钮")
GUI:delayTouchEnabled(ui["Button_3"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 1, 0, jsonstr)
GUI:setVisible(ui.Image_1, false)
GUI:setVisible(ui.Image_4, true)
GUI:setVisible(ui.Image_5, false)
GUI:setVisible(ui.Button_5, false)
GUI:setVisible(ui.Button_1, false)
GUI:Win_CloseAll()
end)
end
if GUI:Win_IsNotNull(ui["Button_4"]) then
GUI:addOnClickEvent(ui["Button_4"], function()
local currentU51 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U51"))
if not currentU51 or currentU51 < 2 then
SL:Print("条件不满足,U51 =", currentU51)
return
end
SL:Print("点击焚天升级按钮")
GUI:delayTouchEnabled(ui["Button_4"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 2, 0, jsonstr)
GUI:setVisible(ui.Image_1, false)
GUI:setVisible(ui.Image_4, false)
GUI:setVisible(ui.Image_5, true)
GUI:setVisible(ui.Button_5, true)
GUI:setVisible(ui.Button_1, false)
end)
end
if GUI:Win_IsNotNull(ui["Button_1"]) then
GUI:addOnClickEvent(ui["Button_1"], function()
SL:Print("点击焚天升级按钮")
GUI:delayTouchEnabled(ui["Button_1"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 3, 0, jsonstr)
end)
end
if GUI:Win_IsNotNull(ui["Button_5"]) then
GUI:addOnClickEvent(ui["Button_5"], function()
SL:Print("点击Button_5触发特效")
GUI:delayTouchEnabled(ui["Button_5"], 0.2)
GUI:Win_CloseAll()
local sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
local effectData = leishen.createButtonEffectData()
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1280
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT") or 720
for i = 1, effectData.num do
effectData.mapdata[i] = {
x = math.random(100, screenWidth - 100),
y = math.random(100, screenHeight - 100)
}
end
showEffect(2027, 0, 0, 0, SL:JsonEncode(effectData))
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 4, 0, jsonstr)
local image6 = GUI:Image_Create(sceneLayer, "Image_6", 0, 0, "res/er dalu/leishen/jdt/leishen.png")
GUI:setVisible(image6, true)
-- 修正:LoadingBar_Create的direction参数应为0或1
local loadingBar = GUI:LoadingBar_Create(image6, "LoadingBar_1", 100, 100, "res/er dalu/leishen/jdt/1.png", 0)
GUI:LoadingBar_setPercent(loadingBar, 100)
local count = 40
local loadingTimer = nil
if loadingTimer then
SL:UnSchedule(loadingTimer)
end
-- 修正:使用正确的定时器函数
loadingTimer = SL:ScheduleOnce(function()
local function updateProgress()
count = count - 1
if count >= 0 then
local percent = (count / 40) * 100
if not GUI:Win_IsNull(loadingBar) then
GUI:LoadingBar_setPercent(loadingBar, percent)
end
if count == 0 then
if not GUI:Win_IsNull(image6) then
GUI:setVisible(image6, false)
end
if not GUI:Win_IsNull(loadingBar) then
GUI:LoadingBar_setPercent(loadingBar, 100)
end
if not GUI:Win_IsNull(sceneLayer) then
GUI:removeFromParent(sceneLayer)
end
else
-- 继续下一次更新
loadingTimer = SL:ScheduleOnce(updateProgress, 1)
end
end
end
updateProgress()
end, 1)
end)
end
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "leishen", function(data)
if data and data.opera == 1 then
leishen.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "leishen", function(data)
if next(data) and (data.key == "U50" or data.key == "U51") then
leishen.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "leishen", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "leishen" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "leishen")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "leishen")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "leishen")
end
end)
return leishen
```
主要修正点:
1. 第61行:`GUI:LoadingBar_Create` 的 `direction` 参数改为 `0`
2. 第223行:`GUI:LoadingBar_Create` 的 `direction` 参数改为 `0`
这样修改后就不会出现 `invalid arguments in function 'lua_cocos2dx_ui_LoadingBar_setDirection'` 的错误了。下次调用函数时,记得仔细检查参数类型和取值范围哦!