Project1

标题: 【VxAce】系统脚本Window_BattleActor脚本请教 [打印本页]

作者: alianlord    时间: 2020-9-1 10:52
标题: 【VxAce】系统脚本Window_BattleActor脚本请教
#==============================================================================
# ■ Window_BattleActor
#------------------------------------------------------------------------------
#  战斗画面中,选择“队友目标”的窗口。
#==============================================================================

class Window_BattleActor < Window_BattleStatus
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #     info_viewport : 信息显示用显示端口
  #--------------------------------------------------------------------------
  def initialize(info_viewport)
    super()
    self.y = info_viewport.rect.y
    self.visible = false
    self.openness = 255
    @info_viewport = info_viewport
  end
  #--------------------------------------------------------------------------
  # ● 显示窗口
  #--------------------------------------------------------------------------
  def show
    if @info_viewport
      width_remain = Graphics.width - width
      self.x = width_remain
      @info_viewport.rect.width = width_remain
      select(0)
    end
    super
  end
  #--------------------------------------------------------------------------
  # ● 隐藏窗口
  #--------------------------------------------------------------------------
  def hide
    @info_viewport.rect.width = Graphics.width if @info_viewport
    super
  end
end

>>>  请问一下 红字的 width 指的是那个窗口或是viewport的宽度,它是怎么引用过来的?
作者: 百里_飞柳    时间: 2020-9-1 10:57
是省略了 self 的方法调用,即 self.width
而 width 是window类的属性(即默认设置了 attr_accessor :width ),具体见F1中RGSS对象-window-属性-width
作者: alianlord    时间: 2020-9-1 11:06
百里_飞柳 发表于 2020-9-1 10:57
是省略了 self 的方法调用,即 self.width
而 width 是window类的属性(即默认设置了 attr_accessor :width ...

self.width 理解为 @actor_window 的 width 。那 从那理知道@actor_window 的 width  。我找不到赋值的地方。
作者: alexncf125    时间: 2020-9-1 11:39
本帖最后由 alexncf125 于 2020-9-1 11:48 编辑
alianlord 发表于 2020-9-1 11:06
self.width 理解为 @actor_window 的 width 。那 从那理知道@actor_window 的 width  。我找不到赋值的地 ...


class Window_BattleStatus < Window_Selectable
  #--------------------------------------------------------------------------
  # ● 初始化對象
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, window_width, window_height)
    refresh
    self.openness = 0
  end
  #--------------------------------------------------------------------------
  # ● 獲取窗口的寬度
  #--------------------------------------------------------------------------
  def window_width
    Graphics.width - 128 + 1

  end
class Window_BattleActor < Window_BattleStatus
  #--------------------------------------------------------------------------
  # ● 初始化對象
  #     info_viewport : 信息顯示用顯示端口
  #--------------------------------------------------------------------------
  def initialize(info_viewport)
    super()
    self.y = info_viewport.rect.y
    self.visible = false
    self.openness = 255
    @info_viewport = info_viewport
  end
  #--------------------------------------------------------------------------
  # ● 顯示窗口
  #--------------------------------------------------------------------------
  def show
    if @info_viewport
      p width          #=>417
      width_remain = Graphics.width - width
      self.x = width_remain
      @info_viewport.rect.width = width_remain
      select(0)
    end
    super
  end
作者: alianlord    时间: 2020-9-1 12:00
原来是这样继承来的,现在总算搞明白了。
作者: alianlord    时间: 2020-9-1 12:13
alexncf125 发表于 2020-9-1 11:39
class Window_BattleStatus < Window_Selectable
  #------------------------------------------------- ...

这里有新的疑问点,我查网上有关super 的教程

我们经常要在子类的initialize方法中调用super和super()。

从语法上说super和super()是有微妙区别的。

    super不带括号表示调用父类的同名函数,并将本函数的所有参数传入父类的同名函数;

    super()带括号则表示调用父类的同名函数,但是不传入任何参数;

如果此说法无误,那 Window_BattleStatus 的
#--------------------------------------------------------------------------
  # ● 获取窗口的宽度
  #--------------------------------------------------------------------------
  def window_width
    Graphics.width - 128
就继承不下来。因为,
Window_BattleActor 的
#--------------------------------------------------------------------------
  # ● 初始化对象
  #     info_viewport : 信息显示用显示端口
  #--------------------------------------------------------------------------
  def initialize(info_viewport)
    super()
    self.y = info_viewport.rect.y
    self.visible = false
    self.openness = 255
    @info_viewport = info_viewport

我的理解是否正确?
  end
作者: 喵呜喵5    时间: 2020-9-1 16:08
本帖最后由 喵呜喵5 于 2020-9-1 16:14 编辑
alianlord 发表于 2020-9-1 12:13
这里有新的疑问点,我查网上有关super 的教程

我们经常要在子类的initialize方法中调用super和super() ...


不正确

不是啥带括号不带括号只导致继承不下来啥的……是不正确的使用括号代码会直接报错…………

super 的作用是调用父类的 initialize 方法,省略括号时,表示调用方法的参数直接使用本方法的参数,
但有的时候,子类方法所需的参数和父类方法所需的参数是不一样的,这时候省略括号就会报参数不一致的错误


你可以试着分析一下下面这段代码:

  1. class A
  2.   def initialize(a = 'A')
  3.     p a
  4.   end
  5. end

  6. A.new()
  7. A.new('AA')

  8. class B < A
  9.   def initialize(a)
  10.     super
  11.   end
  12. end

  13. B.new('B')

  14. class B2 < A
  15.   def initialize(a)
  16.     super(a)
  17.   end
  18. end

  19. B2.new('B2')

  20. class B3 < A
  21.   def initialize(a)
  22.     super()
  23.   end
  24. end

  25. B3.new('B3')

  26. class C < A
  27.   def initialize
  28.     super('C')
  29.   end
  30. end

  31. C.new()

  32. class D < A
  33.   def initialize(a, b, c)
  34.     p a
  35.     super(b)
  36.     p c
  37.   end
  38. end

  39. D.new('D1','D2','D3')
复制代码


作者: alianlord    时间: 2020-9-1 19:32
喵呜喵5 发表于 2020-9-1 16:08
不正确

不是啥带括号不带括号只导致继承不下来啥的……是不正确的使用括号代码会直接报错…………

辛苦了。super 是代入所有的继承架构和要素。
super(0, info_viewport.rect.y, window_width, fitting_height(4))而这类是继承架构数目相同,引入各别不同的要素。
super(), 就是继承驾构的数目不一样,于是完全打散重装。
在va的所有窗口的初始化的 super中,只有Window_BattleActor 用了super()
细思极恐。我还得慢慢钻研。




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