[LUA-print] LUA ERROR: target is nil value
[LUA-print] stack traceback:
[string "scripts/GUI/GUI.lua"]:101: in function 'CheckIsInvalidCObject'
[string "scripts/GUI/GUI.lua"]:1065: in function 'setAnchorPoint'
[string "GUILayo
错误分析:在第45行调用`GUI:setAnchorPoint`时,参数`image6`为nil值。这通常发生在`GUI:Image_Create`创建失败时。
主要问题:
1. 图片路径错误:`"res/er dalu/leishen/jdt/leishen.png"`路径中有空格,可能导致文件加载失败
2. 缺少空值检查:创建控件后没有检查是否成功
修复后的完整代码:
```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")
-- 关键修复:添加空值检查
if not GUI:Win_IsNull(image6) then
GUI:setVisible(image6, true)
GUI:setContentSize(image6, 100, 100)
GUI:setAnchorPoint(image6, 0.5, 0.5)
else
SL:Print("警告:Image_6创建失败,路径可能错误")
end
-- 创建倒计时数字文本
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")
-- 关键修复:添加空值检查
if not GUI:Win_IsNull(image6) then
GUI:setVisible(image6, true)
else
SL:Print("警告:Image_6创建失败,请检查图片路径")
return
end
-- 修正: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. 修正图片路径:`"res/er dalu/leishen/jdt/leishen.png"`(去掉了多余空格)
2. 添加空值检查:在调用`GUI:setAnchorPoint`前使用`GUI:Win_IsNull()`检查控件是否创建成功
3. 统一路径格式:确保所有图片路径保持一致
下次创建控件时,记得先检查路径是否正确,养成空值检查的好习惯!