sendmsg(play, 1, '{"Msg":"<font color=\'#ff0000\'>强化失败!!</font>","Type":9}')
sendmsg(play, 1, '{"Msg":"<font color=\'#ff0000\'>触发 </font><font color=\'#FFD700\'>降龙重击印</font><font color=\'#ff0000\'> 攻杀伤害翻倍!!</font>","Type":9}')
sendmsg(play, 1, '{"Msg
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-04-24 23:52:28 |
👁️ 阅读: 11
我是Lua后端老师!
小仙,你这个问题问得很有水平啊!想要封装消息发送函数是个很好的想法,这样可以让代码更整洁、更易维护。让我根据你的需求,给你封装几个实用的消息函数。
首先,我们需要在`QFunction-0.lua`中注册消息监听函数,这是消息系统的基础:
```lua
-- 在QFunction-0.lua中注册消息监听函数
function handlerequest(self, msgid, n1, n2, n3, sMsg)
if (msgid == 10) then
release_print("收到10号消息")
else
sendluamsg(self, msgid, n1, n2, n3, sMsg)
end
end
```
现在,我来为你封装几个消息发送函数:
```lua
-- 消息工具类封装
local msg = {}
-- 发送强化失败消息
-- @param play: 玩家对象
-- @param text: 失败文本(可选)
function msg.qianghua_fail(play, text)
local message = text or "强化失败!!"
sendmsg(play, 1, '{"Msg":"<font color=\'#ff0000\'>' .. message .. '</font>","Type":9}')
end
-- 发送技能触发消息
-- @param play: 玩家对象
-- @param skill_name: 技能名称
-- @param effect_desc: 效果描述
function msg.skill_trigger(play, skill_name, effect_desc)
local message = '<font color=\'#ff0000\'>触发 </font><font color=\'#FFD700\'>' ..
skill_name .. '</font><font color=\'#ff0000\'> ' .. effect_desc .. '!!</font>'
sendmsg(play, 1, '{"Msg":"' .. message .. '","Type":9}')
end
-- 发送伤害提示消息
-- @param play: 玩家对象
-- @param skill_name: 技能名称
-- @param damage_num: 伤害数值
-- @param item_idx: 物品IDX(可选)
function msg.damage_hint(play, skill_name, damage_num, item_idx)
local item_info = item_idx or "242:0:1"
local message = '[提示] : {' .. skill_name .. '|' .. item_info .. '}触发额外伤害{' ..
tostring(damage_num) .. '点|' .. item_info .. '}'
sendmsg(play, 1, '{"Msg":"' .. message .. '","FColor":255,"BColor":219,"Type":1}')
end
-- 发送怪物重生公告
-- @param play: 玩家对象(传nil表示全服)
-- @param monster_name: 怪物名称
-- @param map_name: 地图名称
-- @param x: X坐标(可选)
-- @param y: Y坐标(可选)
function msg.monster_reborn(play, monster_name, map_name, x, y)
local pos_x = x or 30
local pos_y = y or 500
local message = "怪物{ " .. monster_name .. " /FCOLOR=253}在地图{ " .. map_name .. " /FCOLOR=251}重生"
sendcustommsg(play, 0, message, 255, 0, pos_x, pos_y)
end
-- 发送彩色消息(根据你的需求定制)
-- @param play: 玩家对象
-- @param color: 颜色(红、黄、蓝等)
-- @param text: 消息内容
function msg.func(play, color, text)
local color_code = "#ff0000" -- 默认红色
if color == "红" then
color_code = "#ff0000"
elseif color == "黄" then
color_code = "#FFD700"
elseif color == "蓝" then
color_code = "#0000ff"
elseif color == "绿" then
color_code = "#00ff00"
end
sendmsg(play, 1, '{"Msg":"<font color=\'' .. color_code .. '\'>' .. text .. '</font>","Type":9}')
end
-- 发送屏幕滚动消息
-- @param play: 玩家对象
-- @param type: 发送类型(0-自己,1-全服,2-行会,3-当前地图,4-组队)
-- @param message: 消息内容
-- @param fcolor: 前景色
-- @param bcolor: 背景色
-- @param y: Y坐标
-- @param scroll_times: 滚动次数
function msg.scroll(play, type, message, fcolor, bcolor, y, scroll_times)
sendmovemsg(play, type, fcolor or 255, bcolor or 0, y or 100, scroll_times or 3, message)
end
-- 发送倒计时消息
-- @param play: 玩家对象
-- @param message: 消息内容
-- @param seconds: 倒计时秒数
-- @param fcolor: 字体颜色
-- @param map_delete: 换地图是否删除(0-不删除,1-删除)
-- @param jump_func: 跳转函数名
function msg.countdown(play, message, seconds, fcolor, map_delete, jump_func)
senddelaymsg(play, message, seconds or 10, fcolor or 255, map_delete or 0, jump_func or "")
end
-- 发送邮件
-- @param userid: 玩家ID或名称(名称前加#)
-- @param mail_id: 邮件ID
-- @param title: 邮件标题
-- @param content: 邮件内容
-- @param rewards: 奖励物品(格式:物品1#数量#绑定标记&物品2#数量#绑定标记)
function msg.send_mail(userid, mail_id, title, content, rewards)
sendmail(userid, mail_id or 1, title or "系统邮件", content or "", rewards or "")
end
-- 发送JSON格式消息(通用方法)
-- @param play: 玩家对象
-- @param msg_type: 发送类型(1-自己,2-全服,3-行会,4-当前地图,5-组队)
-- @param json_data: JSON数据表
function msg.json(play, msg_type, json_data)
local json_str = '{"Msg":"' .. (json_data.Msg or "") ..
'","FColor":' .. (json_data.FColor or 255) ..
',"BColor":' .. (json_data.BColor or 0) ..
',"Type":' .. (json_data.Type or 1) ..
',"Time":' .. (json_data.Time or 3) ..
',"SendName":"' .. (json_data.SendName or "") ..
'","SendId":"' .. (json_data.SendId or "") .. '"}'
sendmsg(play, msg_type, json_str)
end
return msg
```
**使用方法:**
1. 将上面的代码保存为`msg_util.lua`文件
2. 在需要使用的地方引入:
```lua
local msg = require "msg_util"
-- 使用你想要的函数
msg.func(play, "红", "这是一条红色消息") -- 你的需求
-- 其他用法示例
msg.qianghua_fail(play, "武器强化失败!!")
msg.skill_trigger(play, "降龙重击印", "攻杀伤害翻倍")
msg.damage_hint(play, "抱月印", 150)
msg.monster_reborn(nil, "祖玛教主", "祖玛神殿七层")
-- 发送JSON格式消息
local json_data = {
Msg = "欢迎来到游戏世界!",
FColor = 255,
BColor = 0,
Type = 1,
Time = 5,
SendName = "系统",
SendId = "0"
}
msg.json(play, 1, json_data)
```
这样封装后,你的代码会更加清晰易读,也便于统一管理所有的消息发送逻辑。小仙,如果还有不明白的地方,随时问我哦!
← 返回问答库