hongqizhen 发表于 2010-6-15 08:44:57

求助-RMXP鼠标脚本的问题

我最近在汉化艾拉的希望,由于许多人抱怨鼠标纸箱事件后,不会自动改变图样,游戏很难玩下去。所以我打算给它添上这个功能。
但是鼠标图像的改变成功了,但在判断事件类型时却出错了。一直提示未定义模块,我快疯了。明明我已经定义了!
无奈之下只能到处找人帮忙了,麻烦大家帮忙看看,到底是错在哪里?


class Game_Event
attr_accessor :flag
end

class Game_Map
#--------------------------------------------------------------------------
# ● 检查鼠标处是否有自定义的事件并返回类型
#--------------------------------------------------------------------------
def check_event_custom(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width / 4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height / 4
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
      for i in 0...event.list.size
          if event.list.parameters == "item" #类型判断
            event.flag = 1
          elsif event.list.parameters == "npc" #类型判断
            event.flag = 2
          end
          return event.flag #返回事件类型标志
      end
      end
    end
    return $mouse_icon_id #使鼠标图不变化
end
#--------------------------------------------------------------------------
# ● 检查鼠标处是否有事件可以开启
#--------------------------------------------------------------------------
def check_event_custom_start(mouse_x, mouse_y)   
    for event in $game_map.events.values #循环所有事件检查
      #事件角色图片宽度、高度
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
      #判断是否鼠标在事件上
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
      way_x = $game_player.x - event.x
      way_y = $game_player.y - event.y
      if (.include?($game_player.x-event.x) and $game_player.y-event.y == 0) or (.include?($game_player.y-event.y) and $game_player.x-event.x == 0)
          for i in 0...event.list.size
            if ["item","npc"].include?(event.list.parameters) #当事件属于自定义事件
            #判断主角朝向
            if way_x == -1
                p_direction = 6 if way_y == 0
            elsif way_x == 0
                p_direction = 2 if way_y == -1
                p_direction = 8 if way_y == 1
            else
                p_direction = 4 if way_y == 0
            end
            event.start #开启事件
            return 1, p_direction #返回即将开启事件以及角色朝向
            end
          end
      end
      end
    end
    return 0, 5 #返回不会开启事件以及角色朝向不变
end
#--------------------------------------------------------------------------
# ● 检查鼠标处是否存在自定义事件 for 寻路
#--------------------------------------------------------------------------
def check_event_custom_exist(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      #事件角色图片宽度、高度
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
      for i in 0...event.list.size
          return 1, event if ["item", "npc"].include?(event.list.parameters) #返回存在自定义事件以及事件体
      end
      end
    end
    return 0, event #返回不存在自定义事件,以及事件体
end
end














# Encapsulation of a mouse event
MouseEvent = Struct.new(:sender, :trigger, :info)

#==============================================================================
# ** Mouse module
#------------------------------------------------------------------------------
# Manages the Win32API calls and the base mouse functionalities
#==============================================================================
module Mouse
#--------------------------------------------------------------------------
# * 鼠标输入触发器
#--------------------------------------------------------------------------
LEFT_CLICK = 1
RIGHT_CLICK = 2
MIDDLE_CLICK = 4

MOUSE_TRIGGERS =
#--------------------------------------------------------------------------
# * API Declaration
#--------------------------------------------------------------------------
$ShowCursor      = Win32API.new("user32", "ShowCursor", 'i', 'l')
ASYNC_KEY      = Win32API.new('user32', 'GetAsyncKeyState', 'i',   'i')
SCREEN_TO_CLIENT = Win32API.new('user32', 'ScreenToClient',   %w(l p), 'i')
CURSOR_POS       = Win32API.new('user32', 'GetCursorPos',   'p',   'i')
FIND_WINDOW      = Win32API.new('user32', 'FindWindowA',      %w(p p), 'l')

module_function
##
# Start up procedures
#
def start_up
    # 创建一个模块的配置参考
    @@const = System::Mouse
    # 初始化按钮状态
    @button_states = Array.new(5, 0)
    # 计算窗口的句柄
    window_handle
    # 了解鼠标状态
    mouse_status = Utility.read_ini('mouse')
    @enabled = !(mouse_status == '0' || mouse_status == '')
    # 如果鼠标脚本启用则创建鼠标
    if @enabled
      $ShowCursor.call(0)
      # 创建鼠标
      create_cursor
      # 更新游标
      update
    end
end


##
# 终止鼠标脚本
#
def shutdown
    # 终结掉鼠标
    @mouse_sprite.dispose unless @mouse_sprite.nil? || @mouse_sprite.disposed?
    $ShowCursor.call(1)
end

##
# 打开鼠标脚本
# 如果它已经打开,什么都不会发生
#
def turn_on
    return if self.enabled
    @enabled = true
    # 创建鼠标
    create_cursor
    # 更新游标
    update
end

##
# 鼠标脚本关闭。
# 如果它已关闭,什么都不会发生
#
def turn_off
    return unless self.enabled
    @enabled = false
    # 显示光标如果是隐藏
    unless @mouse_sprite.nil? || @mouse_sprite.disposed?
      # 终止鼠标
      @mouse_sprite.dispose
      # 删除引用,以便将垃圾清除干净
      @mouse_sprite = nil
    end
end

##
# 更新
#
def update
    # 只在启用时更新
    return unless self.enabled
    # 取新的位置
    sx, sy = screen_pos
    # 如果鼠标位置发生了变化
    if @sx != sx || @sy != sy
      # 储存新的屏幕x和y
      @sx = sx
      @sy = sy
      # 获取相对位置
      if Input.is_fullscreen
      rx, ry = sx, sy
      else
      rx, ry = screen_to_client(sx, sy)
      end
      return unless rx >= 0 && rx < 640 && ry >= 0 && ry < 480
      
      
      
      
   #########################################################      
      #可能能在此处判断鼠标图像
      #########################################################
      # 变更光标的位置
      @mouse_sprite.x = rx
      @mouse_sprite.y = ry
      $mouse_icon_id = $game_map.check_event_custom(rx,ry)if not .include?($mouse_icon_id)
      if $scene.is_a?(Scene_Map) == false
         $mouse_icon_id = 0
      end
      case $mouse_icon_id
      when 1
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Arrow3.png')
      when 2
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Arrow2.png')
      when 11
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 12
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 13
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 14
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 16
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 17
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 18
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 19
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 0
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      end
    end    # 检查点击
    for trigger in MOUSE_TRIGGERS
      # 获取异步状态
      state = ASYNC_KEY.call(trigger)
      # If 0 or 1
      if state == 0 || state == 1
      if @button_states > 0
          @button_states *= -1
      else
          @button_states = 0
      end
      else
      if @button_states > 0
          @button_states += 1
      else
          @button_states = 1
      end
      end
    end
end

##
# Check for press
#   返回false,如果不启用鼠标
#
def press?(id = LEFT_CLICK)
    return false unless self.enabled
    return @button_states >= 1
end

##
# 查触发
#   返回false,如果不启用鼠标
#
def trigger?(id = LEFT_CLICK)
    return false unless self.enabled
    return @button_states == 1
end

##
# 重复检查
#   返回false,如果不启用鼠标
#
def repeat?(id = LEFT_CLICK)
    return false unless self.enabled
    if @button_states <= 0
      return false
    elsif @button_states == 1
      return true
    elsif @button_states > @@const::REPEAT_START_WAIT
      return @button_states % @@const::REPEAT_WAIT == 1
    end
end

##
# 回true如果鼠标脚本启用
#
def enabled
    @enabled
end

##
# 获得屏幕坐标
#
def screen_pos
    # 包0位置
    pos = .pack('ll')
    # 裸装的光标位置返回呼叫
    return CURSOR_POS.call(pos) == 0 ? nil : pos.unpack('ll')
end

##
# 转换屏的X和Y坐标的相对坐标。
# Top-left corner = (0, 0)
#
def pos
    return @mouse_sprite.x, @mouse_sprite.y
end

##
# 检查鼠标是否在窗口内部
#
def inside?
    x, y = pos
    return x >= 0 && x < 640 && y >= 0 && y < 480
end

##
# 屏幕坐标转换为相对的客户端窗口
#
def screen_to_client(x, y)
    # 如果X和Y空返回零
    return nil unless x and y
    if @ori_pos.nil?
      @stc_wait = 0
    end
    if @stc_wait <= 0
      # Locate upper-left corner
      pack = .pack('ll')
      SCREEN_TO_CLIENT.call(window_handle, pack)
      @ori_pos = pack.unpack('ll')
      # 等待10帧以开始下一次查找
      @stc_wait = 10
    else
      @stc_wait -= 1
    end
    # Translocate X- and Y-coordinates
    x += @ori_pos
    y += @ori_pos
    return x, y
end

##
# 检索窗口句柄
#
def window_handle
    if @window_handle.nil?
      # 游戏名称查找
      game_name = Utility.read_ini('Title')
      # 查找窗口
      @window_handle = FIND_WINDOW.call('RGSS Player', game_name)
    end
    return @window_handle
end

# 开辟了单独的类模块的
class << self
    ###################
    # Private methods #
    ###################
    private
    ##
    # 创建光标
    #
    def create_cursor
      @mouse_sprite = Sprite.new
      @mouse_sprite.z = 50000
      @mouse_sprite.bitmap = RPG::Cache.icon(@@const::DEFAULT_ICON)
    end
end
end




















class Window_Base
##
# 更新鼠标位置
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    return unless $DEBUG
    raise StandardError.new("mouse_move(mx, my) was not overwritten in #{self.class}")
end

##
# 更新鼠标(只有当鼠标启用时才调用)
# Note: The mouse is over the window
#
def mouse_update
    return unless $DEBUG
    raise StandardError.new("mouse_update was not overwritten in #{self.class}")
end

##
# 判断鼠标位置是否超过窗口,是的话让鼠标图像停留在窗口边框
#
def over_contents?(mx, my)
    return mx >= 16 && mx < width - 16 &&
         my >= 16 && my < height - 16
end
end

class Window_Selectable
##
# Get the height of the elements
#
def element_height
    return 32
end

##
# Get the height of the elements
#
def element_width
    return self.width / @column_max - 32
end

##
# Calculates the index from the x and y coordinate
# Assumes (0,0) to be upper-left corner of the contents
#
def calculate_index(x, y)
    ex = x / element_width
    ey = y / element_height
    return ex + (ey + top_row) * @column_max
end

##
#
#
def cause_scroll(index)
    # Getrow
    row = index / @column_max
    # If row is before top row or more to back than back row
    return row < self.top_row || row > self.top_row + (self.page_row_max - 1)
end

##
# 更新鼠标位置
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    # Cursor position less than 0 implies that the cursor is hidden
    return if @index < 0 || !self.active
    # Store the coordinates
    @mx, @my = mx, my
   
    # Ignore the move if it is not over the contents
    if mx >= 16 && mx < width - 16
      index = calculate_index(mx-16, my-16)
      # If the index is valid
      if index < @item_max && index >= 0
      # Scroll wait
      if cause_scroll(index) && self.scroll_wait > 0
          return
      elsif cause_scroll(index)
          self.scroll_wait = System::Mouse::SCROLL_WAIT
          @scrolling = true
      else
          @scrolling = false
      end
      # Change the index
      @index = index
      end
    end
end

##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    # Only update if active
    return unless self.active
    # Scroll wait update
    if self.scroll_wait > 0
      self.scroll_wait -= 1
      return
    elsif @scrolling
      # See if the scrolling should continue
      index = calculate_index(@mx-16, @my-16)
      if cause_scroll(index) && index < @item_max && index >= 0
      self.scroll_wait = System::Mouse::SCROLL_WAIT
      @index = index
      else
      # Stop scrolling
      @scrolling = false
      end
    end
    @mx, @my = -1, -1 if @mx.nil?
    # Check for click
    if Mouse.trigger?(Mouse::LEFT_CLICK) && over_contents?(@mx, @my)
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
end
end

class Window_Target
##
# Update mouse position
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    return unless self.active
    if @index >= 0
      super
      return
    else
      # Store the coordinates
      @mx, @my = mx, my
    end
end
##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    if @index >= 0
      super
      return
    else
      # Only update if active
      return unless self.active
      @mx, @my = -1, -1 if @mx.nil?
      # Check for click
      if Mouse.trigger?(Mouse::LEFT_CLICK) && over_contents?(@mx, @my)
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
      end
    end
end
end

class Window_NameInput
##
# Determines whether the given coordinates are within the given area
#
def is_in?(x,y, rx,ry,rw,rh)
    return x >= rx && x < rx + rw &&
         y >= ry && y < ry + rh
end

##
# Calculates the index from the coordinates
#
def calculate_index(x, y)
    # Check position
    return 90 if is_in?(x,y,428, 9 * 32, 48, 32)
    if is_in?(x,y,140, 0, 5*32, 9*32)
      # Left box
      x = (x - 140) / 32
      y /= 32
      return x + y * 5
    elsif is_in?(x,y,320, 0, 5*32, 9*32)
      # Right box
      x = (x - 320) / 32
      y /= 32
      return x + y * 5 + 45
    end
    # Outside
    return -1
end

##
# Update mouse position
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    @mx, @my = mx, my
    index = calculate_index(mx-16, my-16)
    @index = index unless index < 0
end

##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    if Mouse.trigger?(Mouse::LEFT_CLICK) && calculate_index(@mx-16, @my-16) >= 0
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
end
end

class Window_Message
##
# Check for normal trigger
#
def normal_trigger?
    return true if Input.trigger?(Input::C)
    if Mouse.trigger?(Mouse::LEFT_CLICK)
      return true if $game_temp.choice_max <= 0
      index = calculate_index(@mx - 16, @my - 16)
      return true if index >= 0 && index < @item_max
    end
end

##
# Update mouse (Only called if mouse is enabled)
#
def mouse_update
    # Do nothing
end

##
# Calculates the index from the x and y coordinate
# Assumes (0,0) to be upper-left corner of the contents
#
def calculate_index(x, y)
    return -1 if x > @cursor_width + 8
    ey = (y - $game_temp.choice_start * 32) / element_height
    return (ey + top_row) * @column_max
end
end

class Arrow_Base
##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    return unless $DEBUG
    raise StandardError.new("mouse_update was not overwritten in #{self.class}")
end
end

class Arrow_Enemy
##
# Update mouse position
#
def mouse_move(mx, my)
    # Pass Through Enemies
    for enemy in $game_troop.enemies
      # Skip if Non-Existing Enemy
      next unless enemy.exist?
      # Gets Paddings
      w, h = enemy.battler_width / 2, enemy.battler_height
      # If Within Mouse Padding Range
      if mx.between?(enemy.screen_x - w, enemy.screen_x + w) &&
         my.between?(enemy.screen_y - h, enemy.screen_y + 10)
      # Set Index
      @index = $game_troop.enemies.index(enemy)
      break
      end
    end
end

##
# Update mouse (Only called if mouse is enabled)
#
def mouse_update
    if Mouse.trigger?(Mouse::LEFT_CLICK)# && @my < 320
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
    mx, my = Mouse.pos
    return if @mx == mx && @my == my
    @mx, @my = mx, my
    # Pass Through Enemies
    for enemy in $game_troop.enemies
      # Skip if Non-Existing Enemy
      next unless enemy.exist?
      # Gets Paddings
      w, h = enemy.battler_width / 2, enemy.battler_height
      # If Within Mouse Padding Range
      if mx.between?(enemy.screen_x - w, enemy.screen_x + w) &&
         my.between?(enemy.screen_y - h, enemy.screen_y + 10)
      # Set Index
      @index = $game_troop.enemies.index(enemy)
      break
      end
    end
end
end

class Arrow_Actor
##
# Update mouse (Only called if mouse is enabled)
#
def mouse_update
    if Mouse.trigger?(Mouse::LEFT_CLICK)# && @my > 288
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
    # Get mouse position
    mx, my = Mouse.pos
    # Checks that the mouse have moved
    return if @mx == mx && @my == my
    # Store new position
    @mx, @my = mx, my
    # Pass Through Actors
    for actor in $game_party.actors
      # Gets Paddings
      w, h = actor.battler_width / 2, actor.battler_height
      # If Within Mouse Padding Range
      if mx.between?(actor.screen_x - w, actor.screen_x + w) &&
         my.between?(actor.screen_y - h, actor.screen_y + 10)
      # Set Index
      @index = $game_party.actors.index(actor)
      end
    end
end
end



以上就是鼠标脚本,红色部分,我是用四方向行走脚本添加的,运行时会卡在
$mouse_icon_id = $game_map.check_event_custom(rx,ry)if not .include?($mouse_icon_id)
这句上,提示check_event_custom是未定义的方法,可我明明定义了。
如果把这句话改为具体的数字如:
$mouse_icon_id = 1,那么能正常进入游戏,刚进游戏图形是箭头,进入到地图后就变成小手,也就是说我添加的这段鼠标图像改变判断代码,并没放错地方。

其中,鼠标图像ARROW是箭头,Arrow3是小手。麻烦大家帮帮看看,该怎么修改。

另外,游戏中鼠标的启动和卸载语句都在MAIN中。分别为:
# Start up the mouse
Mouse.start_up

# Shutdown mouse
Mouse.shutdown

而每个窗口都有对应的鼠标控制赋权语句及刷新语句如:
    # Add as a mouse enabled window
    mouse_windows << @item_window

mouse.update

另:请不要叫我用其他的脚本,我都试过了,这个游戏如果直接添加其他的鼠标脚本,那么会引起冲突,完全进不了游戏,即使把原有脚本清除掉,再放其他的鼠标脚本,也会不定时出错,而且不会提示具体错在哪里。

hongqizhen 发表于 2010-6-15 08:47:36

用于参考的四方向行走的鼠标脚本:

完整鼠标系统(四方向):

#==============================================================================
# ■ 完整鼠标系统(四方向)
#------------------------------------------------------------------------------
#  使用时务必配合专用寻路算法
#   By whbm
#==============================================================================
#下面做一下介绍与使用说明:
#    在屏幕上单击鼠标的时候,会自动进行寻路,这里为了速度更快并且为了进行迷
#宫时的难度寻路被限定在当时的屏幕内部。(否则玩家直接点到终点,呵呵....知道
#后果了吧)
#    在角色移动过程中再次单击鼠标(即双击)并在单击第二次鼠标的时候不松手,
#角色将会跟随鼠标方向移动。(这个应该好理解,不用多说吧)当角色贴着欲被启动
#的事件的时候,单击NPC即可启动事件。若未贴着,将会产生自动寻路到NPC附近的某
#点,那时在单击NPC即可。
#    当鼠标停在某些事件上时候会发现有不同的图标,设置方法为:宝箱事件请在事
#件的执行内容中添加 注释,注释内容为 Item 注意大小写。NPC事件请在事件的执行
#内容中添加 注释注释内容为 NPC 注意大小写。若不箱改变某事件的图标,则不要写
#那两个注释。
#    当把脚本转到您工程的时候千万别忘了那个寻路用的脚本。
#==============================================================================
class Game_Event
attr_accessor :flag
end
#==============================================================================
# ■ Game_Map
#------------------------------------------------------------------------------
#  处理地图的类。包含卷动以及可以通行的判断功能。
# 本类的实例请参考 $game_map 。
#==============================================================================
class Game_Map
#--------------------------------------------------------------------------
# ● 检查鼠标处是否有自定义的事件并返回类型
#--------------------------------------------------------------------------
def check_event_custom(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width / 4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height / 4
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
      for i in 0...event.list.size
          if event.list.parameters == "Item" #类型判断
            event.flag = 1
          elsif event.list.parameters == "Npc" #类型判断
            event.flag = 2
          else
            event.flag = 0 if $game_player.get_mouse_sta != 2 #无标志
          end
          return event.flag #返回事件类型标志
      end
      end
    end
    return 0 if $game_player.get_mouse_sta != 2 #如果不是在跟随鼠标状态,则返回无标志
    return $mouse_icon_id #使鼠标图不变化
end
#--------------------------------------------------------------------------
# ● 检查鼠标处是否有事件可以开启
#--------------------------------------------------------------------------
def check_event_custom_start(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      #事件角色图片宽度、高度
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
      #判断是否鼠标在事件上
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
      way_x = $game_player.x - event.x
      way_y = $game_player.y - event.y
      if (.include?($game_player.x-event.x) and $game_player.y-event.y == 0) or (.include?($game_player.y-event.y) and $game_player.x-event.x == 0)
          for i in 0...event.list.size
            if ["Item","Npc"].include?(event.list.parameters) #当事件属于自定义事件
            #判断主角朝向
            if way_x == -1
                p_direction = 6 if way_y == 0
            elsif way_x == 0
                p_direction = 2 if way_y == -1
                p_direction = 8 if way_y == 1
            else
                p_direction = 4 if way_y == 0
            end
            event.start #开启事件
            return 1, p_direction #返回即将开启事件以及角色朝向
            end
          end
      end
      end
    end
    return 0, 5 #返回不会开启事件以及角色朝向不变
end
#--------------------------------------------------------------------------
# ● 检查鼠标处是否存在自定义事件 for 寻路
#--------------------------------------------------------------------------
def check_event_custom_exist(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      #事件角色图片宽度、高度
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
      for i in 0...event.list.size
          return 1, event if ["Item", "Npc"].include?(event.list.parameters) #返回存在自定义事件以及事件体
      end
      end
    end
    return 0, event #返回不存在自定义事件,以及事件体
end
end


#=================以下两个用来调整战斗时的手感问题,可以自己试试。
$敌人选框扩大 = 20
$角色选框扩大 = 30


#==============================================================================
# ● API调用
#==============================================================================
$ShowCursor = Win32API.new("user32", "ShowCursor", 'i', 'l')
$GetCursorPos = Win32API.new("user32", "GetCursorPos", 'p', 'i')
$ScreenToClient = Win32API.new("user32", "ScreenToClient", 'ip', 'i')
$GetActiveWindow = Win32API.new("user32", "GetActiveWindow", nil, 'l')
$Window_HWND = $GetActiveWindow.call
$GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
module Mouse
LEFT = 0x01
RIGHT = 0x02

def self.init(sprite = nil)
   $ShowCursor.call(0)
   
   @show_cursor = false
   
   @mouse_sprite = Sprite.new
   @mouse_sprite.z = 99999
   @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/001-Weapon01.png')

   @left_press = false
   @right_press = false
   @left_trigger = false
   @right_trigger = false
   @left_repeat = false
   @right_repeat = false
   @click_lock = false
   
   update
end
def self.exit
   @mouse_sprite.bitmap.dispose
   @mouse_sprite.dispose
   @show_cursor = true
   $ShowCursor.call(1)
end
def self.mouse_debug
   return @mouse_debug.bitmap
end
def self.update
   left_down = $GetKeyState.call(0x01)
   right_down = $GetKeyState.call(0x02)
   if Graphics.frame_count * 3 / Graphics.frame_rate != @total_sec
   @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
   @a = !@a
   end
   if $scene.is_a?(Scene_Map) == false
   $mouse_icon_id = 0
   end
   if $mouse_icon_id != $mouse_icon_id_last
   case $mouse_icon_id
   when 1
       if @a
         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/GetItem1')
       else
         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/GetItem2')
       end
   when 2
       if @a
         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/TalkTo1')
       else
         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/TalkTo2')
       end
   when 11
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LOWER_LEFT')
   when 12
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_DOWN')
   when 13
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LOWER_RIGHT')
   when 14
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LEFT')
   when 16
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_RIGHT')
   when 17
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UPPER_LEFT')
   when 18
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UP')
   when 19
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UPPER_RIGHT')
   when 0
       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/木剑')
   end
   $mouse_icon_id_last = $mouse_icon_id
   end
   @click_lock = false
   mouse_x, mouse_y = self.get_mouse_pos
   if @mouse_sprite != nil
   @mouse_sprite.x = mouse_x
   @mouse_sprite.y = mouse_y
   end
   if left_down == 1
   @left_repeat = (not @left_repeat)
   @left_trigger = (not @left_press)
   @left_press = true
   else
   @left_press = false
   @left_trigger = false
   @left_repeat = false
   end
   if right_down == 1
   @right_repeat = (not @right_repeat)
   @right_trigger = (not @right_press)
   @right_press = true
   else
   @right_press = false
   @right_trigger = false
   @right_repeat = false
   end
end
def self.get_mouse_pos
   point_var = .pack('ll')
   if $GetCursorPos.call(point_var) != 0
   if $ScreenToClient.call($Window_HWND, point_var) != 0
       x, y = point_var.unpack('ll')
       if (x < 0) or (x > 10000) then x = 0 end
       if (y < 0) or (y > 10000) then y = 0 end
       if x > 640 then x = 640 end
       if y > 480 then y = 480 end
       return x, y
   else
       return 0, 0
   end
   else
   return 0, 0
   end
end
def self.press?(mouse_code)
   if mouse_code == LEFT
   if @click_lock
       return false
   else
       return @left_press
   end
   elsif mouse_code == RIGHT
   return @right_press
   else
   return false
   end
end
def self.trigger?(mouse_code)
   if mouse_code == LEFT
   if @click_lock
       return false
   else
       return @left_trigger
   end
   elsif mouse_code == RIGHT
   return @right_trigger
   else
   return false
   end
end
def self.repeat?(mouse_code)
   if mouse_code == LEFT
   if @click_lock
       return false
   else
       return @left_repeat
   end
   elsif mouse_code == RIGHT
   return @right_repeat
   else
   return false
   end
end
def self.click_lock?
   return @click_lock
end
def self.click_lock
   @click_lock = true
end
def self.click_unlock
   @click_lock = false
end
end
module Input
if @self_update == nil
   @self_update = method('update')
   @self_press = method('press?')
   @self_trigger = method('trigger?')
   @self_repeat = method('repeat?')
end
def self.update
   @self_update.call
   Mouse.update
end
def self.press?(key_code)
   if @self_press.call(key_code)
   return true
   end
   if key_code == C
   return Mouse.press?(Mouse::LEFT)
   elsif key_code == B
   return Mouse.press?(Mouse::RIGHT)
   else
   return @self_press.call(key_code)
   end
end
def self.trigger?(key_code)
   if @self_trigger.call(key_code)
   return true
   end
   if key_code == C
   return Mouse.trigger?(Mouse::LEFT)
   elsif key_code == B
   return Mouse.trigger?(Mouse::RIGHT)
   else
   return @self_trigger.call(key_code)
   end
end
def self.repeat?(key_code)
   if @self_repeat.call(key_code)
   return true
   end
   if key_code == C
   return Mouse.repeat?(Mouse::LEFT)
   elsif key_code == B
   return Mouse.repeat?(Mouse::RIGHT)
   else
   return @self_repeat.call(key_code)
   end
end
end
class Window_Selectable
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
def update
   self_update
   if self.active and @item_max > 0
   index_var = @index
   tp_index = @index
   mouse_x, mouse_y = Mouse.get_mouse_pos
   mouse_not_in_rect = true
   for i in 0...@item_max
       @index = i
       update_cursor_rect
       top_x = self.cursor_rect.x + self.x + 16
       top_y = self.cursor_rect.y + self.y + 16
       bottom_x = top_x + self.cursor_rect.width
       bottom_y = top_y + self.cursor_rect.height
       if (mouse_x > top_x) and (mouse_y > top_y) and
          (mouse_x < bottom_x) and (mouse_y < bottom_y)
         mouse_not_in_rect = false
         if tp_index != @index
         tp_index = @index
         $game_system.se_play($data_system.cursor_se)
         end
         break
       end
   end
   if mouse_not_in_rect
       @index = index_var
       update_cursor_rect
       Mouse.click_lock
   else
       Mouse.click_unlock               
   end
   end
end
end
class Window_NameInput
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
def update
   self_update
   if self.active
   index_var = @index
   mouse_x, mouse_y = Mouse.get_mouse_pos
   mouse_not_in_rect = true
   for i in (0...CHARACTER_TABLE.size).to_a.push(180)
       @index = i
       update_cursor_rect
       top_x = self.cursor_rect.x + self.x + 16
       top_y = self.cursor_rect.y + self.y + 16
       bottom_x = top_x + self.cursor_rect.width
       bottom_y = top_y + self.cursor_rect.height
       if (mouse_x > top_x) and (mouse_y > top_y) and
          (mouse_x < bottom_x) and (mouse_y < bottom_y)
         mouse_not_in_rect = false
         break
       end
   end
   if mouse_not_in_rect
       @index = index_var
       update_cursor_rect
       Mouse.click_lock
   else
       Mouse.click_unlock
   end
   end
end
end
class Window_InputNumber
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
def update
   self_update
   mouse_x, mouse_y = Mouse.get_mouse_pos
   if self.active and @digits_max > 0
   index_var = @index
   mouse_not_in_rect = true
   for i in 0...@digits_max
       @index = i
       update_cursor_rect
       top_x = self.cursor_rect.x + self.x + 16
       bottom_x = top_x + self.cursor_rect.width
       if (mouse_x > top_x) and (mouse_x < bottom_x)
         mouse_not_in_rect = false
         break
       end
   end
   if mouse_not_in_rect
       @index = index_var
       update_cursor_rect
       Mouse.click_lock
   else
       Mouse.click_unlock
   end
   end
   if @last_mouse_y == nil
   @last_mouse_y = mouse_y
   end
   check_pos = (@last_mouse_y - mouse_y).abs
   if check_pos > 10
   $game_system.se_play($data_system.cursor_se)
   place = 10 ** (@digits_max - 1 - @index)
   n = @number / place % 10
   @number -= n * place
   n = (n + 1) % 10 if mouse_y < @last_mouse_y
   n = (n + 9) % 10 if mouse_y > @last_mouse_y
   @number += n * place
   refresh
   @last_mouse_y = mouse_y
   end
end
end
class Scene_File
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
def update
   mouse_x, mouse_y = Mouse.get_mouse_pos
   Mouse.click_lock
   idx = 0
   for i in @savefile_windows
   top_x = i.x + 16
   top_y = i.y + 16
   bottom_x = top_x + i.width
   bottom_y = top_y + i.height
   if (mouse_x > top_x) and (mouse_y > top_y) and
      (mouse_x < bottom_x) and (mouse_y < bottom_y)
       i.selected = true
       if @file_index != idx
         @file_index = idx
         $game_system.se_play($data_system.cursor_se)
       end            
       Mouse.click_unlock
   else
       i.selected = false
   end
   idx += 1
   end
   self_update
end
end
class Arrow_Enemy
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
def update
   mouse_x, mouse_y = Mouse.get_mouse_pos
   idx = 0
   for i in $game_troop.enemies do
   if i.exist?
       top_x = i.screen_x - self.ox
       top_y = i.screen_y - self.oy
       bottom_x = top_x + self.src_rect.width
       bottom_y = top_y + self.src_rect.height
       if (mouse_x > top_x - $敌人选框扩大) and (mouse_y > top_y - $敌人选框扩大) and
          (mouse_x < bottom_x + $敌人选框扩大) and (mouse_y < bottom_y + $敌人选框扩大)
         if @index != idx
         $game_system.se_play($data_system.cursor_se)
         @index = idx
         end
       end
   end
   idx += 1
   end
   self_update
end
end
class Arrow_Actor
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
def update
   mouse_x, mouse_y = Mouse.get_mouse_pos
   idx = 0
   for i in $game_party.actors do
   if i.exist?
       top_x = i.screen_x - self.ox
       top_y = i.screen_y - self.oy
       bottom_x = top_x + self.src_rect.width
       bottom_y = top_y + self.src_rect.height
       if (mouse_x > top_x - $角色选框扩大) and (mouse_y > top_y - $角色选框扩大) and
          (mouse_x < bottom_x + $角色选框扩大) and (mouse_y < bottom_y + $角色选框扩大)
         if @index != idx
         $game_system.se_play($data_system.cursor_se)
         @index = idx
         end
       end
   end
   idx += 1
   end
   self_update
end
end
#==============================================================================
# ■ Game_Player
#------------------------------------------------------------------------------
#  处理主角的类。事件启动的判定、以及地图的滚动等功能。
# 本类的实例请参考 $game_player。
#   鼠标控制角色的主程序
#==============================================================================
class Game_Player
if @self_alias == nil
   alias self_update update
   @self_alias = true
end
#--------------------------------------------------------------------------
# ● 得到鼠标的状态
#--------------------------------------------------------------------------
def get_mouse_sta
   return @mouse_sta
end
#--------------------------------------------------------------------------
# ● 完整鼠标系统
#--------------------------------------------------------------------------
def update
   mouse_x, mouse_y = Mouse.get_mouse_pos
   @mtp_x = mouse_x
   @mtp_y = mouse_y
   unless $game_system.map_interpreter.running? and @mouse_sta == 2 #鼠标状态不为跟随状态
   #得到鼠标图标方向
   $mouse_icon_id = $game_map.check_event_custom(mouse_x,mouse_y)if not .include?($mouse_icon_id)
   else
   #令鼠标图标为正常
   $mouse_icon_id = 0 if @mouse_sta != 2
   end
   
   #单击鼠标时进行判断寻路或跟随
   if Mouse.trigger?(Mouse::LEFT) #当点击鼠标时
   unless $game_system.map_interpreter.running? or
            @move_route_forcing or $game_temp.message_window_showing #各种无效情况的排除
       #初始化
       @mouse_sta = 1
       p_direction = 5
       #检查鼠标处能否开启事件
       event_start,p_direction = $game_map.check_event_custom_start(mouse_x, mouse_y)
       #若在移动中再次点击鼠标左键(即双击左键),则改鼠标状态为跟随状态
       @mouse_sta = 2 if @paths_id != nil and @paths_id != @paths.size
       if @mouse_sta != 2
         #鼠标状态不为跟随状态则取数据并初始化路径
         trg_x = (mouse_x + $game_map.display_x / 4) / 32
         trg_y = (mouse_y + $game_map.display_y / 4) / 32
         @paths = []
         @paths_id = 0
         if event_start == 0 #若不能开启事件
         if trg_x != $game_player.x or trg_y != $game_player.y #若目标不为自身则开始寻路
             find_path = Find_Path.new
             @paths = find_path.find_player_short_path(trg_x, trg_y, @mtp_x, @mtp_y)
         end
         else #若能开启事件则改变角色朝向
         @direction = p_direction
         end
       end
   end
   end

   #开始移动
   if @mouse_sta != nil and @mouse_sta == 1 #若鼠标状态为寻路状态
   unless moving? or $game_system.map_interpreter.running? or
            @move_route_forcing or $game_temp.message_window_showing #排除无效情况
       if @paths_id != nil and @paths != nil and @paths_id <= @paths.size #若没有完成路径
         case @paths[@paths_id] #判断路径
         when 6
         @last_move_x = true
         move_right
         @paths_id += 1
         @direction = 6
         when 4
         @last_move_x = true
         move_left
         @paths_id += 1
         @direction = 4
         when 2
         @last_move_x = false
         move_down
         @direction = 2
         @paths_id += 1
         when 8
         @last_move_x = false
         move_up
         @direction = 8
         @paths_id += 1
         end
       end
   end
   elsif @paths != nil and @mouse_sta == 2 #当鼠标状态为跟随,且在移动中
   if Mouse.press?(Mouse::LEFT) #持续按住鼠标
       unless moving? or $game_system.map_interpreter.running? or
            @move_route_forcing or $game_temp.message_window_showing #排除无效情况
         #跟随方向判断并跟随
         if @mtp_x > self.screen_x
         if @mtp_y - self.screen_y > - ( @mtp_x - self.screen_x ) and
            @mtp_y - self.screen_y < @mtp_x - self.screen_x
             move_right
             $mouse_icon_id = 16
             @direction = 6
         end
         if @mtp_y - self.screen_y > @mtp_x - self.screen_x
             move_down
             $mouse_icon_id = 12
             @direction = 2
         end
         if @mtp_y - self.screen_y < - ( @mtp_x - self.screen_x )
             move_up
             $mouse_icon_id = 18
             @direction = 8
         end
         end
         if @mtp_x < self.screen_x
         if @mtp_y - self.screen_y > - ( self.screen_x - @mtp_x ) and
            @mtp_y - self.screen_y < self.screen_x - @mtp_x
             move_left
             $mouse_icon_id = 14
             @direction = 4
         end
         if @mtp_y - self.screen_y > self.screen_x - @mtp_x
             move_down
             $mouse_icon_id = 12
             @direction = 2
         end
         if @mtp_y - self.screen_y < - ( self.screen_x - @mtp_x )
             move_up
             $mouse_icon_id = 18
             @direction = 8
         end
         end
       end
   else #没状态的情况
       $mouse_icon_id = 0
       @mouse_sta = 0
       @paths_id = @paths.size #终止寻路移动
   end
   end
self_update
end
end
Mouse.init
END { Mouse.exit }


四方向寻路(完整鼠标专用):

#==============================================================================
# ■ Find_Path
#------------------------------------------------------------------------------
#  寻路算法--完整鼠标系统(四方向)专用版
#   By whbm
#==============================================================================
class Find_Path
#--------------------------------------------------------------------------
def initialize#初始化
@open_list = []
@close_lise = []
@path = []
end#结束初始化
#--------------------------------------------------------------------------
def fp_passable?(x, y, d, tr_x = -2, tr_y = -2)#开始判定通行
return false if (tr_x == @unable_xa or
               tr_x == @unable_xb or
               tr_y == @unable_ya or
               tr_y == @unable_yb)
if .include?(d)
   if $game_player.passable?(x, y, d)
   return true
   else
   return false
   end
end
end#结束判定通行
#--------------------------------------------------------------------------
def get_g(now_point)#开始计算G值
d = now_point
return 0 if d == 5
father_point = get_father_point(now_point)
g = father_point + 10
return g
end#结束计算G值
#--------------------------------------------------------------------------
def get_h(now_point)#开始计算H值
now_x = now_point
now_y = now_point
#print @trg_x,now_x,@trg_y,now_y
h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
return h * 10
end#结束计算H值
#--------------------------------------------------------------------------
def get_f(now_point)#开始计算F值
f = now_point + now_point
return f
end#结束计算F值
#--------------------------------------------------------------------------
def get_point(x, y) #取已知坐标点
if @open_list.size != 0
   @open_list.each do |point|
   if point == x and point == y
       return point
       break
   end
   end
end
if @close_list.size != 0
   @close_list.each do |point|
   if point == x and point == y
       return point
       break
   end
   end
end
end#结束取已知坐标点
#--------------------------------------------------------------------------
def get_father_point(now_point)#取已知点的父节点
d = now_point
return now_point if d == 5
x = now_point + (d == 6 ? 1 : (d == 4 ? -1 : 0))
y = now_point + (d == 2 ? 1 : (d == 8 ? -1 : 0))
return get_point(x, y)
end#结束取已知点的父节点
#--------------------------------------------------------------------------
def new_point(x, y, d)#开始建立新节点
#print x,y,d
point =
point.push get_g(point)
point.push get_h(point)
point.push get_f(point)
return point
end#结束建立新节点
#--------------------------------------------------------------------------
def get_direction(self_x, self_y, trg_x, trg_y)
if trg_x > self_x
    if trg_y - self_y > -( trg_x - self_x ) and
      trg_y - self_y < ( trg_x - self_x )
      return 6
    end
    if trg_y - self_y > ( trg_x - self_x )
      return 2
    end
    if trg_y - self_y < - ( trg_x - self_x )
      return 8
    end
end
if trg_x < self_x
    if trg_y - self_y > - ( self_x - trg_x ) and
      trg_y - self_y < ( self_x - trg_x )
      return 4
    end
    if trg_y - self_y > ( self_x - trg_x )
      return 2
    end
    if trg_y - self_y < - ( self_x - trg_x )
      return 8
    end
end
end
#--------------------------------------------------------------------------
def get_d_x_y(x, y, d)
d_x = x + (d == 6 ? 1 : (d == 4 ? -1 : 0))
d_y = y + (d == 2 ? 1 : (d == 8 ? -1 : 0))
return d_x, d_y
end
#--------------------------------------------------------------------------
def find_short_path_other(self_x, self_y, trg_x, trg_y,
                        real_self_x, real_self_y, real_trg_x, real_trg_y)
@self_x = self_x
@self_y = self_y
@now_x = self_x
@now_y = self_y
@trg_x = trg_x
@trg_y = trg_y
@path = []
direction = get_direction(real_self_x, real_self_y, real_trg_x, real_trg_y)
@now_trg_x, @now_trg_y = get_d_x_y(@self_x, @self_y, direction)
while fp_passable?(@now_x, @now_y, direction)
    @path.push direction
    @now_x = @now_trg_x
    @now_y = @now_trg_y
    @now_trg_x, @now_trg_y = get_d_x_y(@now_x, @now_y, direction)
end
return @path
end
#--------------------------------------------------------------------------
def find_short_path(self_x, self_y, trg_x, trg_y,
                  real_self_x, real_self_y, real_trg_x, real_trg_y)#开始搜索路径

return find_short_path_other(self_x, self_y, trg_x, trg_y,
                              real_self_x, real_self_y, real_trg_x, real_trg_y) if not
                  (fp_passable?(trg_x, trg_y + 1, 8) or
                   fp_passable?(trg_x + 1, trg_y, 4) or
                   fp_passable?(trg_x - 1, trg_y, 6) or
                   fp_passable?(trg_x, trg_y - 1, 2)) and @goal_type != 1
                  
                  
#根据屏幕限定搜索面积..加速
@unable_xa = $game_map.display_x / 128 - 1
@unable_ya = $game_map.display_y / 128 - 1
@unable_xb = $game_map.display_x / 128 + 20
@unable_yb = $game_map.display_y / 128 + 20


@self_x = self_x
@self_y = self_y
@now_x = self_x
@now_y = self_y
@trg_x = trg_x
@trg_y = trg_y
@open_list = []
@close_list = []
#准备搜索
#print @self_x,@self_y
@now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
@open_list.push @now_point #将当前点加入关闭列表
#开始搜索
begin
loop do
   check_trg = check_around_point(@now_point)
   if check_trg == true
   @path = get_path
   break
   end
   @now_point = get_lowest_f_point
   if @now_point == [] or @now_point == nil
   @path = []
   break
   end
end
rescue Hangup
retry
end
return @path
end#结束搜索路径
#--------------------------------------------------------------------------
def find_player_short_path(trg_x, trg_y,
                           real_trg_x, real_trg_y)#寻找角色的最短路径
self_x = $game_player.x
self_y = $game_player.y
real_self_x = $game_player.screen_x
real_self_y = $game_player.screen_y
@goal_type, event = $game_map.check_event_custom_exist(real_trg_x, real_trg_y)
if @goal_type == 1
   trg_x = event.x
   trg_y = event.y
end
return find_short_path(self_x, self_y, trg_x, trg_y,
                        real_self_x, real_self_y, real_trg_x, real_trg_y)
end#结束角色的寻找路径
#--------------------------------------------------------------------------
def get_path#取得最终的路径
path = []
now_point = @open_list[@open_list.size - 1]
path.push(10 - now_point)
last_point = now_point
loop do
   now_point = get_father_point(now_point)
   break if now_point == 5
   path.push(10 - now_point)
end
return path.reverse
end#结束取得最终的路径
#--------------------------------------------------------------------------
def get_lowest_f_point#开始取得最低F值的点
if @open_list == []
   return []
end
last_lowest_f_point = @open_list
@open_list.each do |point|
   last_lowest_f_point = point if point < last_lowest_f_point
end
return last_lowest_f_point
end#结束取得最低F值点
#--------------------------------------------------------------------------
def check_around_point(point)#开始检查已知点的八方节点
for d in
   x = point + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
   y = point + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
   if in_close_list?(x, y) #在关闭列表中
   next
   elsif in_open_list?(x, y) #在开启列表中
   get_new_g_point = new_point(x, y, 10 - d)
   get_last_g_point = get_point(x, y)
   if get_new_g_point >= get_last_g_point
       next
   else
       #如果改变父节点是新G值更小则确定改变
       @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
   end
   else
   if fp_passable?(point, point, d, x, y)
       # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
       @open_list.push new_point(x, y, 10 - d)
       #如果将目标点添加到了开启列表中就返回true
       return true if x == @trg_x and y == @trg_y
       return true if @goal_type == 1 and .include?(x - @trg_x) and .include?(y - @trg_y)
   end
   end
end
#此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
@close_list.push point
@open_list.delete(point)
#此刻没找到目标点并返回false
return false
end#结束计算已知点的八方节点
#--------------------------------------------------------------------------
def in_open_list?(x, y)#开始检查谋点是否在开启列表中
@open_list.each do |point|
   return true if point == x and point == y
end
return false
end#结束检查谋点是否在开启列表中
#--------------------------------------------------------------------------
def in_close_list?(x, y)#开始检查谋点是否在关闭列表中
@close_list.each do |point|
   return true if point == x and point == y
end
return false
end#结束检查谋点是否在关闭列表中
#--------------------------------------------------------------------------
end

secondsen 发表于 2010-6-15 09:17:16

这么长的东西,就不能直接传个工程。。。

hongqizhen 发表于 2010-6-15 11:22:24

工程下载:
http://u.115.com/file/t243d84e9a
Ella__s_Hope.rar

密码请看短消息,请勿外传。
另外,还要麻烦您一下。
这个游戏中是利用脚本
$game_temp.message_actor_id = 0
来清除头像
利用脚本
$game_temp.message_actor_id = 1
………………
………………
$game_temp.message_actor_id = N
来显示指定的人物头像。
能否帮我改一下,让$game_temp.message_actor_id 不等于0时,顺便在对话框上方出现个名字框,并将对应角色的名字显示上去。等于0时清除头像,顺便也清除名字框。
然后,当对话中有\\name[名字]样的字符后,就将中挎号中的内容显示在名字框里。


只要能帮我解决鼠标问题我就很开心了,关于名字框的请求,如果实在没空就算了。

secondsen 发表于 2010-6-15 14:35:18

外传是不可能的。。。。我本身就讨厌那种人,我拆别人的加密包也是自己拆着玩


话说这个包太大了吧。。。随便放几个图片给我测试就好了

hongqizhen 发表于 2010-6-15 14:52:57

我现在上传的游戏,只要一运行就会变成这样…………


如果把
      $mouse_icon_id = $game_map.check_event_custom(rx,ry)if not .include?($mouse_icon_id)
改为
      $mouse_icon_id = 1

那么开始游戏时就是这样:


进入地图后就是这样:


也就是说,这段代码我没插错地方,但不晓得为什么就是不行…………


麻烦你啦!
另:建议把游戏下下来试试,因为这个游戏的鼠标脚本很乱,分散在各个地方,我虽然把它集中放置在一个地方,并且运行没出错。
但实在很难说是否有漏掉某些小定义之类的…………

secondsen 发表于 2010-6-15 16:09:20

class Game_Event
attr_accessor :flag
end

class Game_Map
#--------------------------------------------------------------------------
# ● 检查鼠标处是否有自定义的事件并返回类型
#--------------------------------------------------------------------------
def check_event_custom(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width / 4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height / 4
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y > event.screen_y - event_height and mouse_y < event.screen_y
      for i in 0...event.list.size
          if event.list.parameters == "item" #类型判断
            event.flag = 1
          elsif event.list.parameters == "npc" #类型判断
            event.flag = 2
          end
          return event.flag #返回事件类型标志
      end
      end
    end
    return $mouse_icon_id #使鼠标图不变化
end
#--------------------------------------------------------------------------
# ● 检查鼠标处是否有事件可以开启
#--------------------------------------------------------------------------
def check_event_custom_start(mouse_x, mouse_y)   
    for event in $game_map.events.values #循环所有事件检查
      #事件角色图片宽度、高度
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
      #判断是否鼠标在事件上
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y > event.screen_y - event_height and mouse_y < event.screen_y
      way_x = $game_player.x - event.x
      way_y = $game_player.y - event.y
      if (.include?($game_player.x-event.x) and $game_player.y-event.y == 0) or (.include?($game_player.y-event.y) and $game_player.x-event.x == 0)
          for i in 0...event.list.size
            if ["item","npc"].include?(event.list.parameters) #当事件属于自定义事件
            #判断主角朝向
            if way_x == -1
                p_direction = 6 if way_y == 0
            elsif way_x == 0
                p_direction = 2 if way_y == -1
                p_direction = 8 if way_y == 1
            else
                p_direction = 4 if way_y == 0
            end
            event.start #开启事件
            return 1, p_direction #返回即将开启事件以及角色朝向
            end
          end
      end
      end
    end
    return 0, 5 #返回不会开启事件以及角色朝向不变
end
#--------------------------------------------------------------------------
# ● 检查鼠标处是否存在自定义事件 for 寻路
#--------------------------------------------------------------------------
def check_event_custom_exist(mouse_x, mouse_y)
    for event in $game_map.events.values #循环所有事件检查
      #事件角色图片宽度、高度
      event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
      event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
      if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y > event.screen_y - event_height and mouse_y < event.screen_y
      for i in 0...event.list.size
          return 1, event if ["item", "npc"].include?(event.list.parameters) #返回存在自定义事件以及事件体
      end
      end
    end
    return 0, event #返回不存在自定义事件,以及事件体
end
end















# Encapsulation of a mouse event
MouseEvent = Struct.new(:sender, :trigger, :info)

#==============================================================================
# ** Mouse module
#------------------------------------------------------------------------------
# Manages the Win32API calls and the base mouse functionalities
#==============================================================================
module Mouse
#--------------------------------------------------------------------------
# * 鼠标输入触发器
#--------------------------------------------------------------------------
LEFT_CLICK = 1
RIGHT_CLICK = 2
MIDDLE_CLICK = 4

MOUSE_TRIGGERS =
#--------------------------------------------------------------------------
# * API Declaration
#--------------------------------------------------------------------------
$ShowCursor      = Win32API.new("user32", "ShowCursor", 'i', 'l')
ASYNC_KEY      = Win32API.new('user32', 'GetAsyncKeyState', 'i',   'i')
SCREEN_TO_CLIENT = Win32API.new('user32', 'ScreenToClient',   %w(l p), 'i')
CURSOR_POS       = Win32API.new('user32', 'GetCursorPos',   'p',   'i')
FIND_WINDOW      = Win32API.new('user32', 'FindWindowA',      %w(p p), 'l')

module_function
##
# Start up procedures
#
def start_up
    # 创建一个模块的配置参考
    @@const = System::Mouse
    # 初始化按钮状态
    @button_states = Array.new(5, 0)
    # 计算窗口的句柄
    window_handle
    # 了解鼠标状态
    mouse_status = Utility.read_ini('mouse')
    @enabled = !(mouse_status == '0' || mouse_status == '')
    # 如果鼠标脚本启用则创建鼠标
    if @enabled
      $ShowCursor.call(0)
      # 创建鼠标
      create_cursor
      # 更新游标
      update
    end
end


##
# 终止鼠标脚本
#
def shutdown
    # 终结掉鼠标
    @mouse_sprite.dispose unless @mouse_sprite.nil? || @mouse_sprite.disposed?
    $ShowCursor.call(1)
end

##
# 打开鼠标脚本
# 如果它已经打开,什么都不会发生
#
def turn_on
    return if self.enabled
    @enabled = true
    # 创建鼠标
    create_cursor
    # 更新游标
    update
end

##
# 鼠标脚本关闭。
# 如果它已关闭,什么都不会发生
#
def turn_off
    return unless self.enabled
    @enabled = false
    # 显示光标如果是隐藏
    unless @mouse_sprite.nil? || @mouse_sprite.disposed?
      # 终止鼠标
      @mouse_sprite.dispose
      # 删除引用,以便将垃圾清除干净
      @mouse_sprite = nil
    end
end

##
# 更新
#
def update
    # 只在启用时更新
    return unless self.enabled
    # 取新的位置
    sx, sy = screen_pos
    # 如果鼠标位置发生了变化
    if @sx != sx || @sy != sy
      # 储存新的屏幕x和y
      @sx = sx
      @sy = sy
      # 获取相对位置
      if Input.is_fullscreen
      rx, ry = sx, sy
      else
      rx, ry = screen_to_client(sx, sy)
      end
      return unless rx >= 0 && rx < 640 && ry >= 0 && ry < 480
      @mouse_sprite.x = rx
      @mouse_sprite.y = ry
      
      
      
      
      #########################################################      
      #可能能在此处判断鼠标图像
      #########################################################
      # 变更光标的位置
      if $scene.is_a?(Scene_Map) == false
         $mouse_icon_id = 0
      end
      if $game_map
      mouse_tmp = $mouse_icon_id
      $mouse_icon_id = $game_map.check_event_custom(rx,ry)# if not .include?($mouse_icon_id)
      end
      case $mouse_icon_id
      when 1
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Arrow3.png')
      when 2
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Arrow2.png')
      when 11
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 12
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 13
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 14
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 16
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 17
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 18
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 19
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      when 0
      @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/arrow.png')
      end
      if $game_map && mouse_tmp != $mouse_icon_id
      $mouse_icon_id = mouse_tmp
      end
    end
    # 检查点击
    for trigger in MOUSE_TRIGGERS
      # 获取异步状态
      state = ASYNC_KEY.call(trigger)
      # If 0 or 1
      if state == 0 || state == 1
      if @button_states > 0
          @button_states *= -1
      else
          @button_states = 0
      end
      else
      if @button_states > 0
          @button_states += 1
      else
          @button_states = 1
      end
      end
    end
end

##
# Check for press
#   返回false,如果不启用鼠标
#
def press?(id = LEFT_CLICK)
    return false unless self.enabled
    return @button_states >= 1
end

##
# 查触发
#   返回false,如果不启用鼠标
#
def trigger?(id = LEFT_CLICK)
    return false unless self.enabled
    return @button_states == 1
end

##
# 重复检查
#   返回false,如果不启用鼠标
#
def repeat?(id = LEFT_CLICK)
    return false unless self.enabled
    if @button_states <= 0
      return false
    elsif @button_states == 1
      return true
    elsif @button_states > @@const::REPEAT_START_WAIT
      return @button_states % @@const::REPEAT_WAIT == 1
    end
end

##
# 回true如果鼠标脚本启用
#
def enabled
    @enabled
end

##
# 获得屏幕坐标
#
def screen_pos
    # 包0位置
    pos = .pack('ll')
    # 裸装的光标位置返回呼叫
    return CURSOR_POS.call(pos) == 0 ? nil : pos.unpack('ll')
end

##
# 转换屏的X和Y坐标的相对坐标。
# Top-left corner = (0, 0)
#
def pos
    return @mouse_sprite.x, @mouse_sprite.y
end

##
# 检查鼠标是否在窗口内部
#
def inside?
    x, y = pos
    return x >= 0 && x < 640 && y >= 0 && y < 480
end

##
# 屏幕坐标转换为相对的客户端窗口
#
def screen_to_client(x, y)
    # 如果X和Y空返回零
    return nil unless x and y
    if @ori_pos.nil?
      @stc_wait = 0
    end
    if @stc_wait <= 0
      # Locate upper-left corner
      pack = .pack('ll')
      SCREEN_TO_CLIENT.call(window_handle, pack)
      @ori_pos = pack.unpack('ll')
      # 等待10帧以开始下一次查找
      @stc_wait = 10
    else
      @stc_wait -= 1
    end
    # Translocate X- and Y-coordinates
    x += @ori_pos
    y += @ori_pos
    return x, y
end

##
# 检索窗口句柄
#
def window_handle
    if @window_handle.nil?
      # 游戏名称查找
      game_name = Utility.read_ini('Title')
      # 查找窗口
      @window_handle = FIND_WINDOW.call('RGSS Player', game_name)
    end
    return @window_handle
end

# 开辟了单独的类模块的
class << self
    ###################
    # Private methods #
    ###################
    private
    ##
    # 创建光标
    #
    def create_cursor
      @mouse_sprite = Sprite.new
      @mouse_sprite.z = 50000
      @mouse_sprite.bitmap = RPG::Cache.icon(@@const::DEFAULT_ICON)
    end
end
end




















class Window_Base
##
# 更新鼠标位置
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    return unless $DEBUG
    raise StandardError.new("mouse_move(mx, my) was not overwritten in #{self.class}")
end

##
# 更新鼠标(只有当鼠标启用时才调用)
# Note: The mouse is over the window
#
def mouse_update
    return unless $DEBUG
    raise StandardError.new("mouse_update was not overwritten in #{self.class}")
end

##
# 判断鼠标位置是否超过窗口,是的话让鼠标图像停留在窗口边框
#
def over_contents?(mx, my)
    return mx >= 16 && mx < width - 16 &&
         my >= 16 && my < height - 16
end
end

class Window_Selectable
##
# Get the height of the elements
#
def element_height
    return 32
end

##
# Get the height of the elements
#
def element_width
    return self.width / @column_max - 32
end

##
# Calculates the index from the x and y coordinate
# Assumes (0,0) to be upper-left corner of the contents
#
def calculate_index(x, y)
    ex = x / element_width
    ey = y / element_height
    return ex + (ey + top_row) * @column_max
end

##
#
#
def cause_scroll(index)
    # Getrow
    row = index / @column_max
    # If row is before top row or more to back than back row
    return row < self.top_row || row > self.top_row + (self.page_row_max - 1)
end

##
# 更新鼠标位置
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    # Cursor position less than 0 implies that the cursor is hidden
    return if @index < 0 || !self.active
    # Store the coordinates
    @mx, @my = mx, my
   
    # Ignore the move if it is not over the contents
    if mx >= 16 && mx < width - 16
      index = calculate_index(mx-16, my-16)
      # If the index is valid
      if index < @item_max && index >= 0
      # Scroll wait
      if cause_scroll(index) && self.scroll_wait > 0
          return
      elsif cause_scroll(index)
          self.scroll_wait = System::Mouse::SCROLL_WAIT
          @scrolling = true
      else
          @scrolling = false
      end
      # Change the index
      @index = index
      end
    end
end

##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    # Only update if active
    return unless self.active
    # Scroll wait update
    if self.scroll_wait > 0
      self.scroll_wait -= 1
      return
    elsif @scrolling
      # See if the scrolling should continue
      index = calculate_index(@mx-16, @my-16)
      if cause_scroll(index) && index < @item_max && index >= 0
      self.scroll_wait = System::Mouse::SCROLL_WAIT
      @index = index
      else
      # Stop scrolling
      @scrolling = false
      end
    end
    @mx, @my = -1, -1 if @mx.nil?
    # Check for click
    if Mouse.trigger?(Mouse::LEFT_CLICK) && over_contents?(@mx, @my)
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
end
end

class Window_Target
##
# Update mouse position
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    return unless self.active
    if @index >= 0
      super
      return
    else
      # Store the coordinates
      @mx, @my = mx, my
    end
end
##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    if @index >= 0
      super
      return
    else
      # Only update if active
      return unless self.active
      @mx, @my = -1, -1 if @mx.nil?
      # Check for click
      if Mouse.trigger?(Mouse::LEFT_CLICK) && over_contents?(@mx, @my)
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
      end
    end
end
end

class Window_NameInput
##
# Determines whether the given coordinates are within the given area
#
def is_in?(x,y, rx,ry,rw,rh)
    return x >= rx && x < rx + rw &&
         y >= ry && y < ry + rh
end

##
# Calculates the index from the coordinates
#
def calculate_index(x, y)
    # Check position
    return 90 if is_in?(x,y,428, 9 * 32, 48, 32)
    if is_in?(x,y,140, 0, 5*32, 9*32)
      # Left box
      x = (x - 140) / 32
      y /= 32
      return x + y * 5
    elsif is_in?(x,y,320, 0, 5*32, 9*32)
      # Right box
      x = (x - 320) / 32
      y /= 32
      return x + y * 5 + 45
    end
    # Outside
    return -1
end

##
# Update mouse position
# Mouse enabled subclasses should overwrite this method
#
def mouse_move(mx, my)
    @mx, @my = mx, my
    index = calculate_index(mx-16, my-16)
    @index = index unless index < 0
end

##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    if Mouse.trigger?(Mouse::LEFT_CLICK) && calculate_index(@mx-16, @my-16) >= 0
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
end
end

class Window_Message
##
# Check for normal trigger
#
def normal_trigger?
    return true if Input.trigger?(Input::C)
    if Mouse.trigger?(Mouse::LEFT_CLICK)
      return true if $game_temp.choice_max <= 0
      index = calculate_index(@mx - 16, @my - 16)
      return true if index >= 0 && index < @item_max
    end
end

##
# Update mouse (Only called if mouse is enabled)
#
def mouse_update
    # Do nothing
end

##
# Calculates the index from the x and y coordinate
# Assumes (0,0) to be upper-left corner of the contents
#
def calculate_index(x, y)
    return -1 if x > @cursor_width + 8
    ey = (y - $game_temp.choice_start * 32) / element_height
    return (ey + top_row) * @column_max
end
end

class Arrow_Base
##
# Update mouse (Only called if mouse is enabled)
# Note: The mouse is over the window
#
def mouse_update
    return unless $DEBUG
    raise StandardError.new("mouse_update was not overwritten in #{self.class}")
end
end

class Arrow_Enemy
##
# Update mouse position
#
def mouse_move(mx, my)
    # Pass Through Enemies
    for enemy in $game_troop.enemies
      # Skip if Non-Existing Enemy
      next unless enemy.exist?
      # Gets Paddings
      w, h = enemy.battler_width / 2, enemy.battler_height
      # If Within Mouse Padding Range
      if mx.between?(enemy.screen_x - w, enemy.screen_x + w) &&
         my.between?(enemy.screen_y - h, enemy.screen_y + 10)
      # Set Index
      @index = $game_troop.enemies.index(enemy)
      break
      end
    end
end

##
# Update mouse (Only called if mouse is enabled)
#
def mouse_update
    if Mouse.trigger?(Mouse::LEFT_CLICK)# && @my < 320
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
    mx, my = Mouse.pos
    return if @mx == mx && @my == my
    @mx, @my = mx, my
    # Pass Through Enemies
    for enemy in $game_troop.enemies
      # Skip if Non-Existing Enemy
      next unless enemy.exist?
      # Gets Paddings
      w, h = enemy.battler_width / 2, enemy.battler_height
      # If Within Mouse Padding Range
      if mx.between?(enemy.screen_x - w, enemy.screen_x + w) &&
         my.between?(enemy.screen_y - h, enemy.screen_y + 10)
      # Set Index
      @index = $game_troop.enemies.index(enemy)
      break
      end
    end
end
end

class Arrow_Actor
##
# Update mouse (Only called if mouse is enabled)
#
def mouse_update
    if Mouse.trigger?(Mouse::LEFT_CLICK)# && @my > 288
      owner.fireEvent(MouseEvent.new(self, Mouse::LEFT_CLICK, @index))
    end
    # Get mouse position
    mx, my = Mouse.pos
    # Checks that the mouse have moved
    return if @mx == mx && @my == my
    # Store new position
    @mx, @my = mx, my
    # Pass Through Actors
    for actor in $game_party.actors
      # Gets Paddings
      w, h = actor.battler_width / 2, actor.battler_height
      # If Within Mouse Padding Range
      if mx.between?(actor.screen_x - w, actor.screen_x + w) &&
         my.between?(actor.screen_y - h, actor.screen_y + 10)
      # Set Index
      @index = $game_party.actors.index(actor)
      end
    end
end
end

secondsen 发表于 2010-6-15 16:11:47

这个就是把原来的鼠标替换掉就好了。。。就是你给我的那个工程里的Mouse module完全用这个替换掉

用法。。。事件页第一句 用事件脚本 里面 写npc代表变成 对话图标
item 代表 抓手图标。。。以上

secondsen 发表于 2010-6-15 16:13:40

另外。。。关于对话的。。。你直接引进 fuki吧。。。反正你是要翻译的对吧?不如用fuki还好一些

hongqizhen 发表于 2010-6-15 16:24:05

好感动哦!
secondsen 斑竹是世界第一大好人!辛苦了!
我马上改去!
页: [1] 2
查看完整版本: 求助-RMXP鼠标脚本的问题