Project1

标题: 【已解决】谢谢帮助 [打印本页]

作者: 凯Katy    时间: 2026-4-29 13:19
标题: 【已解决】谢谢帮助
本帖最后由 凯Katy 于 2026-4-30 08:17 编辑

为什么我的背包界面无法使用物品


为什么一但使用了物品就会卡在这个画面,按什么按键都没有用

代码部分

#==============================================================================
# ▼ 全屏背包插件(基于图片 shukai.png)
# 功能:将 Graphics/Pictures/shukai.png 拉伸至全窗口作为背景,
#       物品列表显示在左半区域,物品说明在右半区域。
#==============================================================================

module CustomBag
  BG_IMAGE = "shukai"                # 背景图片文件名

  # 物品列表窗口区域(左半部分)
  # 格式:Rect.new(x, y, width, height)  坐标以窗口左上角为原点(0,0)
  LIST_RECT = Rect.new(20, 40, 250, 336)

  # 物品说明窗口区域(右半部分)
  DESC_RECT = Rect.new(280, 40, 244, 336)

  LINE_HEIGHT = 24                   # 物品列表每行高度(像素)
end

#==============================================================================
# 物品列表窗口(透明背景,仅显示图标和名称)
#==============================================================================
class Window_BagItemList < Window_Selectable
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @data = []
    refresh
    self.index = 0
    activate
    self.opacity = 0
    self.back_opacity = 0
  end

  def refresh
    @data = []
    return unless $game_party
    $game_party.items.each { |i| @data << i unless i.key_item? }
    $game_party.items.each { |i| @data << i if i.key_item? }
    $game_party.weapons.each { |w| @data << w }
    $game_party.armors.each { |a| @data << a }
    create_contents
    draw_all_items
  end

  def item
    @data[index] if @data && index >= 0 && index < @data.size
  end

  def draw_item(index)
    item = @data[index]
    return unless item
    rect = item_rect(index)
    draw_icon(item.icon_index, rect.x, rect.y)
    draw_text(rect.x + 24, rect.y, rect.width - 24, line_height, item.name)
  end

  def item_max
    @data ? @data.size : 0
  end
end

#==============================================================================
# 物品说明窗口(透明背景,显示描述文字)
#==============================================================================
class Window_BagDesc < Window_Base
  def initialize(x, y, width, height)
    super(x, y, width, height)
    self.opacity = 0
    self.back_opacity = 0
    @item = nil
  end

  def set_item(item)
    @item = item
    refresh
  end

  def refresh
    contents.clear
    return unless @item
    draw_text_ex(4, 4, @item.description)
  end
end

#==============================================================================
# 自定义背包场景
#==============================================================================
class Scene_Bag < Scene_Base
  def start
    super
    create_background
    create_item_list
    create_description
  end

  def create_background
    @bg_sprite = Sprite.new
    src = Cache.picture(CustomBag::BG_IMAGE)
    @bg_sprite.bitmap = Bitmap.new(Graphics.width, Graphics.height)
    @bg_sprite.bitmap.stretch_blt(@bg_sprite.bitmap.rect, src, src.rect)
    @bg_sprite.x = 0
    @bg_sprite.y = 0
  end

  def create_item_list
    @list_window = Window_BagItemList.new(
      CustomBag::LIST_RECT.x, CustomBag::LIST_RECT.y,
      CustomBag::LIST_RECT.width, CustomBag::LIST_RECT.height
    )
    @list_window.set_handler(:ok, method(:use_item))
    @list_window.set_handler(:cancel, method(:return_scene))
  end

  def create_description
    @desc_window = Window_BagDesc.new(
      CustomBag::DESC_RECT.x, CustomBag::DESC_RECT.y,
      CustomBag::DESC_RECT.width, CustomBag::DESC_RECT.height
    )
    update_description
  end

  def update_description
    item = @list_window.item
    @desc_window.set_item(item)
  end

  def update
    super
    if @list_window && @list_window.active
      if @last_index != @list_window.index
        update_description
        @last_index = @list_window.index
      end
    end
  end

  def use_item
    item = @list_window.item
    if item && item.is_a?(RPG::Item) && item.consumable
      $game_party.consume_item(item)
      @list_window.refresh
      update_description
      Sound.play_use_item
    else
      Sound.play_buzzer
    end
  end

  def return_scene
    SceneManager.goto(Scene_Map)
  end
end

#==============================================================================
# 替换默认物品场景
#==============================================================================
class << SceneManager
  alias_method :custom_bag_call, :call
  def call(scene_class)
    if scene_class == Scene_Item
      custom_bag_call(Scene_Bag)
    else
      custom_bag_call(scene_class)
    end
  end
end

作者: l001213    时间: 2026-4-29 15:47
可能是使用物品时没有实现目标选择
作者: alexncf125    时间: 2026-4-29 22:11
def use_item里再加句@list_window.activate吧?
作者: 凯Katy    时间: 2026-4-30 08:10
alexncf125 发表于 2026-4-29 22:11
def use_item里再加句@list_window.activate吧?

不行呢,但感谢帮助
作者: 凯Katy    时间: 2026-4-30 08:14
alexncf125 发表于 2026-4-29 22:11
def use_item里再加句@list_window.activate吧?

删了一段代码,在加@list_window.activate就能用了,谢谢帮助





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1