Search code examples
rubyshoes

Ruby Shoes animation


Why only the first animation works good in my program? An image should move from left to the right and then from right to the left. This is happening only from left to the right.

Shoes.app do

  @im = image("D:/image.jpg", left: 0, top: 220, width: 180, height: 230)

  @an = animate 20 do
    if @im.left < ([email protected])
      @im.left += 5
    else
      @an.stop
    end
  end

  @an2 = animate 20 do
    if @im.left <= ([email protected]) and @im.left > 0
      @im.left -= 5
    else
      @an2.stop
    end
  end

end 

Solution

  • It looks like you don't need two animation object, but work with single animation object to move forward and backward during animation.

    Here is a working app doing animation from left-to-right and right-to-left to prescribed number of iterations defined by variable @iterations.

    Before running, don't forget to update the image path (Currently, a.jpg)

    Shoes.app do
    
      @im = image("a.jpg", left: 0, top: 220, width: 180, height: 230)
    
      @direction = :forward
      @iteration = 5
    
      @an = animate 64 do
        if (@im.left < ([email protected])) && @direction == :forward
            @im.left += 5
        elsif @direction == :forward
            @direction = :backward 
        end
    
        if @im.left > 0 && @direction == :backward
            @im.left -= 5
        elsif @direction == :backward
            @direction = :forward
            @iteration -= 1
        end
    
        @an.stop if @iteration == 0
      end
    end