function 场景类_玩家:开始移动() if self.目标格子[1] == nil then self:停止移动() return
时间: 2024-05-27 22:14:21 浏览: 7
end
local 目标格子 = self.目标格子[1]
local 当前格子 = self.当前格子
if 当前格子[1] == 目标格子[1] and 当前格子[2] == 目标格子[2] then
table.remove(self.目标格子, 1)
if #self.目标格子 == 0 then
self:停止移动()
end
return
end
local 移动速度 = self.移动速度
local 目标位置 = self.地图:格子转坐标(目标格子[1], 目标格子[2])
local 当前位置 = self:getPosition()
local 方向 = cc.pSub(目标位置, 当前位置)
方向 = cc.pNormalize(方向)
local 移动距离 = cc.pMul(方向, 移动速度)
local 新位置 = cc.pAdd(当前位置, 移动距离)
self:setPosition(新位置)
if 方向.x < 0 then
self:setScaleX(-1)
else
self:setScaleX(1)
end
self.当前格子 = self.地图:坐标转格子(新位置.x, 新位置.y)
相关问题
优化这段代码 if index == BackpackMainWindow.TAB.attr or index == BackpackMainWindow.TAB.backpack then self.titlePanel:Hiden() self.growPanel:Hiden() self.characterpanel:Show() self.honorPanel:Hiden() if index == BackpackMainWindow.TAB.attr then self.attrpanel:Show() self.backgridpanel:Hiden() elseif index == BackpackMainWindow.TAB.backpack then self.attrpanel:Hiden() self.growPanel:Hiden() self.backgridpanel:Show(self.openArgs) self.secondTab = nil end elseif index == BackpackMainWindow.TAB.title then self.characterpanel:Hiden() self.backgridpanel:Hiden() self.attrpanel:Hiden() self.growPanel:Hiden() self.titlePanel:Show(self.extra_param) self.honorPanel:Hiden() elseif index == BackpackMainWindow.TAB.grow then self.characterpanel:Hiden() self.backgridpanel:Hiden() self.attrpanel:Hiden() self.titlePanel:Hiden() self.growPanel:Show(0) self.honorPanel:Hiden() elseif index == BackpackMainWindow.TAB.honor then self.characterpanel:Hiden() self.backgridpanel:Hiden() self.attrpanel:Hiden() self.titlePanel:Hiden() self.growPanel:Hiden() self.honorPanel:Show() end
可以将每个TAB对应的操作封装成一个函数,然后根据不同的index调用对应的函数,代码如下:
```lua
function BackpackMainWindow:ShowCharacterPanel()
self.titlePanel:Hiden()
self.growPanel:Hiden()
self.characterpanel:Show()
self.honorPanel:Hiden()
end
function BackpackMainWindow:ShowAttributePanel()
self.titlePanel:Hiden()
self.growPanel:Hiden()
self.characterpanel:Show()
self.honorPanel:Hiden()
self.attrpanel:Show()
self.backgridpanel:Hiden()
end
function BackpackMainWindow:ShowBackpackPanel()
self.titlePanel:Hiden()
self.growPanel:Hiden()
self.characterpanel:Show()
self.honorPanel:Hiden()
self.attrpanel:Hiden()
self.growPanel:Hiden()
self.backgridpanel:Show(self.openArgs)
self.secondTab = nil
end
function BackpackMainWindow:ShowTitlePanel()
self.characterpanel:Hiden()
self.backgridpanel:Hiden()
self.attrpanel:Hiden()
self.growPanel:Hiden()
self.titlePanel:Show(self.extra_param)
self.honorPanel:Hiden()
end
function BackpackMainWindow:ShowGrowPanel()
self.characterpanel:Hiden()
self.backgridpanel:Hiden()
self.attrpanel:Hiden()
self.titlePanel:Hiden()
self.growPanel:Show(0)
self.honorPanel:Hiden()
end
function BackpackMainWindow:ShowHonorPanel()
self.characterpanel:Hiden()
self.backgridpanel:Hiden()
self.attrpanel:Hiden()
self.titlePanel:Hiden()
self.growPanel:Hiden()
self.honorPanel:Show()
end
function BackpackMainWindow:OnTabClick(index)
if index == BackpackMainWindow.TAB.attr then
self:ShowAttributePanel()
elseif index == BackpackMainWindow.TAB.backpack then
self:ShowBackpackPanel()
elseif index == BackpackMainWindow.TAB.title then
self:ShowTitlePanel()
elseif index == BackpackMainWindow.TAB.grow then
self:ShowGrowPanel()
elseif index == BackpackMainWindow.TAB.honor then
self:ShowHonorPanel()
end
end
```
这样,代码更加清晰,易于维护和修改。同时,如果需要新增或修改某个TAB对应的操作,只需在对应的函数中修改即可,不会影响到其他部分的代码。
function after_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para) local uci_r = uci.cursor() if para.log_send ~= nil then uci_r:delete("behave_audit", "setting", "log_send") uci_r:commit("behave_audit") end if para.log_send_enable == nil and para.send_server == nil then return err.ENONE end local status = ubus.connect():call("logger","config_reload",{}) return err.ENONE end function before_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para) local uci_r = uci.cursor() if para.log_send ~= nil then uci_r:set("behave_audit", "setting", "log_send_enable", para.log_send) uci_r:commit("behave_audit") end return err.ENONE end function get_behave_audit_info(filter, uciname, secname, para) local uci_r = uci.cursor() local result = {} uci_r:foreach("behave_audit", "behave_audit", function(section) section["log_send"] = section["log_send_enable"] result = section end) return err.ENONE, result end 有啥可优化的吗
这段代码可以进行一些优化,如下所示:
1. 在函数 `after_save_audit` 中,可以将 `if para.log_send_enable == nil and para.send_server == nil then` 的判断条件改为 `if not para.log_send_enable and not para.send_server then`,使代码更为简洁明了。
2. 在函数 `before_save_audit` 中,可以将 `if para.log_send ~= nil then` 的判断条件改为 `if para.log_send then`,同样可以使代码更为简洁。
3. 在函数 `get_behave_audit_info` 中,可以将 `result = section` 改为 `table.insert(result, section)`,以便将所有查询到的结果都存储到 `result` 表中。
4. 可以将重复的代码提取出来,定义为公共函数,以便在多个函数中复用,提高代码的复用性和可维护性。
综上所述,可以对这段代码进行如下优化:
```
local function update_audit_log_send(uci_r, enable)
if enable then
uci_r:set("behave_audit", "setting", "log_send_enable", "1")
else
uci_r:delete("behave_audit", "setting", "log_send_enable")
end
uci_r:commit("behave_audit")
end
function after_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para)
local uci_r = uci.cursor()
if para.log_send ~= nil then
update_audit_log_send(uci_r, false)
end
if not para.log_send_enable and not para.send_server then
return err.ENONE
end
ubus.connect():call("logger","config_reload",{})
return err.ENONE
end
function before_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para)
local uci_r = uci.cursor()
if para.log_send then
update_audit_log_send(uci_r, true)
end
return err.ENONE
end
function get_behave_audit_info(filter, uciname, secname, para)
local uci_r = uci.cursor()
local result = {}
uci_r:foreach("behave_audit", "behave_audit", function(section)
section["log_send"] = section["log_send_enable"]
table.insert(result, section)
end)
return err.ENONE, result
end
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)