Lua派发事件与消息监听

LoginScene类:
local UserinfomationData = require "data.UserinfomationData"     --引用角色信息数

--收到服务器发来的角色信息
function LoginScene:onCmdRole( args)
     local proto = args. proto

     local userinfomationData = UserinfomationData()
    userinfomationData. hp = proto. hp             --生命

    client. userinfomationData = userinfomationData
     print( "收到服务器发来的生命:",proto. hp)

    client. userinfomationData: OnLifeText(proto. hp) --把变更的数据发送派发--生命
end



UserinfomationData类:
--- 角色信息类

--派发事件
local UserinfomationData = class( "UserinfomationData")

local Dispatcher = require( "tools/Dispatcher")

require "data/define"

function UserinfomationData:ctor()
self. dispatcher = Dispatcher()

     self. hp = 0 --生命
end

--需要实行更新的属性--生命
function UserinfomationData:OnLifeText( hp)
self. hp = hp
self. dispatcher: emit(distribute_incident. LifeText, self. hp) --生命变化派发事件
end


define类:

--派发事件

distribute_incident = {
    LifeText = "LifeText", --生命
}

--监听事件
monitor_incident = {
    LifeText = "LifeText", --生命
}


Main类:

uDa= require( "data/UserinfomationData"--引用角色信息类(派发事件)
uVa = require( "data/UserinfomationView")     --监听注册事件(例子)
hVa = require( "gui/Main/HeroInfoView")   --监听注册事件
function Main()    
    client. userinfomationData = uDa()   --派发事件  
     uVa()    --监听注册事件(例子)
     hVa()    --监听注册事件
end
   

UserinfomationView 类:

--监听注册事件
local UserinfomationView = class( "UserinfomationView")

require "data/define"

function UserinfomationView:ctor()
client. userinfomationData. dispatcher: subscribe(monitor_incident. LifeText, self. Onhp) --生命
end

--监听属性-生命值的变化
function UserinfomationView.Onhp( hp)
print( "get hp====>" .. hp)
end

return UserinfomationView


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值