Project1

标题: 为什么一个变量等于一个数组的单元数会出错 [打印本页]

作者: 876加几    时间: 2013-10-4 14:22
标题: 为什么一个变量等于一个数组的单元数会出错
本帖最后由 876加几 于 2013-10-4 15:50 编辑

单元数判断:
  1. 数组名称.size
复制代码
方法:
  1. @数组名称_size = @数组名称.size
复制代码
用数组新建任务就会总是提示.size出错,帮助文本本来就这么写的呀,怎么会出错呢?
原脚本:
  1. #==============================================================================
  2. # ■ Window_Task
  3. #------------------------------------------------------------------------------
  4. #  显示任务的选择行窗口。
  5. #==============================================================================

  6. class Window_Task < Window_Selectable
  7.   #--------------------------------------------------------------------------
  8.   # ● 初始化对像
  9.   #--------------------------------------------------------------------------
  10.   def initialize
  11.     super(0, 0, 640, 480 - 64)
  12.     @name = nil
  13.     @task_help = nil
  14.     [url=home.php?mod=space&uid=236945]@gold[/url] = nil
  15.     @items = nil
  16.     @weapons = nil
  17.     @armors = nil
  18.     @item = nil
  19.     [url=home.php?mod=space&uid=36793]@weapon[/url] = nil
  20.     @armor = nil
  21.     if @name == nil
  22.       @item_max = 1
  23.     else
  24.       @item_max = @name.size
  25.     end
  26.     difficulty = nil
  27.     self.contents = Bitmap.new(width - 32, height - 32)
  28.     refresh
  29.     self.index = -1
  30.   end
  31.   #--------------------------------------------------------------------------
  32.   # ● 刷新
  33.   #--------------------------------------------------------------------------
  34.   def refresh
  35.     self.contents.clear
  36.     if @name == nil
  37.       @item_max = 1
  38.     else
  39.       @item_max = @name.size
  40.     end
  41.   end
  42.   #--------------------------------------------------------------------------
  43.   # ● 报偿设置
  44.   #     item_id/weapon_id/armor_id : 报偿物品在数据库中的位置,nil等于没报偿
  45.   #--------------------------------------------------------------------------
  46.   def reward_items(item_id = nil)
  47.     @item[@item.size] = item_id
  48.   end
  49.   def reward_weapons(weapon_id = nil)
  50.     @weapon[@weapon.size] = weapon_id
  51.   end
  52.   def reward_armor(armor_id = nil)
  53.     @armor[@armor.size] = armor_id
  54.   end
  55.   #--------------------------------------------------------------------------
  56.   # ● 新建任务
  57.   #     new_name   :       新任务的名称
  58.   #     task_help  :       新任务的简介
  59.   #     difficulty :       新任务的难度
  60.   #     gold       :       金钱报偿,0等于没报偿
  61.   #--------------------------------------------------------------------------
  62.   def new_task(new_name, task_help, difficulty = 1, gold = 0)
  63.     @name_size = @name.size
  64.     @task_help_size = @task_help.size
  65.     difficulty_size = difficulty.size
  66.     @gold_size = @gold.size
  67.     @items_size = @items.size
  68.     @weapons_size = @weapons.size
  69.     @armors_size = @armors.size
  70.     @name[@name_size] = new_name
  71.     @name2[@name_size] = new_name
  72.     @task_help[@task_help_size] = task_help
  73.     difficulty[difficulty_size] = difficulty
  74.     @gold[@gold_size] = gold
  75.     @items[@items_size] = @item
  76.     @weapons[@weapons_size] = @weapon
  77.     @armors[@armors_size] = @armor
  78.   end
  79.   #--------------------------------------------------------------------------
  80.   # ● 完成任务
  81.   #     bbb   : 任务ID
  82.   #--------------------------------------------------------------------------
  83.   def final_task(bbb)
  84.     [url=home.php?mod=space&uid=110690]@BBB[/url] = bbb - 1
  85.     unless @name[@bbb].include?("(已失败)")
  86.       @name[@bbb] = @name[@bbb] + "(已完成)"
  87.       @task_help[@bbb] = @task_help[@bbb] + "(已完成)"
  88.       $game_party.gain_gold(@gold[bbb])
  89.       for i in [email protected]
  90.         if @item != nil
  91.           $game_party.gain_item(@item[i], 1)
  92.         end
  93.       end
  94.       for i in [email protected]
  95.         if @weapon != nil
  96.           $game_party.gain_weapon(@weapon[i], 1)
  97.         end
  98.       end
  99.       for i in [email protected]
  100.         if @armor != nil
  101.           $game_party.gain_armor(@armor[i], 1)
  102.         end
  103.       end
  104.     end
  105.   end
  106.   #--------------------------------------------------------------------------
  107.   # ● 任务失败
  108.   #     ccc   : 任务ID
  109.   #--------------------------------------------------------------------------
  110.   def lose_task(ccc)
  111.     [url=home.php?mod=space&uid=9267]@CCC[/url] = ccc - 1
  112.     unless @name[@bbb].include?("(已完成)")
  113.       @name[@ccc] = @name[@ccc] + "(已失败)"
  114.       @task_help[@ccc] = @task_help[@ccc] + "(已失败)"
  115.     end
  116.   end
  117.   #--------------------------------------------------------------------------
  118.   # ● 描绘项目
  119.   #     index : 项目编号
  120.   #     color : 文字色
  121.   #--------------------------------------------------------------------------
  122.   def draw_item(index,color)
  123.     self.contents.font.color = color
  124.     rect = Rect.new(4, 32 * index, 640 / 2 - 8, 32)
  125.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  126.     self.contents.draw_text(rect, @name[index])
  127.   end
  128.   #--------------------------------------------------------------------------
  129.   # ● 项目无效化
  130.   #     index : 项目编号
  131.   #--------------------------------------------------------------------------
  132.   def disable_item(index)
  133.     draw_item(index, disabled_color)
  134.   end
  135.   #--------------------------------------------------------------------------
  136.   # ● 刷新光标矩形
  137.   #--------------------------------------------------------------------------
  138.   def update_cursor_rect
  139.     if [url=home.php?mod=space&uid=370741]@Index[/url] < 0
  140.       self.cursor_rect.empty
  141.     else
  142.       self.cursor_rect.set(0, @index * 56, 320 - 8, 32)
  143.     end
  144.   end
  145.   #--------------------------------------------------------------------------
  146.   # ● 拆分数组并显示内容
  147.   #--------------------------------------------------------------------------
  148.   def update
  149.     for i in 0...@item_max
  150.       x = 320 + 16
  151.       if self.index == i and $t == true
  152.         refresh
  153.         self.contents.font.color = system_color
  154.         self.contents.draw_text(x, 0, 100, 32, "任务名:")
  155.         self.contents.draw_text(x, 64, 100, 32, "难度:")
  156.         self.contents.draw_text(x, 112, 100, 32, "说明:")
  157.         if @gold[index] != 0
  158.           self.contents.draw_text(x, 176, 100, 32, "奖励金钱:")
  159.           y = 240
  160.         else
  161.           y = 208
  162.         end
  163.         if @items[index] != nil
  164.           self.contents.draw_text(x, y, 100, 32, "奖励物品:")
  165.         end
  166.         self.contents.font.color = normal_color
  167.         self.contents.draw_text(x, 32, 320 - 32, 32, @name2[index])
  168.         self.contents.draw_text(x, 144, 280, 32, @task_help[index])
  169.         if @gold[index] != 0
  170.           self.contents.draw_text(x, 208, 100, 32, @gold[index])
  171.         end
  172.         if @items[index] != nil and @item != nil
  173.           for j in [email protected]
  174.             @y = y + @item[j] * 32
  175.           end
  176.           for g in [email protected]
  177.             draw_item_name($data_items[@item[g]], x, @y)
  178.           end
  179.           for g in [email protected]
  180.             draw_item_name($data_weapons[@weapon[g]], x, @y + @item.size * 32)
  181.           end
  182.           for g in [email protected]
  183.             draw_item_name($data_armors[@armor[g]], x, @y + @item.size * 32 + @weapon.size * 32)
  184.           end
  185.         end
  186.         #Moon = difficulty[index] / DIFFICULTY::System
  187.         #Star = difficulty[index] % DIFFICULTY::System
  188.         #Sun = Moon / DIFFICULTY::System
  189.         #Moon %= DIFFICULTY::System
  190.         #Space = DIFFICULTY::Space
  191.         #rect = Rect.new(0, 0, DIFFICULTY::Size, DIFFICULTY::Size)
  192.         #while Sun>0
  193.         #  self.contents.blt(x + Space, 96, Sun, rect)
  194.         #  Space += DIFFICULTY::Size + DIFFICULTY::Space;Sun -= 1
  195.         #end
  196.         #while Moon>0
  197.         #  self.contents.blt(x + Space, 97, Moon, rect)
  198.         #  Space += DIFFICULTY::Size + DIFFICULTY::Space;Moon -= 1
  199.         #end
  200.         #while Star>0
  201.         #  self.contents.blt(x + Space, 98, Star, rect)
  202.         #  Space += DIFFICULTY::Size + DIFFICULTY::Space;Star -= 1
  203.         #end
  204.         case difficulty[index]
  205.         when 1
  206.           self.contents.draw_text(x + 60, 64, 280, 32, "★☆☆☆☆")
  207.         when 2
  208.           self.contents.draw_text(x + 60, 64, 280, 32, "★★☆☆☆")
  209.         when 3
  210.           self.contents.draw_text(x + 60, 64, 280, 32, "★★★☆☆")
  211.         when 4
  212.           self.contents.draw_text(x + 60, 64, 280, 32, "★★★★☆")
  213.         when 5
  214.           self.contents.draw_text(x + 60, 64, 280, 32, "★★★★★")
  215.         end
  216.       end
  217.     end
  218.   end
  219. end
复制代码

作者: 恐惧剑刃    时间: 2013-10-4 15:24
本帖最后由 恋′挂机 于 2013-10-4 15:31 编辑

可能报错因为什么什么for::NilClass
原因是你那个实例变量没有定义
  1. @array = ["1", "2", "3", "4"]
  2. @array_size = @array.size
  3. p @array_size
复制代码
再说简单点,就是给你  数组名称.size前边加个@,@数组名称.size
作者: 紫英晓狼1130    时间: 2013-10-4 15:59
需要定义实例变量,而且我觉得LZ的描绘星星的部分也有些麻烦…
作者: 芯☆淡茹水    时间: 2013-10-4 16:17
麻烦了就该简化一下。

改前:
  1. case difficulty[index]
  2. when 1
  3.   self.contents.draw_text(x + 60, 64, 280, 32, "★☆☆☆☆")
  4. when 2
  5.   self.contents.draw_text(x + 60, 64, 280, 32, "★★☆☆☆")
  6. when 3
  7.   self.contents.draw_text(x + 60, 64, 280, 32, "★★★☆☆")
  8. when 4
  9.   self.contents.draw_text(x + 60, 64, 280, 32, "★★★★☆")
  10. when 5
  11.   self.contents.draw_text(x + 60, 64, 280, 32, "★★★★★")
  12. end
复制代码
改后:
  1. text1 = "★" * difficulty[index]      
  2. text2 = "☆" * (5 - difficulty[index])
  3. text = text1 + text2
  4. self.contents.draw_text(x + 60, 64, 280, 32, txt)
复制代码

作者: 876加几    时间: 2013-10-4 16:17
紫英晓狼1130 发表于 2013-10-4 15:59
需要定义实例变量,而且我觉得LZ的描绘星星的部分也有些麻烦…

追问:
一定义实例变量语法就出错了。
作者: chd114    时间: 2013-10-4 16:18
1、尽量别用中文做变量单元模块之类的名称
2、需要调用前记得定义




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