tips_etc_010 - spoolkitamura/nyle-doc-jp GitHub Wiki
drawと updateのどちらに書けばいい?
Screenクラスの draw
メソッドと update
メソッドは、どちらも一定の間隔で
呼び出されますので、どちらにプログラムを書いても動作的には違いがありません。
ただ、プログラムの構成をわかりやすくし、複雑になっても修正したり拡張したり
しやすいプログラムにするには、
「draw
」メソッドには「描画に関する処理」 を書き
「update
」メソッドには「データの更新に関する処理 を書くようにすることを推奨します。
下記の3つのプログラムはどれも同じ動作になりますが、
draw
と update
の役割が明確になっている最後の3番目のプログラムの書き方が
もっともわかりやすいと思います。
draw
メソッドにすべて記述
require 'nyle'
class Screen < Nyle::Screen
def initialize
super(240, 240)
@r = 10
end
def draw
Nyle.draw_circle(120, 120, @r, {color: :BLUE})
@r += 5
@r = 10 if @r > 100
end
end
Screen.new.show_all
Nyle.main
[実行結果]
![]() |
---|
update
メソッドにすべて記述
require 'nyle'
class Screen < Nyle::Screen
def initialize
super(240, 240)
@r = 10
end
def update
Nyle.draw_circle(120, 120, @r, {color: :BLUE})
@r += 5
@r = 10 if @r > 100
end
end
Screen.new.show_all
Nyle.main
[実行結果]
![]() |
---|
draw
メソッドとupdate
メソッドに分けて記述
require 'nyle'
class Screen < Nyle::Screen
def initialize
super(240, 240)
@r = 10
end
def draw
Nyle.draw_circle(120, 120, @r, {color: :BLUE})
end
def update
@r += 5
@r = 10 if @r > 100
end
end
Screen.new.show_all
Nyle.main
[実行結果]
![]() |
---|