-- 金条
function stdmodefunc103(actor, item)
local item_name = getiteminfo(actor, item, 7)
local item_num = getbagitemcount(actor, item_name)
if item_num >= 1 then
setplaydef(actor, "N12", 1)
item_103main(actor, item)
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-06-01 18:33:08 |
👁️ 阅读: 3
我是lua后端老师!
小仙,你这个金条使用脚本的思路是正确的,但代码实现上有些地方需要优化。我来帮你整改一下,让脚本更清晰、更符合你的需求:
```lua
-- 金条使用功能
function stdmodefunc103(actor, item)
local item_name = getiteminfo(actor, item, 7)
local item_num = getbagitemcount(actor, item_name)
if item_num >= 1 then
setplaydef(actor, "N12", 1)
item_103main(actor, item)
stop(actor)
end
end
function item_103main(actor, item)
local item_name = getiteminfo(actor, item, 7)
local item_bind = getiteminfo(actor, item, 6) -- 获取绑定状态
release_print("金条绑定状态:", item_bind)
-- 根据绑定状态获取对应类型的金条数量
local item_num, money_text, money_id = 0, "", 0
if item_bind and item_bind == 0 then
-- 非绑定金条
item_num = getbagitemcount(actor, "金条", 1)
money_text = "万金币"
money_id = 1
else
-- 绑定金条
item_num = getbagitemcount(actor, "金条", 2)
money_text = "万绑定金币"
money_id = 3
end
if item_num >= 1 then
if getplaydef(actor, "N12") < 1 then
setplaydef(actor, "N12", 1)
end
local text = getplaydef(actor, "N12") * 100
local sc = [[
<Img|x=-562.0|y=-1049.0|width=2000|height=2000|esc=0|img=public/1900000651_1.png|link=@exit>
<Img|x=-2.0|y=2.0|show=4|bg=1|reload=0|img=public/button/exp/bg1.png|bagPos=1|move=0|loadDelay=0|esc=1|reset=1>
<Layout|x=545|y=0|width=80|height=80|link=@exit>
<Button|x=389.0|y=2.0|pimg=public/1900000511.png|nimg=public/1900000510.png|link=@exit>
<ItemShow|x=36.0|y=63.0|width=70|height=70|itemcount=]] .. item_num .. [[|showtips=1|itemname=]] .. item_name .. [[>
<RText|x=111.0|y=140.0|outlinecolor=0|size=16|outline=1|color=255|text=<使用获得: /FCOLOR=7><]] .. text .. money_text .. [[/FCOLOR=251>>
<Button|x=281.0|y=65.0|size=18|color=255|nimg=public/button/exp/+.png|pimg=public/button/exp/++.png|link=@item_103,]] .. item .. [[,+>
<Button|x=116.0|y=65.0|size=18|color=255|nimg=public/button/exp/-.png|pimg=public/button/exp/--.png|link=@item_103,]] .. item .. [[,->
<Text|a=4|x=213.0|y=80.0|size=15|color=7|text=已选择: $STM(SLIDERV_N12)个>
<Slider|x=124.0|y=111.0|width=180|defvalue=]] .. getplaydef(actor, "N12") .. [[|maxvalue=]] .. item_num .. [[|sliderid=N12|link=@item_103,]] .. item .. [[,滑动>
<Button|x=147.0|y=181.0|size=15|pimg=public/button/exp/an2.png|nimg=public/button/exp/an1.png|color=161|text=使用金条|link=@item_103,]] .. item .. [[,使用物品,]] .. item_bind .. [[,]] .. money_id .. [[>
]]
say(actor, sc)
stop(actor)
end
end
function item_103(actor, item, action_type, item_bind, money_id)
local item_name = getiteminfo(actor, item, 7)
local N12_num = getplaydef(actor, "N12")
-- 处理滑动条
if action_type == "滑动" then
if N12_num < 1 then
setplaydef(actor, "N12", 1)
stdmodefunc103(actor, item)
end
local num = getconst(actor, "<$NPCPARAMS(3,N12)>")
setplaydef(actor, "N12", num)
end
-- 处理加减按钮
local current_item_num = 0
if item_bind and tonumber(item_bind) == 0 then
current_item_num = getbagitemcount(actor, "金条", 1) -- 非绑定数量
else
current_item_num = getbagitemcount(actor, "金条", 2) -- 绑定数量
end
if action_type == "+" and N12_num < current_item_num then
setplaydef(actor, "N12", N12_num + 1)
end
if action_type == "-" and N12_num > 1 then
setplaydef(actor, "N12", N12_num - 1)
end
-- 处理使用物品
if action_type == "使用物品" then
item_bind = tonumber(item_bind)
money_id = tonumber(money_id)
-- 检查数量是否足够
if current_item_num < N12_num then
say(actor, "金条数量不足!")
return
end
-- 扣除对应类型的金条
local take_count = N12_num
local items = getbagitems(actor)
if items ~= "0" and take_count > 0 then
for k, item_obj in ipairs(items) do
if getiteminfo(actor, item_obj, 7) == "金条" then
local obj_bind_state = getiteminfo(actor, item_obj, 6)
-- 只扣除对应绑定状态的金条
if (item_bind == 0 and obj_bind_state == 0) or
(item_bind > 0 and obj_bind_state > 0) then
local item_uid = getiteminfo(actor, item_obj, 1)
delitembymakeindex(actor, item_uid, take_count, "双击金条")
break
end
end
end
end
-- 给予对应货币
local money_num = N12_num * 1000000
local money_name = "金币"
if money_id == 3 then
money_name = "绑定金币"
end
changemoney(actor, money_id, "+", money_num, "双击金条", true)
sendmsg(actor, 1, '{"Msg":"[获得'.. money_name ..'] ' .. N12_num * 100 .. '万","FColor":255,"BColor":219,"Type":1}')
-- 重置选择数量
setplaydef(actor, "N12", 1)
if getbagitemcount(actor, "金条") < 1 then
close(actor)
return
end
end
-- 重新打开界面
item_103main(actor, item)
end
```
**主要改进点:**
1. **绑定状态判断更清晰**:使用`getiteminfo(actor, item, 6)`获取绑定状态,0=非绑定,>0=绑定
2. **货币类型传递**:在按钮链接中传递了`money_id`参数,1号货币=金币,3号货币=绑定金币
3. **精确扣除**:在扣除物品时,只扣除对应绑定状态的金条,避免扣错类型
4. **货币给予**:根据绑定状态给予对应的货币类型
5. **数量检查**:增加了数量检查,确保不会扣除超过拥有的数量
**使用说明:**
- 非绑定金条(绑定状态=0) → 给予1号货币(金币)
- 绑定金条(绑定状态>0) → 给予3号货币(绑定金币)
- 每次使用后重置选择数量为1
- 金条用完自动关闭界面
小仙,这样修改后你的脚本就完全符合需求了!如果还有不明白的地方,随时问我哦!
← 返回问答库