On Thu, 6 Aug 2009, Juan Andrés Gebhard wrote:
> Hi everyone! I'm trying to make a chess game. To draw the board, I thought
> of placing 8 flows, and 8 stack inside of each flow. And finally, each stack
> would have a PNG image inside.
> Instead of simple stacks, i decided to create a widget, so that it would be
> easier to change the image inside of it (that happens whenever a player
> makes a move).
> The problem with the widget is that i can't put one next to each other. They
> insist on putting themselves below the previous one, which isn't the case
> with simple stacks.
>
> class Casillero < Widget
> attr_accessor :img, :back, :color
> def initialize opts = {}
> @stack = stack :width =>100, :height=>100
> @back = @stack.background(*opts[:color]) if opts[:color]
> end
> end
>
> Shoes.app{
> flow :width => 900 do
> 8.times do |i|
> @c = casillero :color => black if i%2==0
> @c = casillero :color => white if i%2!=0
# I think each time round the loop you flatten the @c instance variable.
# Then maybe the casillero gets garbage collected? I don't know, but
# it looks suspicious to me anyway. Your @c will only point at the last
# casillero you create.
# I think perhaps you could write it as:
a_colour = if (i%2).zero?
black
else
white
end
casillero :color => a_colour
# or
casillero :color => (i%2).zero? ? black : white ;
> end
> end
> }
# and not bother to keep a variable for that square. Although it might
# make sense to put the casilleros in a Hash of Hashes so you can access
# them later.
>
This is all just thinking out loud, not tried any of this. Hope it helps.
Hugh