设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1695|回复: 3
打印 上一主题 下一主题

[已经解决] 物品合成脚本第224行出错

[复制链接]

Lv1.梦旅人

梦石
0
星屑
215
在线时间
51 小时
注册时间
2015-11-8
帖子
20
跳转到指定楼层
1
发表于 2016-1-26 16:49:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 RyanBern 于 2016-1-26 21:33 编辑

代码如下
RUBY 代码复制
  1. #=============================================================================
  2. # 物品合成简易优化版 Ver 1.0
  3. #-----------------------------------------------------------------------------
  4. # By : RyanBern
  5. #-----------------------------------------------------------------------------
  6. # 功能介绍:
  7. #     这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
  8. #     通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
  9. #     改成了外部文件的形式,下面就说明一下如何使用。
  10. #-----------------------------------------------------------------------------
  11. # 使用方法:
  12. #     1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
  13. #       再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
  14. #       <recipe=1>
  15. #         <product>kind=0,id=3</product>
  16. #         <ingredients>
  17. #           kind=0,id=1,number=2
  18. #           kind=0,id=2,number=2
  19. #         </ingredients>
  20. #       </recipe>
  21. #       其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
  22. #       第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
  23. #       就是对应的ID
  24. #       第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
  25. #       每一行代表一种原料的分类,id,和数量,注意不要写重复了。
  26. #       如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
  27. #     2.有关学习配方:
  28. #       $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
  29. #       $game_party.learn_recipe(配方ID) 学习该ID的配方
  30. #       $game_party.forget_recipe(配方ID) 失去该ID的配方
  31. #     3.有关场景调用:
  32. #       $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
  33. #       $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
  34. #       $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
  35. #-----------------------------------------------------------------------------
  36. # 注意事项:
  37. #     1.Recipes.txt里面的缩进不是必须的。
  38. #     2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
  39. #       以便日后做出更改。
  40. #-----------------------------------------------------------------------------
  41. # 更新记录:
  42. #=============================================================================
  43. module RB
  44.   Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m
  45.   Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m
  46.   Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m
  47.   Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
  48. end
  49. module RPG
  50.   class Recipe
  51.     attr_accessor :id
  52.     attr_accessor :product
  53.     attr_accessor :ingredients
  54.     def initialize
  55.       @id = 0
  56.       @product = Ingredient.new
  57.       @ingredients = []
  58.     end
  59.     class Ingredient
  60.       attr_accessor :kind
  61.       attr_accessor :id
  62.       attr_accessor :number
  63.       def initialize(kind = 0, id = 0, number = 0)
  64.         @kind = kind
  65.         @id = id
  66.         @number = number
  67.       end
  68.     end
  69.   end
  70.   def self.extract_recipe
  71.     filename = "Recipes.txt"
  72.     outfile_name = "Data/Recipes.rxdata"
  73.     data = []
  74.     begin
  75.       file = File.open(filename, "r")
  76.       str = file.read
  77.       file.close
  78.       while str.slice!(RB::Regex_Recipe)
  79.         id = $1.to_i
  80.         contents = $2
  81.         recipe = Recipe.new
  82.         recipe.id = id
  83.         if RB::Regex_Product =~ contents
  84.           recipe.product.kind = $1.to_i
  85.           recipe.product.id = $2.to_i
  86.         end
  87.         if RB::Regex_Ingredient =~ contents
  88.           ingredient_contents = $1
  89.           ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|
  90.             recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
  91.           end
  92.         end
  93.         data[id] = recipe
  94.       end
  95.       outfile = File.open(outfile_name, "wb")
  96.       Marshal.dump(data, outfile)
  97.       outfile.close
  98.     rescue
  99.       p 1
  100.     end
  101.   end
  102. end
  103.  
  104. class Game_Party
  105.   alias rb_initialize_20141226 initialize
  106.   def initialize
  107.     rb_initialize_20141226
  108.     @recipes = {}
  109.   end
  110.   def recipe_learn?(recipe_id)
  111.     @recipes ||= {}
  112.     return @recipes[recipe_id]
  113.   end
  114.   def learn_recipe(recipe_id)
  115.     @recipes ||= {}
  116.     @recipes[recipe_id] = true
  117.   end
  118.   def forget_recipe(recipe_id)
  119.     @recipes ||= {}
  120.     @recipes[recipe_id] = nil
  121.   end
  122.   def product_max_num(recipe_id)
  123.     max = -1
  124.     recipe = $data_recipes[recipe_id]
  125.     return 0 if recipe.nil?
  126.     recipe.ingredients.each do |ingredient|
  127.       num_need = ingredient.number
  128.       case ingredient.kind
  129.       when 0
  130.         num_current = item_number(ingredient.id)
  131.       when 1
  132.         num_current = weapon_number(ingredient.id)
  133.       when 2
  134.         num_current = armor_number(ingredient.id)
  135.       else
  136.         num_current = 0
  137.       end
  138.       max = num_current / num_need if max == -1 || num_current / num_need < max
  139.     end
  140.     return max == -1 ? 0 : max
  141.   end
  142. end
  143.  
  144. class Window_Product < Window_Selectable
  145.   def initialize(mode = -1)
  146.     super(0, 64, 240, 480 - 64)
  147.     self.index = 0
  148.     @mode = mode
  149.     refresh
  150.   end
  151.   def recipe
  152.     return @data[self.index]
  153.   end
  154.   def item
  155.     recipe = self.recipe
  156.     return nil if recipe.nil?
  157.     product = recipe.product
  158.     case product.kind
  159.     when 0
  160.       item = $data_items[product.id]
  161.     when 1
  162.       item = $data_weapons[product.id]
  163.     when 2
  164.       item = $data_armors[product.id]
  165.     else
  166.       item = nil
  167.     end
  168.     return item
  169.   end
  170.   def refresh
  171.     if self.contents != nil
  172.       self.contents.dispose
  173.       self.contents = nil
  174.     end
  175.     @data = []
  176.     (1...$data_recipes.size).each do |i|
  177.       if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
  178.         @data << $data_recipes[i] if $data_recipes[i] != nil
  179.       end
  180.     end
  181.     @item_max = @data.size
  182.     if @item_max > 0
  183.       self.contents = Bitmap.new(width - 32, @item_max * 32)
  184.       @data.each_index{|i| draw_item(i)}
  185.     else
  186.       self.contents = Bitmap.new(width - 32, 32)
  187.       self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
  188.     end
  189.   end
  190.   def draw_item(index)
  191.     x = 4
  192.     y = 32 * index
  193.     product = @data[index].product
  194.     case product.kind
  195.     when 0
  196.       item = $data_items[product.id]
  197.     when 1
  198.       item = $data_weapons[product.id]
  199.     when 2
  200.       item = $data_armors[product.id]
  201.     else
  202.       item = nil
  203.     end
  204.     if item == nil
  205.       return
  206.     end
  207.     disabled = $game_party.product_max_num(@data[index].id) == 0
  208.     bitmap = RPG::Cache.icon(item.icon_name)
  209.     opacity = disabled ? 128 : 255
  210.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  211.     self.contents.font.color = disabled ? disabled_color : normal_color
  212.     self.contents.draw_text(x + 28, y, 176, 32, item.name)
  213.   end
  214.   def update_help
  215.     @help_window.set_text(self.item == nil ? "" : self.item.description)
  216.   end
  217. end
  218.  
  219. class Window_Ingredients < Window_Base
  220.   def initialize
  221.     super(240, 288, 400, 192)
  222.     @waiting = 0
  223.     @dy = 1
  224.     [url=home.php?mod=space&uid=2564094]@recipe[/url] = nil
  225.     refresh
  226.   end
  227.   def recipe=(recipe)
  228.     if [url=home.php?mod=space&uid=2564094]@recipe[/url] != recipe
  229.       @recipe = recipe
  230.       @waiting = 40
  231.       @dy = 1
  232.       self.oy = 0
  233.       refresh
  234.     end
  235.   end
  236.   def refresh
  237.     if self.contents != nil
  238.       self.contents.dispose
  239.       self.contents = nil
  240.     end
  241.     if @recipe == nil
  242.       return
  243.     end
  244.     h = 32 * @recipe.ingredients.size
  245.     if h > 0
  246.       self.contents = Bitmap.new(width - 32, h)
  247.       @recipe.ingredients.each_with_index do |ingredient, index|
  248.         draw_item(ingredient, index)
  249.       end
  250.     end
  251.   end
  252.   def draw_item(ingredient, index)
  253.     x = 4
  254.     y = index * 32
  255.     case ingredient.kind
  256.     when 0
  257.       item = $data_items[ingredient.id]
  258.       num = $game_party.item_number(ingredient.id)
  259.     when 1
  260.       item = $data_weapons[ingredient.id]
  261.       num = $game_party.weapon_number(ingredient.id)
  262.     when 2
  263.       item = $data_armors[ingredient.id]
  264.       num = $game_party.armor_number(ingredient.id)
  265.     else
  266.       item = nil
  267.     end
  268.     if item == nil
  269.       return
  270.     end
  271.     bitmap = RPG::Cache.icon(item.icon_name)
  272.     disabled = num < ingredient.number
  273.     opacity = disabled ? 128 : 255
  274.     self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  275.     self.contents.font.color = disabled ? disabled_color : normal_color
  276.     self.contents.draw_text(x + 28, y, 192, 32, item.name)
  277.     self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
  278.     self.contents.font.color = normal_color
  279.     self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
  280.   end
  281.   def update
  282.     super
  283.     if self.contents != nil && self.contents.height > self.height - 32
  284.       if @waiting > 0
  285.         @waiting -= 1
  286.         return
  287.       end
  288.       self.oy += @dy
  289.       if self.oy + self.height - 32 >= self.contents.height
  290.         @dy = -1
  291.         @waiting = 60
  292.       end
  293.       if self.oy <= 0
  294.         @dy = 1
  295.         @waiting = 60
  296.       end
  297.     end
  298.   end
  299. end
  300.  
  301. class Window_Craft_Number < Window_Base
  302.   def initialize
  303.     super(240, 64, 400, 160)
  304.     self.contents = Bitmap.new(width - 32, height - 32)
  305.     @recipe = nil
  306.     [url=home.php?mod=space&uid=25307]@Max[/url] = 1
  307.     @number = 1
  308.     @number_current = 0
  309.   end
  310.   def recipe=(recipe)
  311.     if @recipe != recipe
  312.       @recipe = recipe
  313.       reset
  314.       refresh
  315.     end
  316.   end
  317.   def reset
  318.     if @recipe == nil
  319.       @number = 0
  320.       @number_current = 0
  321.       return
  322.     end
  323.     product = @recipe.product
  324.     case product.kind
  325.     when 0
  326.       @number_current = $game_party.item_number(product.id)
  327.     when 1
  328.       @number_current = $game_party.weapon_number(product.id)
  329.     when 2
  330.       @number_current = $game_party.armor_number(product.id)
  331.     else
  332.       @number_current = 0
  333.     end
  334.     [url=home.php?mod=space&uid=25307]@Max[/url] = $game_party.product_max_num(@recipe.id)
  335.     @max = [@max, 99 - @number_current].min
  336.     @number = @max == 0 ? 0 : 1
  337.   end
  338.   def number
  339.     return @number
  340.   end
  341.   #--------------------------------------------------------------------------
  342.   # ● 刷新
  343.   #--------------------------------------------------------------------------
  344.   def refresh
  345.     self.contents.clear
  346.     self.contents.font.color = system_color
  347.     self.contents.draw_text(4, 0, 364, 32, "所持数")
  348.     self.contents.draw_text(4, 32, 364, 32, "最大可合成数")
  349.     self.contents.draw_text(4, 64, 364, 32, "当前合成数量")
  350.     self.contents.font.color = normal_color
  351.     self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
  352.     self.contents.draw_text(4, 32, 364, 32, @max.to_s, 2)
  353.     self.contents.draw_text(252, 96, 32, 32, "×")
  354.     self.contents.draw_text(308, 96, 24, 32, @number.to_s, 2)
  355.   end
  356.   def update
  357.     super
  358.     if self.active
  359.       # 光标右 (+1)
  360.       if Input.repeat?(Input::RIGHT) and @number < @max
  361.         $game_system.se_play($data_system.cursor_se)
  362.         @number += 1
  363.         refresh
  364.       end
  365.       # 光标左 (-1)
  366.       if Input.repeat?(Input::LEFT) and @number > 1
  367.         $game_system.se_play($data_system.cursor_se)
  368.         @number -= 1
  369.         refresh
  370.       end
  371.       # 光标上 (+10)
  372.       if Input.repeat?(Input::UP) and @number < @max
  373.         $game_system.se_play($data_system.cursor_se)
  374.         @number = [@number + 10, @max].min
  375.         refresh
  376.       end
  377.       # 光标下 (-10)
  378.       if Input.repeat?(Input::DOWN) and @number > 1
  379.         $game_system.se_play($data_system.cursor_se)
  380.         @number = [@number - 10, 1].max
  381.         refresh
  382.       end
  383.     end
  384.   end
  385. end
  386. class Window_Craft_Label < Window_Base
  387.   def initialize
  388.     super(240, 224, 400, 64)
  389.     self.contents = Bitmap.new(width - 32, height - 32)
  390.     refresh
  391.   end
  392.   def refresh
  393.     self.contents.clear
  394.     self.contents.font.color = system_color
  395.     self.contents.draw_text(0, 0, width - 32, 32, "材料名称               现有  需要")
  396.   end
  397. end
  398.  
  399. class Scene_Title
  400.   alias rb_main_20141226 main
  401.   def main
  402.     if $DEBUG
  403.       RPG.extract_recipe
  404.     end
  405.     $data_recipes = load_data("Data/Recipes.rxdata")
  406.     rb_main_20141226
  407.   end
  408. end
  409.  
  410. class Scene_Craft
  411.   def initialize(mode = -1)
  412.     @mode = mode
  413.   end
  414.   def main
  415.     @product_window = Window_Product.new(@mode)
  416.     @help_window = Window_Help.new
  417.     @product_window.help_window = @help_window
  418.     @ingredient_window = Window_Ingredients.new
  419.     @number_window = Window_Craft_Number.new
  420.     @label_window = Window_Craft_Label.new
  421.     Graphics.transition
  422.     loop do
  423.       Graphics.update
  424.       Input.update
  425.       update
  426.       if $scene != self
  427.         break
  428.       end
  429.     end
  430.     Graphics.freeze
  431.     @product_window.dispose
  432.     @help_window.dispose
  433.     @ingredient_window.dispose
  434.     @number_window.dispose
  435.     @label_window.dispose
  436.   end
  437.   def update
  438.     @product_window.update
  439.     @ingredient_window.update
  440.     @ingredient_window.recipe = @product_window.recipe
  441.     @number_window.recipe = @product_window.recipe
  442.     if @product_window.active
  443.       update_product
  444.       return
  445.     end
  446.     if @number_window.active
  447.       update_number
  448.     end
  449.   end
  450.   def update_product
  451.     if Input.trigger?(Input::B)
  452.       $game_system.se_play($data_system.cancel_se)
  453.       $scene = Scene_Map.new
  454.       return
  455.     end
  456.     if Input.trigger?(Input::C)
  457.       recipe = @product_window.recipe
  458.       if recipe == nil || $game_party.product_max_num(recipe.id) == 0
  459.         $game_system.se_play($data_system.buzzer_se)
  460.         return
  461.       end
  462.       $game_system.se_play($data_system.decision_se)
  463.       @number_window.cursor_rect.set(304, 96, 32, 32)
  464.       @product_window.active = false
  465.       @number_window.active = true
  466.       return
  467.     end
  468.   end
  469.   def update_number
  470.     @number_window.update
  471.     if Input.trigger?(Input::B)
  472.       $game_system.se_play($data_system.cancel_se)
  473.       @number_window.active = false
  474.       @product_window.active = true
  475.       @number_window.cursor_rect.empty
  476.       return
  477.     end
  478.     if Input.trigger?(Input::C)
  479.       $game_system.se_play($data_system.shop_se)
  480.       recipe = @product_window.recipe
  481.       case recipe.product.kind
  482.       when 0
  483.         $game_party.gain_item(recipe.product.id, @number_window.number)
  484.       when 1
  485.         $game_party.gain_weapon(recipe.product.id, @number_window.number)
  486.       when 2
  487.         $game_party.gain_armor(recipe.product.id, @number_window.number)
  488.       end
  489.       recipe.ingredients.each do |ingredient|
  490.         case ingredient.kind
  491.         when 0
  492.           $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
  493.         when 1
  494.           $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
  495.         when 2
  496.           $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
  497.         end
  498.       end
  499.       @product_window.refresh
  500.       @ingredient_window.refresh
  501.       @number_window.reset
  502.       @number_window.refresh
  503.       @product_window.active = true
  504.       @number_window.active = false
  505.       @number_window.cursor_rect.empty
  506.       return
  507.     end
  508.   end
  509. end

评分

参与人数 1星屑 +35 收起 理由
RyanBern + 35 手动认可奖励

查看全部评分

Lv4.逐梦者 (版主)

梦石
0
星屑
9532
在线时间
5073 小时
注册时间
2013-6-21
帖子
3580

开拓者贵宾剧作品鉴家

2
发表于 2016-1-26 21:35:32 | 只看该作者
代码被论坛错误解析。
代码中有多处
  1. [url=home....]xxxx[/url]
复制代码
请将含url的部分(包括方括号)去掉。

或者是直接下载范例工程。
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
215
在线时间
51 小时
注册时间
2015-11-8
帖子
20
3
 楼主| 发表于 2016-1-27 19:41:56 | 只看该作者
那第193行错误是怎么回事?

点评

我这边测试没有发现问题,请前往http://rm.66rpg.com/thread-374803-1-1.html发布帖下载范例工程查看一下?  发表于 2016-1-27 21:16
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
215
在线时间
51 小时
注册时间
2015-11-8
帖子
20
4
 楼主| 发表于 2016-1-28 15:37:30 | 只看该作者
灰色的 发表于 2016-1-27 19:41
那第193行错误是怎么回事?

谢谢您,问题已经解决
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-9-22 21:35

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表