Project1

标题: 为何像“+”这样的RGSS3内部语言也会未定义? [打印本页]

作者: 饿啊    时间: 2016-4-15 13:28
标题: 为何像“+”这样的RGSS3内部语言也会未定义?
本帖最后由 饿啊 于 2016-4-15 13:30 编辑

本人多日来的疑问,在学会一点点RGSS3后竟然彻底懵逼了——这种RGSS3内部语言会有这种错误...谁能答疑一下?
(如下脚本和错误来自于VA图书馆的CP战斗系统,我原先也问过)
RUBY 代码复制
  1. #RTAB战斗系统 For RPG Maker VX Ace 编写者:SLICK
  2.  
  3. #encoding:utf-8
  4. #==============================================================================
  5. # ■ Scene_Battle
  6. #------------------------------------------------------------------------------
  7. #  战斗画面
  8. #==============================================================================
  9.  
  10. class Scene_Battle < Scene_Base
  11.   attr_accessor :cp_quota
  12.   attr_accessor :rtab_wait
  13.   attr_accessor :active_members
  14.   #--------------------------------------------------------------------------
  15.   # ● 开始处理
  16.   #--------------------------------------------------------------------------
  17.   def start
  18.     super
  19.     @cp_quota = 0
  20.     create_spriteset
  21.     create_all_windows
  22.     BattleManager.method_wait_for_message = method(:wait_for_message)
  23.   end
  24.   #--------------------------------------------------------------------------
  25.   # ● 战斗开始
  26.   #--------------------------------------------------------------------------
  27.   def battle_start
  28.     @rtab_wait = true
  29.     @active_members = []
  30.     BattleManager.battle_start
  31.     set_cp_quota
  32.     process_event
  33.     #start_party_command_selection
  34.   end
  35.   #--------------------------------------------------------------------------
  36.   # ● 获取敌我双方人物的平均行动速度并决定CP槽的Quota值
  37.   #--------------------------------------------------------------------------
  38.   def set_cp_quota
  39.     sss = 0.0
  40.     for iii in $game_party.members
  41.       sss += iii.param(6)
  42.     end
  43.     sss /= $game_party.members.size
  44.     @cp_quota = sss * 200
  45.     @spriteset.cp_quota = @cp_quota
  46.     for iii in $game_party.members + $game_troop.members
  47.       iii.cp_quota = @cp_quota
  48.     end
  49.   end
  50.   #--------------------------------------------------------------------------
  51.   # ● 进行下一个指令输入
  52.   #--------------------------------------------------------------------------
  53.   def next_command
  54.     if BattleManager.next_command
  55.       start_actor_command_selection
  56.     else
  57.       turn_start(@active_members)
  58.     end
  59.   end
  60.   #--------------------------------------------------------------------------
  61.   # ● 返回上一个指令输入
  62.   #--------------------------------------------------------------------------
  63.   def prior_command
  64.     if BattleManager.prior_command
  65.       start_actor_command_selection
  66.     else
  67.       start_party_command_selection
  68.     end
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # ● 更新画面
  72.   #--------------------------------------------------------------------------
  73.   def update
  74.     super
  75.     if BattleManager.in_turn?
  76.       process_event
  77.       process_action
  78.     else
  79.       if @rtab_wait
  80.         for iii in $game_party.members + $game_troop.members
  81.           iii.in_turn = false
  82.           iii.rtab_cp += iii.param(6)
  83.           if iii.rtab_cp >= iii.cp_quota
  84.             @active_members.push(iii)
  85.             iii.in_turn = true
  86.           end
  87.         end
  88.         if @active_members.size > 0
  89.           @rtab_wait = false
  90.           start_party_command_selection
  91.         end
  92.       end
  93.     end
  94.     BattleManager.judge_win_loss
  95.   end
  96.   #--------------------------------------------------------------------------
  97.   # ● 回合开始
  98.   #--------------------------------------------------------------------------
  99.   def turn_start(members)
  100.     @party_command_window.close
  101.     @actor_command_window.close
  102.     @status_window.unselect
  103.     @subject = nil
  104.     BattleManager.turn_start(members)
  105.     @log_window.wait
  106.     @log_window.clear
  107.   end
  108.   #--------------------------------------------------------------------------
  109.   # ● 回合结束
  110.   #--------------------------------------------------------------------------
  111.   def turn_end
  112.     @active_members.each do |battler|
  113.       battler.on_turn_end
  114.       refresh_status
  115.       @log_window.display_auto_affected_status(battler)
  116.       @log_window.wait_and_clear
  117.     end
  118.     BattleManager.turn_end
  119.     process_event
  120.     @rtab_wait = true
  121.     @active_members = []
  122.   end
  123.   #--------------------------------------------------------------------------
  124.   # ● 处理战斗行动
  125.   #--------------------------------------------------------------------------
  126.   def process_action
  127.     return if scene_changing?
  128.     if !@subject || !@subject.current_action
  129.       @subject = BattleManager.next_subject
  130.     end
  131.     return turn_end unless @subject
  132.     if @subject.current_action
  133.       @subject.current_action.prepare
  134.       if @subject.current_action.valid?
  135.         @status_window.open
  136.         execute_action
  137.       end
  138.       @subject.remove_current_action
  139.     end
  140.     process_action_end unless @subject.current_action
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # ● 开始队伍指令的选择
  144.   #--------------------------------------------------------------------------
  145.   def start_party_command_selection
  146.     unless scene_changing?
  147.       refresh_status
  148.       @status_window.unselect
  149.       @status_window.open
  150.       if BattleManager.input_start
  151.         BattleManager.clear_actor
  152.         BattleManager.next_command
  153.         @status_window.select(BattleManager.actor.index)
  154.         @actor_command_window.setup(BattleManager.actor)
  155.       else
  156.         @party_command_window.deactivate
  157.         @actor_command_window.close
  158.         turn_start(@active_members)
  159.       end
  160.     end
  161.   end
  162.   #--------------------------------------------------------------------------
  163.   # ● 生成角色指令窗口
  164.   #--------------------------------------------------------------------------
  165.   def create_actor_command_window
  166.     @actor_command_window = Window_ActorCommand.new
  167.     @actor_command_window.viewport = @info_viewport
  168.     @actor_command_window.set_handler(:attack, method(:command_attack))
  169.     @actor_command_window.set_handler(:skill,  method(:command_skill))
  170.     @actor_command_window.set_handler(:guard,  method(:command_guard))
  171.     @actor_command_window.set_handler(:item,   method(:command_item))
  172.     @actor_command_window.set_handler(:cancel, method(:prior_command))
  173.     @actor_command_window.set_handler(:escape, method(:command_escape))
  174.     @actor_command_window.x = Graphics.width
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   # ● 指令“撤退”
  178.   #--------------------------------------------------------------------------
  179.   def command_escape
  180.     unless BattleManager.process_escape
  181.       turn_start(@active_members)
  182.       for iii in @active_members
  183.         iii.rtab_cp = 0
  184.       end
  185.     end
  186.   end
  187. end
  188.  
  189. #encoding:utf-8
  190. #==============================================================================
  191. # ■ Window_ActorCommand
  192. #------------------------------------------------------------------------------
  193. #  战斗画面中,选择角色行动的窗口。
  194. #==============================================================================
  195.  
  196. class Window_ActorCommand < Window_Command
  197.   #--------------------------------------------------------------------------
  198.   # ● 生成指令列表
  199.   #--------------------------------------------------------------------------
  200.   def make_command_list
  201.     return unless @actor
  202.     add_attack_command
  203.     add_skill_commands
  204.     add_guard_command
  205.     add_item_command
  206.     add_escape_command
  207.   end
  208.   #--------------------------------------------------------------------------
  209.   # ● 添加撤退指令
  210.   #--------------------------------------------------------------------------
  211.   def add_escape_command
  212.     add_command(Vocab::escape, :escape, BattleManager.can_escape?)
  213.   end
  214. end
  215.  
  216. #encoding:utf-8
  217. #==============================================================================
  218. # ■ BattleManager
  219. #------------------------------------------------------------------------------
  220. #  战斗过程的管理器。
  221. #==============================================================================
  222.  
  223. module BattleManager
  224.   #--------------------------------------------------------------------------
  225.   # ● 战斗开始
  226.   #--------------------------------------------------------------------------
  227.   def self.battle_start
  228.     $game_system.battle_count += 1
  229.     $game_party.on_battle_start
  230.     $game_troop.on_battle_start
  231.     $game_troop.enemy_names.each do |name|
  232.       $game_message.add(sprintf(Vocab::Emerge, name))
  233.     end
  234.     if @preemptive
  235.       $game_message.add(sprintf(Vocab::Preemptive, $game_party.name))
  236.     elsif @surprise
  237.       $game_message.add(sprintf(Vocab::Surprise, $game_party.name))
  238.     end
  239.     wait_for_message
  240.   end
  241.   #--------------------------------------------------------------------------
  242.   # ● 遇敌时的处理
  243.   #--------------------------------------------------------------------------
  244.   def self.on_encounter
  245.     @preemptive = false
  246.     @surprise = false
  247.   end
  248.   #--------------------------------------------------------------------------
  249.   # ● 回合开始
  250.   #--------------------------------------------------------------------------
  251.   def self.turn_start(members)
  252.     @phase = :turn
  253.     clear_actor
  254.     $game_troop.increase_turn(members)
  255.     make_action_orders(members)
  256.   end
  257.   #--------------------------------------------------------------------------
  258.   # ● 生成行动顺序
  259.   #--------------------------------------------------------------------------
  260.   def self.make_action_orders(members)
  261.     @action_battlers = []
  262.     for iii in $game_party.members
  263.       if members.include?(iii)
  264.         @action_battlers.push(iii) unless @surprise
  265.       end
  266.     end
  267.     for iii in $game_troop.members
  268.       if members.include?(iii)
  269.         @action_battlers.push(iii) unless @preemptive
  270.       end
  271.     end
  272.     @action_battlers.each {|battler| battler.make_speed }
  273.     @action_battlers.sort! {|a,b| b.speed - a.speed }
  274.   end
  275. end
  276.  
  277. #encoding:utf-8
  278. #==============================================================================
  279. # ■ Game_Battler
  280. #------------------------------------------------------------------------------
  281. #  处理战斗者的类。Game_Actor 和 Game_Enemy 类的父类。
  282. #==============================================================================
  283.  
  284. class Game_Battler < Game_BattlerBase
  285.   attr_accessor :turn_count
  286.   attr_accessor :rtab_cp
  287.   attr_accessor :cp_quota
  288.   attr_accessor :in_turn
  289.   #--------------------------------------------------------------------------
  290.   # ● 初始化对象
  291.   #--------------------------------------------------------------------------
  292.   alias neoinitialize initialize
  293.   def initialize
  294.     @turn_count = 0
  295.     @rtab_cp = 0
  296.     @cp_quota = 100
  297.     @in_turn = false
  298.     neoinitialize
  299.   end
  300.   #--------------------------------------------------------------------------
  301.   # ● 战斗开始处理
  302.   #--------------------------------------------------------------------------
  303.   def on_battle_start
  304.     @turn_count = 0
  305.     @rtab_cp = 0
  306.     @cp_quota = 100
  307.     @in_turn = false
  308.     init_tp unless preserve_tp?
  309.   end
  310.   #--------------------------------------------------------------------------
  311.   # ● 处理伤害
  312.   #    调用前需要设置好
  313.   #    @result.hp_damage   @result.mp_damage
  314.   #    @result.hp_drain    @result.mp_drain
  315.   #--------------------------------------------------------------------------
  316.   def execute_damage(user)
  317.     on_damage(@result.hp_damage) if @result.hp_damage > 0
  318.     self.hp -= @result.hp_damage
  319.     self.mp -= @result.mp_damage
  320.     user.hp += @result.hp_drain
  321.     user.mp += @result.mp_drain
  322.   end
  323.   #--------------------------------------------------------------------------
  324.   # ● 判定是否可以输入指令
  325.   #--------------------------------------------------------------------------
  326.   def inputable?
  327.     normal? && !auto_battle? && @in_turn
  328.   end
  329.   #--------------------------------------------------------------------------
  330.   # ● 回合结束处理
  331.   #--------------------------------------------------------------------------
  332.   def on_turn_end
  333.     @result.clear
  334.     regenerate_all
  335.     update_state_turns
  336.     update_buff_turns
  337.     remove_states_auto(2)
  338.     # 该角色的回合计数加1并清空CP
  339.     @turn_count += 1
  340.     @rtab_cp = 0
  341.   end
  342. end
  343.  
  344. #encoding:utf-8
  345. #==============================================================================
  346. # ■ Game_Troop
  347. #------------------------------------------------------------------------------
  348. #  管理敌群和战斗相关资料的类,也可执行如战斗事件管理之类的功能。
  349. #   本类的实例请参考 $game_troop 。
  350. #==============================================================================
  351.  
  352. class Game_Troop < Game_Unit
  353.   #--------------------------------------------------------------------------
  354.   # ● 增加回合
  355.   #--------------------------------------------------------------------------
  356.   def increase_turn(members)
  357.     troop.pages.each {|page| @event_flags[page] = false if page.span == 1 }
  358.     turnmax = []
  359.     for iii in members
  360.       turnmax.push(iii.turn_count)
  361.     end
  362.     @turn_count = turnmax.max
  363.   end
  364. end
  365.  
  366. #encoding:utf-8
  367. #==============================================================================
  368. # ■ Spriteset_Battle
  369. #------------------------------------------------------------------------------
  370. #  处理战斗画面的精灵的类。本类在 Scene_Battle 类的内部使用。
  371. #==============================================================================
  372.  
  373. class Spriteset_Battle
  374.   attr_accessor :icons
  375.   attr_accessor :cp_quota
  376.   #--------------------------------------------------------------------------
  377.   # ● 初始化对象
  378.   #--------------------------------------------------------------------------
  379.   def initialize
  380.     @cp_quota = 100
  381.     create_viewports
  382.     create_battleback1
  383.     create_battleback2
  384.     create_enemies
  385.     create_actors
  386.     create_pictures
  387.     create_timer
  388.     create_rtabcp_gauge
  389.     update
  390.   end
  391.   #--------------------------------------------------------------------------
  392.   # ● 生成战斗CP条
  393.   #--------------------------------------------------------------------------
  394.   def create_rtabcp_gauge
  395.     @cpgauge_back = Sprite.new(@viewport3)
  396.     @cpgauge_back.bitmap = Bitmap.new(400, 8)
  397.     @cpgauge_back.bitmap.gradient_fill_rect(0, 0, 400, 16, Color.new(0, 255, 0), Color.new(255, 0, 0))
  398.     @cpgauge_back.x = 16
  399.     @cpgauge_back.y = 16
  400.     # 为战斗中的各个角色生成其图标
  401.     @icons = {}
  402.     for iii in $game_party.members
  403.       @icons[iii] = Sprite.new(@viewport3)
  404.       @icons[iii].bitmap = Bitmap.new("Graphics/System/bicon1.png")
  405.       @icons[iii].ox = 12
  406.       @icons[iii].oy = 12
  407.       @icons[iii].x = 16
  408.       @icons[iii].y = 20
  409.     end
  410.     for iii in $game_troop.members
  411.       @icons[iii] = Sprite.new(@viewport3)
  412.       @icons[iii].bitmap = Bitmap.new("Graphics/System/bicon2.png")
  413.       @icons[iii].ox = 12
  414.       @icons[iii].oy = 12
  415.       @icons[iii].x = 16
  416.       @icons[iii].y = 20
  417.     end
  418.   end
  419.   #--------------------------------------------------------------------------
  420.   # ● 释放
  421.   #--------------------------------------------------------------------------
  422.   def dispose
  423.     dispose_battleback1
  424.     dispose_battleback2
  425.     dispose_enemies
  426.     dispose_actors
  427.     dispose_pictures
  428.     dispose_timer
  429.     dispose_viewports
  430.     dispose_rtabcp_gauge
  431.   end
  432.   #--------------------------------------------------------------------------
  433.   # ● 消灭战斗CP条
  434.   #--------------------------------------------------------------------------
  435.   def dispose_rtabcp_gauge
  436.     @cpgauge_back.bitmap.dispose
  437.     @cpgauge_back.dispose
  438.     for iii in $game_party.members
  439.       @icons[iii].bitmap.dispose
  440.       @icons[iii].dispose
  441.     end
  442.     for iii in $game_troop.members
  443.       @icons[iii].bitmap.dispose
  444.       @icons[iii].dispose
  445.     end
  446.   end
  447.   #--------------------------------------------------------------------------
  448.   # ● 更新画面
  449.   #--------------------------------------------------------------------------
  450.   def update
  451.     update_battleback1
  452.     update_battleback2
  453.     update_enemies
  454.     update_actors
  455.     update_cp
  456.     update_pictures
  457.     update_timer
  458.     update_viewports
  459.   end
  460.   #--------------------------------------------------------------------------
  461.   # ● 更新CP条
  462.   #--------------------------------------------------------------------------
  463.   def update_cp
  464.     for iii in $game_party.members + $game_troop.members
  465.       @icons[iii].x = 16 + 400 * iii.rtab_cp / @cp_quota
  466.       @icons[iii].visible = iii.alive?
  467.     end
  468.   end
  469. end


(@cinderelmini                  不要误会成我没有解决啊...)
此处为我原先的问题

204222l8bshhow8p6yhhol.png (5.97 KB, 下载次数: 15)

204222l8bshhow8p6yhhol.png

作者: 喵呜喵5    时间: 2016-4-15 14:05
本帖最后由 喵呜喵5 于 2016-4-15 14:06 编辑

道理其实很简单:
你问电脑 一张白纸 + 数字1 等于多少,电脑说不知道,你可以理解
为什么你问电脑 一个不是数字的东西 + 一个数字 等于多少,电脑说不知道,你却很奇怪呢?
作者: cinderelmini    时间: 2016-4-15 15:05
本帖最后由 cinderelmini 于 2016-4-15 15:08 编辑

不是方法没定义,然后运算方法是定义给特定对象(数字,数组,字符什么的)的,
然后弹这个未定义运算符号方法的问题就是前面要运算的变量并不是能运算的对象,
所以会有这种问题……

举个栗子的话……
乃可以跟一个人说人类的语言,然后对面能懂,
但是也可以跟一只喵说人类的语言,然而它不懂……
人类的语言是已经定义了的方法,但是对象不一样。
作者: 丿梁丶小柒    时间: 2016-4-15 18:58
在给一个很容易懂的解释。

虽然是内定好的class但是也是可以报错的。

提个简单的例子把,比如调用这个类的时候需要传入数字的参数进去才能正常运行。

但是你给他传入了非数字类型进去肯定会报错的啊。
作者: shitake    时间: 2016-4-15 20:41
本帖最后由 shitake 于 2016-4-15 22:46 编辑


@taroxd

就我个人的理解来看,应该是  .& 或者直接写 &。而 &. 应该是个不怎么正确的写法。
. 在ruby里通常表示方法的调用,写作.& 是和 .+ 这样差不多的写法【ruby里实际上是不存在运算符的,所有的运算符本质上都应该是以特殊符号为方法名的拟态方法】
那么 &. 这样的写法怎么来的呢,举个例子:
  1. a = []
  2. a&.each{ |i| p i }
复制代码
而这里实际上应该是先 .& 然后在 .each,这个 . 应该是和其后边的方法连在一起的 。
不过这也不一定说明我是对的,应为在cruby 2.3.0 的环境下,我尝试写 a.&.each{ |i| p i } 会报错。囧
另外,通过调用 methods 方法,发现在常用的类里面,只有 String、Numerc、Array、NilClass、TrueClass、FalseClass、MatchData 这几个类的实例里有 .& 这个方法。

恩,我去查了下最先看到这个的出处
然后发现除了这里,其他提到孤寂运算符的地方都是写做 &. 貌似是这篇翻译的问题(其他语言的版本里也是 &. 囧)
另外看这篇 Matz 开始也是考虑过 .& 这样的,貌似还纠结过使用 ?  还是 & 。




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