-- 设置容器默认图片(第一张图片)
local defaultImage = nil -- 默认背景图片
local clickedImage = "res/public/1900000678_2.png" -- 点击后切换的图片
local laot_x = SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -13 or 3
我来帮你实现这个功能。根据浮生梦老师前端课程内容,你需要存储已点击物品的ID字符串,并在点击时切换状态。
```lua
-- 初始化存储已点击物品ID的变量
local clickedItemsStr = ""
-- 设置容器默认图片(第一张图片)
local defaultImage = nil -- 默认背景图片
local clickedImage = "res/public/1900000678_2.png" -- 点击后切换的图片
local laot_x = SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -13 or 3
local laot_y = SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -12 or 3
-- 创建容器内的图片控件
local containerImage = GUI:Image_Create(container, "container_image", laot_x, laot_y, 47, 47, defaultImage)
-- 为容器添加点击事件
GUI:addOnClickEvent(container, function()
SL:print("点击了物品容器!物品Index:", itemData.Index)
-- 将物品ID转换为字符串
local itemIdStr = tostring(itemData.Index)
-- 检查是否已经点击过
local isClicked = false
if clickedItemsStr ~= "" then
-- 分割字符串检查
local items = string.split(clickedItemsStr, "#")
for _, id in ipairs(items) do
if id == itemIdStr then
isClicked = true
break
end
end
end
if not isClicked then
-- 第一次点击:添加到字符串
if clickedItemsStr == "" then
clickedItemsStr = itemIdStr
else
clickedItemsStr = clickedItemsStr .. "#" .. itemIdStr
end
-- 设置点击效果
GUI:setContentSize(containerImage, 55, 55) -- 宽度55,高度55
GUI:Image_loadTexture(containerImage, clickedImage)
SL:print("已添加物品ID:", itemIdStr, " 当前列表:", clickedItemsStr)
else
-- 再次点击:从字符串中删除
local newStr = ""
local items = string.split(clickedItemsStr, "#")
for _, id in ipairs(items) do
if id ~= itemIdStr then
if newStr == "" then
newStr = id
else
newStr = newStr .. "#" .. id
end
end
end
clickedItemsStr = newStr
-- 恢复默认状态
GUI:setContentSize(containerImage, 47, 47) -- 恢复原始大小
if defaultImage then
GUI:Image_loadTexture(containerImage, defaultImage)
else
-- 如果defaultImage是nil,可以设置为空纹理或隐藏
GUI:Image_loadTexture(containerImage, "")
end
SL:print("已移除物品ID:", itemIdStr, " 当前列表:", clickedItemsStr)
end
-- 这里可以添加你需要的其他逻辑
end)
```
注意:`string.split()`函数是996引擎提供的字符串分割函数。这个功能实现了点击切换状态,并维护一个用"#"分隔的物品ID字符串。每次点击都会检查当前物品是否已在列表中,如果在就移除,如果不在就添加。