Project1

标题: 关于描绘状态的问题 [打印本页]

作者: 945127391    时间: 2013-2-21 06:07
标题: 关于描绘状态的问题
本帖最后由 945127391 于 2013-2-22 22:06 编辑

好吧,我研究了好久都研究不出来= =(众:你研究过吗?!)
其实是这样的:
Window_Base有一个描绘角色状态的脚本,也就是下面这部分内容:
  1. #--------------------------------------------------------------------------
  2. # ● 绘制强化/弱化状态的图标
  3. #--------------------------------------------------------------------------
  4. def draw_actor_icons(actor, x, y, width = 96)
  5.   icons = (actor.state_icons + actor.buff_icons)[0, width / 24]
  6.   icons.each_with_index {|n, i| draw_icon(n, x + 24 * i, y) }
  7. end
复制代码
但是我理解不了第二句和第三句……
第二句是什么意思?
each_with_index又是啥??
希望大神指导……
谢谢了辛苦了拜托了……
(光速秒逃)
作者: LBQ    时间: 2013-2-21 09:28
each_index 还好解释= =
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-each_index
这个就是each_index的解释= =貌似是数组的方法= =
(假如我搞错了= =,这个icon其实是哈希的话自行查找哈希的each_index 方法= =)
作者: Sion    时间: 2013-2-21 10:54
本帖最后由 Sion 于 2013-2-21 13:07 编辑

Array[i, n]就是得到Array从索引 i  处开始之后的 n 个元素(包含元素 i )组成的数组
  1. a = ["a", "b", "c", "d", "e", "f"]
  2. i = 0; n = 4
  3. p a[i, n] #=> ["a", "b", "c", "d"]
  4. p a       #=> ["a", "b", "c", "d", "e", "f"]
复制代码
each_index是索引;each_with_index是元素+索引,block可以设置2个参数,只用1个参数的时候等于each:
  1. a = ["a", "b", "c"]
  2. a.each_index {|i| print  i, " -- " }                #=> 0 -- 1 -- 2 --
  3. puts ""
  4. a.each_with_index {|n| print n, " -- " }            #=> a -- b -- c --
  5. puts ""
  6. a.each {|n| print n, " -- " }                       #=> a -- b -- c --
  7. puts ""
  8. a.each_with_index {|n,i| print n, ",",  i, " -- " } #=> a,0 -- b,1 -- c,2 --
复制代码

作者: 945127391    时间: 2013-2-22 21:57
Sion 发表于 2013-2-21 10:54
Array就是得到Array从索引 i  处开始之后的 n 个元素(包含元素 i )组成的数组each_index是索引;each_wit ...

似乎有点懂了= =
也就是说,
  1. icons = (actor.state_icons + actor.buff_icons)[0, width / 24]
复制代码
相当于
  1. icons = (actor.state_icons + actor.buff_icons)
  2. icons = icons[0, width / 24]
复制代码

然后,
  1. icons.each_with_index {|n, i| draw_icon(n, x + 24 * i, y) }
复制代码
相当于:
  1. for i in 0...icons.size
  2.   n = icons[i]
  3.   draw_icon(n, x + 24 * i, y)
  4. end
复制代码

好吧上面都是我蒙的




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