-1

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 < (self.width-@im.width)
      @im.left += 5
    else
      @an.stop
    end
  end

  @an2 = animate 20 do
    if @im.left <= (self.width-@im.width) and @im.left > 0
      @im.left -= 5
    else
      @an2.stop
    end
  end

end 
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Tkinter
  • 19
  • 1
  • 3

1 Answers1

0

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 < (self.width-@im.width)) && @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 
Wand Maker
  • 18,476
  • 8
  • 53
  • 87