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

Project1

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

[已经解决] undefined method "*" for nil:NilClass 什么意思?

[复制链接]

Lv1.梦旅人

梦石
0
星屑
55
在线时间
9 小时
注册时间
2009-3-18
帖子
12
跳转到指定楼层
1
发表于 2010-11-13 13:37:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 tyru12 于 2010-11-13 14:12 编辑
  1. rect.y += index * WLH
复制代码
提示这行:NoMthodError occurred
                 undefined method "*" for nil:NilClass  

这是什么意思?



源程序
  1. =begin

  2.   脚本:【完美输入法修正】
  3.   
  4.   功能:输入法。
  5.   
  6.   说明: 直接用Type_Field创建输入域即可进行输入,在此可根据Type_Field域对象
  7.   
  8.         的活动标记active来自定义刷新等,在Type_Field中需要自己处理特殊按键
  9.         
  10.         的处理方法。具体不明白之处请参考范例工程。

  11.   作者:灼眼的夏娜
  12.   
  13.   补充: 至于以前那个版本也许很多人都注意到那个烦人的问题了吧,按Enter和Tab那
  14.   
  15.         些会出现不爽的声音,这个版本解决了那个问题,并简化添加了Type_Field类
  16.         
  17.         来方便创建输入域。

  18. =end

  19. #==============================================================================
  20. # ■ RInput
  21. #------------------------------------------------------------------------------
  22. #  全键盘处理的模块。
  23. #==============================================================================
  24. module RInput
  25.   
  26.   #--------------------------------------------------------------------------
  27.   # ● 常量定义
  28.   #--------------------------------------------------------------------------
  29.   ENTER             = 0x0D
  30.   SPACE             = 0x20
  31.   UP                = 0x26
  32.   DOWN              = 0x28
  33.   LEFT              = 0x25
  34.   RIGHT             = 0x27
  35.   
  36.   LCTRL             = 0xA2
  37.   LALT              = 0xA4

  38.   #--------------------------------------------------------------------------
  39.   # ● 临时Hash
  40.   #--------------------------------------------------------------------------
  41.   @R_Key_Hash = {}
  42.   @R_Key_Repeat = {}
  43.   
  44.   #--------------------------------------------------------------------------
  45.   # ● 唯一API
  46.   #--------------------------------------------------------------------------
  47.   GetKeyState = Win32API.new("user32","GetAsyncKeyState",['I'],'I')

  48.   #--------------------------------------------------------------------------
  49.   # ● 单键处理
  50.   #--------------------------------------------------------------------------
  51.   def self.trigger?(rkey)
  52.     result = GetKeyState.call(rkey)
  53.     if @R_Key_Hash[rkey] == 1 and result != 0
  54.       return false
  55.     end
  56.     if result != 0
  57.       @R_Key_Hash[rkey] = 1
  58.       return true
  59.     else
  60.       @R_Key_Hash[rkey] = 0
  61.       return false
  62.     end
  63.   end

  64. end

  65. #==============================================================================
  66. # ■ EasyConv
  67. #------------------------------------------------------------------------------
  68. #  转码模块。
  69. #==============================================================================
  70. module EasyConv

  71.   #--------------------------------------------------------------------------
  72.   # ● 常量定义
  73.   #--------------------------------------------------------------------------
  74.   CP_ACP = 0
  75.   CP_UTF8 = 65001

  76.   #--------------------------------------------------------------------------
  77.   # ● 模块函数
  78.   #--------------------------------------------------------------------------
  79.   module_function
  80.   
  81.   #--------------------------------------------------------------------------
  82.   # ● 转码
  83.   #--------------------------------------------------------------------------
  84.   def s2u(text)

  85.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  86.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')

  87.     len = m2w.call(CP_ACP, 0, text, -1, nil, 0)
  88.     buf = "\0" * (len*2)
  89.     m2w.call(CP_ACP, 0, text, -1, buf, buf.size/2)

  90.     len = w2m.call(CP_UTF8, 0, buf, -1, nil, 0, nil, nil)
  91.     ret = "\0" * len
  92.     w2m.call(CP_UTF8, 0, buf, -1, ret, ret.size, nil, nil)
  93.     ret[-1] = ""   
  94.     return ret
  95.   end
  96.   #--------------------------------------------------------------------------
  97.   # ● 转码
  98.   #--------------------------------------------------------------------------
  99.   def u2s(text)
  100.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  101.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')

  102.     len = m2w.call(CP_UTF8, 0, text, -1, nil, 0)
  103.     buf = "\0" * (len*2)
  104.     m2w.call(CP_UTF8, 0, text, -1, buf, buf.size/2)

  105.     len = w2m.call(CP_ACP, 0, buf, -1, nil, 0, nil, nil)
  106.     ret = "\0" * len
  107.     w2m.call(CP_ACP, 0, buf, -1, ret, ret.size, nil, nil)
  108.     return ret
  109.   end
  110. end
  111. #==============================================================================
  112. # ■ TypeAPI
  113. #------------------------------------------------------------------------------
  114. #  输入法相关API模块。
  115. #==============================================================================
  116. module TypeAPI
  117.   #--------------------------------------------------------------------------
  118.   # ● API定义
  119.   #--------------------------------------------------------------------------
  120.   GPPS         = Win32API.new("kernel32","GetPrivateProfileString",'pppplp','l')
  121.   FindWindow   = Win32API.new("user32", "FindWindow", 'pp', 'i')
  122.   CreateWindow = Win32API.new("NetGame","CreatWnd",'l','l')
  123.   SetHK        = Win32API.new("NetGame","SetHK",'v','v')
  124.   GetText      = Win32API.new("NetGame","GetWndText",'l','p')
  125.   SetFocus     = Win32API.new("user32","SetFocus",'l','l')
  126.   IfBack       = Win32API.new("NetGame","If_Back",'v','i')
  127.   StartType    = Win32API.new("NetGame","StartType",'v','v')
  128.   EndType      = Win32API.new("NetGame","EndType",'v','v')
  129.   GetKeyInfos  = Win32API.new("NetGame","GetKeyInfo",'v','i')
  130.   
  131.   #--------------------------------------------------------------------------
  132.   # ● 模块函数
  133.   #--------------------------------------------------------------------------
  134.   module_function
  135.   
  136.   #--------------------------------------------------------------------------
  137.   # ● 获取参数
  138.   #--------------------------------------------------------------------------
  139.   def getParam
  140.     val = "\0" * 256
  141.     GPPS.call("Game","Title","",val,256,"./Game.ini")
  142.     val.delete!("\0")
  143.     return val
  144.   end
  145.   
  146.   #--------------------------------------------------------------------------
  147.   # ● 获取窗口
  148.   #--------------------------------------------------------------------------
  149.   def findWindow
  150.     return FindWindow.call("RGSS Player",self.getParam)
  151.   end
  152.   
  153.   #--------------------------------------------------------------------------
  154.   # ● 创建窗口
  155.   #--------------------------------------------------------------------------
  156.   def createWindow(hwnd)
  157.     return CreateWindow.call(hwnd)
  158.   end
  159.   
  160.   #--------------------------------------------------------------------------
  161.   # ● 设置HK
  162.   #--------------------------------------------------------------------------
  163.   def setHK
  164.     SetHK.call
  165.   end
  166.   
  167.   #--------------------------------------------------------------------------
  168.   # ● 获取文字
  169.   #--------------------------------------------------------------------------
  170.   def getText
  171.     return EasyConv.s2u(GetText.call(@subhwnd))
  172.   end
  173.   
  174.   #--------------------------------------------------------------------------
  175.   # ● 设置焦点
  176.   #--------------------------------------------------------------------------
  177.   def setFocus
  178.     SetFocus.call(@subhwnd)
  179.   end
  180.   
  181.   #--------------------------------------------------------------------------
  182.   # ● 转换焦点
  183.   #--------------------------------------------------------------------------
  184.   def lostFocus
  185.     SetFocus.call(@hwnd)
  186.   end
  187.   
  188.   #--------------------------------------------------------------------------
  189.   # ● 退格判定
  190.   #--------------------------------------------------------------------------
  191.   def ifBack
  192.     return IfBack.call
  193.   end
  194.   
  195.   #--------------------------------------------------------------------------
  196.   # ● 开始输入
  197.   #--------------------------------------------------------------------------
  198.   def startType
  199.     StartType.call
  200.   end
  201.   
  202.   #--------------------------------------------------------------------------
  203.   # ● 结束输入
  204.   #--------------------------------------------------------------------------
  205.   def endType
  206.     EndType.call
  207.   end
  208.   
  209.   #--------------------------------------------------------------------------
  210.   # ● 输入中特殊按键处理
  211.   #--------------------------------------------------------------------------
  212.   def getKeyInfos
  213.     return GetKeyInfos.call
  214.   end
  215.   
  216.   #--------------------------------------------------------------------------
  217.   # ● 获取句柄
  218.   #--------------------------------------------------------------------------
  219.   @hwnd    = self.findWindow
  220.   
  221.   @subhwnd = self.createWindow(@hwnd)

  222.   #--------------------------------------------------------------------------
  223.   # ● 设置HK应用
  224.   #--------------------------------------------------------------------------
  225.   self.setHK
  226.   
  227. end

  228. #==============================================================================
  229. # ■ Type_Field
  230. #------------------------------------------------------------------------------
  231. #  处理输入域的类。
  232. #==============================================================================
  233. class Type_Field
  234.   
  235.   #--------------------------------------------------------------------------
  236.   # ● 定义实例变量
  237.   #--------------------------------------------------------------------------
  238.   attr(:active)
  239.   
  240.   #--------------------------------------------------------------------------
  241.   # ● 初始化
  242.   #--------------------------------------------------------------------------
  243.   def initialize(v,default_text = "",default_careth = nil,default_fonts = 16,    default_fontc = Color.new(255,255,255,255))
  244.     # active
  245.     @active = true
  246.     # 视口
  247.     rect = v.rect
  248.     @v = Viewport.new(rect.x,rect.y,rect.width,rect.height)
  249.     @beijing= Sprite.new(@v)
  250.     @beijing.bitmap = Cache.system("1081-1")#Cache.("Title")picture icon
  251.     @w = rect.width
  252.     @h = rect.height
  253.     # 属性
  254.     @caret_h = default_careth.nil? ? @h : [@h,default_careth].min
  255.     @caret_y = rect.y + (@h - @caret_h) / 2
  256.     @font_size = [default_fonts,@h].min
  257.     # 描绘contents
  258.     @cts = Sprite.new(@v)
  259.     @cts.bitmap = Bitmap.new(@w - 3,@h)
  260.     @cts.bitmap.font.size = @font_size
  261.     @cts.bitmap.font.color = default_fontc
  262.     # 辅助属性
  263.     @bk_count = 0
  264.     @text = default_text.scan(/./)
  265.     @max_width = @w - 3
  266.     @caret_pos = @text.size
  267.     @save_pos = @caret_pos
  268.     # 光标Caret
  269.     @v1 = Viewport.new(rect.x,@caret_y,@w + 3,@caret_h)
  270.     @caret_sp = Sprite.new(@v1)
  271.     @caret_bitmap = Bitmap.new(3,@caret_h)
  272.     @caret_bitmap.fill_rect(3,0,1,@caret_h,Color.new(0,0,0,180))
  273.     @caret_bitmap.fill_rect(2,0,1,@caret_h,Color.new(0,0,0))
  274.     @caret_bitmap.fill_rect(2,0,1,@caret_h,Color.new(255,255,255))
  275.     @caret_sp.bitmap = @caret_bitmap
  276.     @caret_sp.x = self.get_width(@text[0,@caret_pos].to_s)
  277.     @caret_sp.y = 0
  278.     @caret_sp.visible = false
  279.     @caret_flash_count = 0
  280.     # 刷新
  281.     refresh
  282.     # 设置焦点
  283.     TypeAPI.setFocus
  284.     # 开始输入
  285.     TypeAPI.startType
  286.   end
  287.   
  288.   #--------------------------------------------------------------------------
  289.   # ● 设置活动标记
  290.   #--------------------------------------------------------------------------
  291.   def active=(value)
  292.     if value != true and value != false
  293.       return
  294.     end
  295.     @active = value
  296.     @caret_sp.visible = @active
  297.   end
  298.   
  299.   #--------------------------------------------------------------------------
  300.   # ● 释放
  301.   #--------------------------------------------------------------------------
  302.   def dispose
  303.     @caret_bitmap.dispose
  304.     @caret_sp.bitmap.dispose
  305.     @caret_sp.dispose
  306.     @cts.bitmap.dispose
  307.     @cts.dispose
  308.     @v.dispose
  309.     @v1.dispose
  310.   end

  311.   #--------------------------------------------------------------------------
  312.   # ● 刷新
  313.   #--------------------------------------------------------------------------
  314.   def refresh
  315.     @cts.bitmap.clear
  316.     @cts.bitmap.draw_text(0,0,@w,@h,@text.to_s)
  317.   end

  318.   #--------------------------------------------------------------------------
  319.   # ● 获取文字
  320.   #--------------------------------------------------------------------------
  321.   def get_text
  322.     return @text.to_s
  323.   end

  324.   #--------------------------------------------------------------------------
  325.   # ● 取得字符宽度
  326.   #--------------------------------------------------------------------------
  327.   def get_width(str)
  328.     return @cts.bitmap.text_size(str).width
  329.   end
  330.   
  331.   #--------------------------------------------------------------------------
  332.   # ● 更新
  333.   #--------------------------------------------------------------------------
  334.   def update
  335.     # 非激活状态则返回
  336.     unless @active
  337.       return
  338.     end
  339.     # 获取按键信息
  340.     key_info = TypeAPI.getKeyInfos
  341.     case key_info
  342.     when 0x09 # Tab
  343.       # 按下 Tab 键的情况自己定义怎么处理
  344.       return
  345.     when 0x0d # Enter
  346.       text = get_text                              #取得文字内容
  347.       $scene.type_window.add_instant_text(text)    #文字窗口添加文字
  348.       @text = []                                   #文字清空
  349.       refresh
  350.       return
  351.     when 0x1B # Esc
  352.       # 按下 Esc 键的情况自己定义怎么处理
  353.       return
  354.     end
  355.     self.update_text
  356.     self.update_lrb
  357.     self.update_back
  358.     self.update_caret
  359.   end

  360.   #--------------------------------------------------------------------------
  361.   # ● 更新文字
  362.   #--------------------------------------------------------------------------
  363.   def update_text
  364.     # 文字刷新
  365.     TypeAPI.setFocus
  366.     text = TypeAPI.getText
  367.     if text != ""
  368.       for char in text.scan(/./)
  369.         if self.get_width(@text.to_s + char) <= @max_width
  370.           @text[@caret_pos,0] = char
  371.           @caret_pos += 1
  372.         else
  373.           break
  374.         end
  375.       end
  376.       refresh
  377.     end
  378.   end
  379.   
  380.   #--------------------------------------------------------------------------
  381.   # ● 更新左右按键
  382.   #--------------------------------------------------------------------------
  383.   def update_lrb
  384.     if RInput.trigger?(RInput::LEFT) and @caret_pos > 0
  385.       @caret_pos -= 1
  386.       return
  387.     end
  388.     if RInput.trigger?(RInput::RIGHT) and @caret_pos < @text.size
  389.       @caret_pos += 1
  390.       return
  391.     end
  392.   end
  393.   
  394.   #--------------------------------------------------------------------------
  395.   # ● 更新退格
  396.   #--------------------------------------------------------------------------
  397.   def update_back
  398.     # 前退格处理
  399.     @bk_count += TypeAPI.ifBack
  400.     if @bk_count > 0
  401.       @bk_count -= 1
  402.       if @caret_pos > 0
  403.         @text.delete_at(@caret_pos - 1);@caret_pos -= 1;refresh
  404.       end
  405.     end
  406.   end
  407.   
  408.   #--------------------------------------------------------------------------
  409.   # ● 更新光标闪烁
  410.   #--------------------------------------------------------------------------
  411.   def update_caret
  412.     # 闪烁计时
  413.     @caret_flash_count += 1
  414.     if @caret_flash_count == 20
  415.       @caret_sp.visible = !@caret_sp.visible
  416.       @caret_flash_count = 0
  417.     end
  418.     # Caret位置刷新
  419.     if @save_pos != @caret_pos
  420.       @save_pos = @caret_pos
  421.       @caret_sp.x = self.get_width(@text[0,@save_pos].to_s)
  422.     end
  423.   end
  424.   
  425. end
  426.   
  427. #==============================================================================
  428. # ■ Window_TypeMessage  在地图画面显示文字的窗口。
  429. #------------------------------------------------------------------------------
  430. #==============================================================================

  431. class Window_TypeMessage < Window_Message
  432.   #--------------------------------------------------------------------------
  433.   # ● 初始化对象
  434.   #--------------------------------------------------------------------------
  435.   def initialize
  436.     super
  437.     self.openness = 255
  438.     self.opacity = 0                                   #背景透明度为0
  439.     self.contents.font.size = 18                       #字号18
  440.     self.x = 20                                       
  441.     @lines = []                                       
  442.     refresh
  443.     #控制开关,如果开关一打开则不可见。也可以改成其他开关或者干脆去掉
  444.     self.visible = false if $game_switches[1] == true
  445.   end
  446.   #--------------------------------------------------------------------------
  447.   # ● 释放
  448.   #--------------------------------------------------------------------------
  449.   def dispose
  450.     super
  451.   end
  452.   #--------------------------------------------------------------------------
  453.   # ● 刷新画面
  454.   #--------------------------------------------------------------------------
  455.   def update
  456.     return if $game_switches[1] == true
  457.     super
  458.     refresh if Graphics.frame_count % 8 == 0
  459.   end
  460.   #--------------------------------------------------------------------------
  461.   # ● 打开窗口 (无效化)
  462.   #--------------------------------------------------------------------------
  463.   def open
  464.   end
  465.   #--------------------------------------------------------------------------
  466.   # ● 关闭窗口 (无效化)
  467.   #--------------------------------------------------------------------------
  468.   def close
  469.   end
  470.   #--------------------------------------------------------------------------
  471.   # ● 设置窗口背景与位置 (无效化)
  472.   #--------------------------------------------------------------------------
  473.   def reset_window
  474.   end
  475.   #--------------------------------------------------------------------------
  476.   # ● 清除
  477.   #--------------------------------------------------------------------------
  478.   def clear
  479.     refresh
  480.   end
  481.   #--------------------------------------------------------------------------
  482.   # ● 获取行数
  483.   #--------------------------------------------------------------------------
  484.   def line_number
  485.     return @lines.size
  486.   end
  487.   #--------------------------------------------------------------------------
  488.   # ● 返回一行
  489.   #--------------------------------------------------------------------------
  490.   def back_one
  491.     @lines.pop
  492.     refresh
  493.   end
  494.   #--------------------------------------------------------------------------
  495.   # ● 返回至指定行
  496.   #     line_number : 行编号
  497.   #--------------------------------------------------------------------------
  498.   def back_to(line_number)
  499.     while @lines.size > line_number
  500.       @lines.pop
  501.     end
  502.     refresh
  503.   end
  504.   #--------------------------------------------------------------------------
  505.   # ● 添加文章
  506.   #     text : 添加的文章
  507.   #--------------------------------------------------------------------------
  508.   def add_instant_text(text)
  509.     @lines.push([text, Graphics.frame_count])
  510.     @lines.delete_at(0) if @lines.size > 4
  511.     refresh
  512.   end
  513.   #--------------------------------------------------------------------------
  514.   # ● 替换文章
  515.   #     text : 要替换的文章
  516.   #    将最下行替换为别的文章。
  517.   #--------------------------------------------------------------------------
  518.   def replace_instant_text(text)
  519.     @lines.pop
  520.     @lines.push([text, Graphics.frame_count])
  521.     refresh
  522.   end
  523.   #--------------------------------------------------------------------------
  524.   # ● 获取最下行的文章
  525.   #--------------------------------------------------------------------------
  526.   def last_instant_text
  527.     return @lines[-1]
  528.   end
  529.   #--------------------------------------------------------------------------
  530.   # ● 刷新
  531.   #--------------------------------------------------------------------------
  532.   def refresh
  533.     return if $game_switches[1] == true
  534.     self.contents.clear
  535.     for i in
  536.       draw_line(i)
  537.     end
  538.     self.y = 360 - (@lines.size ) * WLH           #每画一行窗口就移动一次位置
  539.   end
  540.   #--------------------------------------------------------------------------
  541.   # ● 描绘行
  542.   #     index : 行编号
  543.   #--------------------------------------------------------------------------
  544.   def draw_line(index)
  545.     rect = Rect.new(0, 0, 0, 0)
  546.     rect.x += 4
  547.     rect.y += index*WLH
  548.     rect.width = contents.width - 8
  549.     rect.height = WLH
  550.     self.contents.clear_rect(rect)
  551.     self.contents.font.color = normal_color
  552.     self.contents.font.color.alpha = 700 - (Graphics.frame_count - @lines[index][1]) * 4                                       #透明度变化,即逐渐消失
  553.     self.contents.draw_text(rect, @lines[index][0])
  554.   end
  555. end
  556. #对Scene_Map进行的修改,增加了type_window这个窗口和一个接口。
  557. class Scene_Map < Scene_Base
  558.   attr_accessor :type_window
  559.   def start
  560.     super
  561.     $game_map.refresh
  562.     @spriteset = Spriteset_Map.new
  563.     @message_window = Window_Message.new
  564.     @type_window = Window_TypeMessage.new
  565.   end
  566.   def terminate
  567.     super
  568.     if $scene.is_a?(Scene_Battle)     # 正在切换战斗画面的情况下
  569.       @spriteset.dispose_characters   # 为了生成背景隐藏角色
  570.     end
  571.     snapshot_for_background
  572.     @spriteset.dispose
  573.     @message_window.dispose
  574.     @type_window.dispose
  575.     if $scene.is_a?(Scene_Battle)     # 正在切换战斗画面的情况下
  576.       perform_battle_transition       # 执行战斗前变换
  577.     end
  578.   end
  579.   def update
  580.     super
  581.     $game_map.interpreter.update      # 更新解释器
  582.     $game_map.update                  # 更新滴入
  583.     $game_player.update               # 更新玩家
  584.     $game_system.update               # 更新计时器
  585.     @spriteset.update                 # 更新活动块元件
  586.     @message_window.update            # 更新消息窗口
  587.     @type_window.update
  588.     unless $game_message.visible      # 正在显示消息以外的情况
  589.       update_transfer_player
  590.       update_encounter
  591.       update_call_menu
  592.       update_call_debug
  593.       update_scene_change
  594.     end
  595.   end
  596. end
复制代码

Lv2.观梦者

梦石
0
星屑
270
在线时间
1373 小时
注册时间
2005-10-16
帖子
5113

贵宾

2
发表于 2010-11-13 13:38:53 | 只看该作者
nil 值无法进行 * 乘法运算。
换言之,你的index或者是WLH是nil了

点评

应该是index nil了,WLH是*方法的参数。然后,星叔好呀~  发表于 2010-11-14 19:15
哦,那么应该怎么改呢? 这句在附件的569行  发表于 2010-11-13 13:46
我只个搬答案的
叔叔我已经当爹了~
婚后闪人了……
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
270
在线时间
1373 小时
注册时间
2005-10-16
帖子
5113

贵宾

3
发表于 2010-11-14 19:12:42 | 只看该作者
请注意557行。。。
我只个搬答案的
叔叔我已经当爹了~
婚后闪人了……
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-5-23 09:40

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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