function DengjiTiShengA:YiJianHeCheng(actor)
print(666666)
local page = self._cfg
local cost = page[1].costs
local t1 = string.split(cost, "&")
local needItem, needItems = t1[1], t1[2]
local itemId, itemNum = string.match(need
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-04-28 22:06:58 |
👁️ 阅读: 2
我是lua后端老师!
小仙,你这个一键合成功能看起来挺有意思的,我来帮你看看怎么写。从你的代码来看,你已经在解析配置表中的消耗和产出数据了。
根据你的需求“10603#10&2#500000 10604#1”,我来帮你完善这个一键合成功能:
```lua
function DengjiTiShengA:YiJianHeCheng(actor)
release_print("开始一键合成")
local page = self._cfg
local cost = page[1].costs
local t1 = string.split(cost, "&")
local needItem, needItems = t1[1], t1[2]
local itemId, itemNum = string.match(needItem, "(%d+)#(%d+)")
local itemIds, itemNums = string.match(needItems, "(%d+)#(%d+)")
-- 获取合成次数(根据玩家拥有的等级丹数量计算)
local levelDanCount = getbagitemcount(actor, tonumber(itemId)) -- 获取背包中等级丹数量
local yuanbaoCount = getbindmoney(actor) -- 获取绑定元宝数量
-- 计算最大可合成次数
local maxTimesByDan = math.floor(levelDanCount / tonumber(itemNum)) -- 根据等级丹计算
local maxTimesByYuanbao = math.floor(yuanbaoCount / tonumber(itemNums)) -- 根据元宝计算
local maxTimes = math.min(maxTimesByDan, maxTimesByYuanbao)
if maxTimes <= 0 then
if levelDanCount < tonumber(itemNum) then
messagebox(actor, "等级丹数量不足,需要"..tonumber(itemNum).."个")
end
if yuanbaoCount < tonumber(itemNums) then
messagebox(actor, "绑定元宝不足,需要"..tonumber(itemNums).."个")
end
return
end
-- 询问玩家合成次数
local inputTimes = tonumber(messagebox(actor, "请输入合成次数(1-"..maxTimes.."次)", "1"))
if not inputTimes or inputTimes <= 0 or inputTimes > maxTimes then
messagebox(actor, "输入次数无效")
return
end
-- 计算总消耗
local totalDanNeeded = tonumber(itemNum) * inputTimes -- 需要的总等级丹数量
local totalYuanbaoNeeded = tonumber(itemNums) * inputTimes -- 需要的总元宝数量
-- 扣除物品
local takeDanResult = takeitemex(actor, tonumber(itemId), totalDanNeeded) -- 扣除等级丹
local takeYuanbaoResult = consumebindmoney(actor, totalYuanbaoNeeded) -- 扣除绑定元宝
if takeDanResult and takeYuanbaoResult then
-- 给予合成物品
local giveItemResult = giveitem(actor, tonumber(itemIds), tonumber(itemNums) * inputTimes) -- 给予千年圣丹
if giveItemResult then
messagebox(actor, "合成成功!获得"..(tonumber(itemNums) * inputTimes).."个千年圣丹")
release_print("一键合成成功", getbaseinfo(actor, 1), "消耗:", totalDanNeeded, "个等级丹和", totalYuanbaoNeeded, "元宝", "获得:", tonumber(itemNums) * inputTimes, "个千年圣丹")
else
-- 如果给予失败,返还扣除的物品
giveitem(actor, tonumber(itemId), totalDanNeeded)
changemoney(actor, totalYuanbaoNeeded, 1)
messagebox(actor, "合成失败,物品已返还")
end
else
messagebox(actor, "扣除物品失败,请检查背包空间")
end
end
```
**代码说明:**
1. 首先计算玩家当前拥有的等级丹和绑定元宝数量
2. 根据两种资源计算最大可合成次数
3. 让玩家输入想要合成的次数
4. 批量扣除等级丹和元宝
5. 批量给予千年圣丹
6. 如果给予失败,会返还扣除的资源
**需要注意的几点:**
1. `getbagitemcount`函数获取背包中指定物品的数量
2. `getbindmoney`函数获取绑定元宝数量
3. `takeitemex`函数扣除指定数量的物品
4. `consumebindmoney`函数扣除绑定元宝
5. `giveitem`函数给予物品
这样就能实现你想要的“一键扣除同时一件得到几次的千年圣丹”功能了!如果有不懂的地方随时问我,或者可以请教浮生梦尊师!
我是lua后端老师!
小仙,你这个一键合成功能看起来挺有意思的,我来帮你看看怎么写。从你的代码来看,你已经在解析配置表中的消耗和产出数据了。
根据你的需求“10603#10&2#500000 10604#1”,我来帮你完善这个一键合成功能:
```lua
function DengjiTiShengA:YiJianHeCheng(actor)
release_print("开始一键合成")
local page = self._cfg
local cost = page[1].costs
local t1 = string.split(cost, "&")
local needItem, needItems = t1[1], t1[2]
local itemId, itemNum = string.match(needItem, "(%d+)#(%d+)")
local itemIds, itemNums = string.match(needItems, "(%d+)#(%d+)")
-- 获取合成次数(根据玩家拥有的等级丹数量计算)
local levelDanCount = getbagitemcount(actor, tonumber(itemId)) -- 获取背包中等级丹数量
local yuanbaoCount = getbindmoney(actor) -- 获取绑定元宝数量
-- 计算最大可合成次数
local maxTimesByDan = math.floor(levelDanCount / tonumber(itemNum)) -- 根据等级丹计算
local maxTimesByYuanbao = math.floor(yuanbaoCount / tonumber(itemNums)) -- 根据元宝计算
local maxTimes = math.min(maxTimesByDan, maxTimesByYuanbao)
if maxTimes <= 0 then
if levelDanCount < tonumber(itemNum) then
messagebox(actor, "等级丹数量不足,需要"..tonumber(itemNum).."个")
end
if yuanbaoCount < tonumber(itemNums) then
messagebox(actor, "绑定元宝不足,需要"..tonumber(itemNums).."个")
end
return
end
-- 询问玩家合成次数
local inputTimes = tonumber(messagebox(actor, "请输入合成次数(1-"..maxTimes.."次)", "1"))
if not inputTimes or inputTimes <= 0 or inputTimes > maxTimes then
messagebox(actor, "输入次数无效")
return
end
-- 计算总消耗
local totalDanNeeded = tonumber(itemNum) * inputTimes -- 需要的总等级丹数量
local totalYuanbaoNeeded = tonumber(itemNums) * inputTimes -- 需要的总元宝数量
-- 扣除物品
local takeDanResult = takeitemex(actor, tonumber(itemId), totalDanNeeded) -- 扣除等级丹
local takeYuanbaoResult = consumebindmoney(actor, totalYuanbaoNeeded) -- 扣除绑定元宝
if takeDanResult and takeYuanbaoResult then
-- 给予合成物品
local giveItemResult = giveitem(actor, tonumber(itemIds), tonumber(itemNums) * inputTimes) -- 给予千年圣丹
if giveItemResult then
messagebox(actor, "合成成功!获得"..(tonumber(itemNums) * inputTimes).."个千年圣丹")
release_print("一键合成成功", getbaseinfo(actor, 1), "消耗:", totalDanNeeded, "个等级丹和", totalYuanbaoNeeded, "元宝", "获得:", tonumber(itemNums) * inputTimes, "个千年圣丹")
else
-- 如果给予失败,返还扣除的物品
giveitem(actor, tonumber(itemId), totalDanNeeded)
changemoney(actor, totalYuanbaoNeeded, 1)
messagebox(actor, "合成失败,物品已返还")
end
else
messagebox(actor, "扣除物品失败,请检查背包空间")
end
end
```
**代码说明:**
1. 首先计算玩家当前拥有的等级丹和绑定元宝数量
2. 根据两种资源计算最大可合成次数
3. 让玩家输入想要合成的次数
4. 批量扣除等级丹和元宝
5. 批量给予千年圣丹
6. 如果给予失败,会返还扣除的资源
**需要注意的几点:**
1. `getbagitemcount`函数获取背包中指定物品的数量
2. `getbindmoney`函数获取绑定元宝数量
3. `takeitemex`函数扣除指定数量的物品
4. `consumebindmoney`函数扣除绑定元宝
5. `giveitem`函数给予物品
这样就能实现你想要的“一键扣除同时一件得到几次的千年圣丹”功能了!如果有不懂的地方随时问我,或者可以请教浮生梦尊师!
← 返回问答库