You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仅水平移动光标时的侧向移动LUA脚本开发问题

鼠标控制角色反向移动Lua脚本修复方案

需求:鼠标向右移动时控制角色向左移动,鼠标向左移动时控制角色向右移动。原代码因方向逻辑错误、循环冗余等问题导致运行不稳定,以下是修复后的脚本及说明:

修复后的代码

EnablePrimaryMouseButtonEvents(true);
local distance_horiz  = 2    -- 触发按键的水平移动阈值(像素)
local key_press_delay = 0    -- 按键触发间隔(毫秒)
local screen_width    = 2560 -- 游戏屏幕宽度
local screen_height   = 1440 -- 游戏屏幕高度

function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
        local horiz = 0          -- 累积水平偏移量
        local horiz_key = nil    -- 当前按住的水平方向按键
        local horiz_key_tm = 0   -- 下一次可触发按键的时间
        local prev_x, prev_y     -- 上一帧鼠标坐标
        
        -- 获取初始鼠标坐标
        local x, y = GetMousePosition()
        x = math.floor((x + 0.5 + 2^-16) * (screen_width - 1) / (2^16 - 1))
        y = math.floor((y + 0.5 + 2^-16) * (screen_height - 1) / (2^16 - 1))
        prev_x, prev_y = x, y

        -- 左键按住时持续检测鼠标移动
        while IsMouseButtonPressed(1) do
            Sleep(10)
            local tm = GetRunningTime()
            
            -- 获取当前鼠标坐标并计算偏移
            x, y = GetMousePosition()
            x = math.floor((x + 0.5 + 2^-16) * (screen_width - 1) / (2^16 - 1))
            y = math.floor((y + 0.5 + 2^-16) * (screen_height - 1) / (2^16 - 1))
            local x_offset = x - prev_x
            prev_x = x

            -- 累积水平偏移量
            horiz = horiz + x_offset

            -- 达到阈值且到了触发时间时处理按键
            if tm >= horiz_key_tm then
                if math.abs(horiz) >= distance_horiz then
                    local new_key
                    -- 修正方向映射:鼠标右移→角色左移(A键),鼠标左移→角色右移(D键)
                    if horiz > 0 then
                        horiz = horiz - distance_horiz
                        new_key = "A"
                    else
                        horiz = horiz + distance_horiz
                        new_key = "D"
                    end

                    -- 切换按键时释放旧按键
                    if new_key ~= horiz_key then
                        if horiz_key then
                            ReleaseKey(horiz_key)
                        end
                        PressKey(new_key)
                        horiz_key = new_key
                    end
                    horiz_key_tm = tm + key_press_delay
                elseif horiz_key then
                    -- 偏移量不足时释放按键
                    ReleaseKey(horiz_key)
                    horiz_key = nil
                end
            end
        end

        -- 循环结束后确保释放所有按键
        if horiz_key then
            ReleaseKey(horiz_key)
        end
    end
end

关键修改说明

  • 修正方向逻辑:原代码将鼠标右移映射为"D"(角色右移),完全违背需求,修复后鼠标右移对应角色左移按键"A",鼠标左移对应角色右移按键"D"。
  • 简化循环结构:移除原代码中冗余的双层循环和中键判断,只保留左键按住时的单循环,避免逻辑混乱导致的响应延迟。
  • 修复坐标初始化:首次获取鼠标坐标后直接赋值给prev_x,确保第一帧的偏移计算准确,不会出现无效值。
  • 优化按键释放:当鼠标停止移动(累积偏移量低于阈值)时立即释放当前按键,同时在循环结束后额外检查并释放按键,防止角色持续移动。
  • 移除无关逻辑:删除原代码中未使用的垂直移动变量和中键退出逻辑,减少不必要的计算和干扰。

内容的提问来源于stack exchange,提问作者SDES

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:21:01