DeathKing 发表于 2013-7-4 23:14
我现在读的工地大学就是成天杂事多,写URG3我都得挤时间……这不,又遇上考试周了。 ...
举个例子:
@command_window.set_handler(:new_game, method(:command_new_game)) @command_window.set_handler(:continue, method(:command_continue)) @command_window.set_handler(:shutdown, method(:command_shutdown))
@command_window.set_handler(:new_game, method(:command_new_game))
@command_window.set_handler(:continue, method(:command_continue))
@command_window.set_handler(:shutdown, method(:command_shutdown))
太特么的 NC 了,没完没了地重复形式,完全可以设计一个方法来简化掉
#-------------------------------------------------------------------------- # * Create Command Window #-------------------------------------------------------------------------- def create_command_window @command_window = Window_TitleCommand.new set_command_window_handlers [ [:new_game, method(:command_new_game)], [:continue, method(:command_continue)], [:shutdown, method(:command_shutdown)] ] end def set_command_window_handlers(pairs) pairs.each do |pair| @command_window.set_handler(*pair) end end
#--------------------------------------------------------------------------
# * Create Command Window
#--------------------------------------------------------------------------
def create_command_window
@command_window = Window_TitleCommand.new
set_command_window_handlers [
[:new_game, method(:command_new_game)],
[:continue, method(:command_continue)],
[:shutdown, method(:command_shutdown)]
]
end
def set_command_window_handlers(pairs)
pairs.each do |pair|
@command_window.set_handler(*pair)
end
end
这还不算最优方案,但是 RGSS 这种浪费在重复不必要的输入上太多了
rect = Rect.new(20, 30, 140, 32) bitmap = Bitmap.new(rect.width, rect.height) sprite = Sprite.new sprite.x = rect.x sprite.y = rect.y sprite.bitmap = bitmap
rect = Rect.new(20, 30, 140, 32)
bitmap = Bitmap.new(rect.width, rect.height)
sprite = Sprite.new
sprite.x = rect.x
sprite.y = rect.y
sprite.bitmap = bitmap
像这样生成一个实例马上 balabala 一堆属性,这你能忍?完全可以设计一个关键字参数的初始化方法嘛
rect = Rect.new(20, 30, 140, 32) bitmap = Bitmap.new(rect.width, rect.height) sprite = Sprite.new bitmap: bitmap, x: rect.x, y: rect.y
rect = Rect.new(20, 30, 140, 32)
bitmap = Bitmap.new(rect.width, rect.height)
sprite = Sprite.new bitmap: bitmap, x: rect.x, y: rect.y
Rect 也可以设计一个 spot 用来得到 xy,aspect 得到 width 和 height
rect = Rect.new(20, 30, 140, 32) bitmap = Bitmap.new(*rect.aspect) sprite = Sprite.new bitmap: bitmap, location: rect.spot
rect = Rect.new(20, 30, 140, 32)
bitmap = Bitmap.new(*rect.aspect)
sprite = Sprite.new bitmap: bitmap, location: rect.spot
RGSS 里这种成对属性分开设置的地方实在太多了 |