We need to learn the scope, the difference between local variable and instance variable, over and over again.
# sample60.rb
Shoes.app :width => 300, :height => 300 do
i = 45
button 'new'do
i += 5
box = rand(2) == 0 ? rect(i, i, 20) : oval(i, i, 20)
box.style :fill => send(COLORS.keys.map{|sym|sym.to_s}[rand(COLORS.keys.size)])
@flag = false
box.click{@flag = true; @box = box}
box.release{@flag = false}
motion{|left, top| @box.move(left-10, top-10) if @flag}
end
end
sample60.png
This snapshot is created by the following setps.
- clicked 'new' button 30 times
- picked up and drew 6 ovals to the lower side
- picked up and drew 7 rects to the upper side
The sample60.rb worked well with Shoes-0.r1263