locked
Animating shapes multiple times RRS feed

  • Question

  • It seems that whenever i try to to animate a rectangle multiple times, the rectangle either doesnt move at all or only listens to one animation command.

    Example: rectangle1 = shapes.addrectangle

    shapes.move (rectangle1, 100, 100)

    shapes.animate (rectangle1, 200, 200, 3000)

    shapes.animate (rectangle1, 300, 300, 3000)

    shapes.animate (rectangle1, 400, 400, 3000)

    thanks

    Sunday, March 18, 2012 12:49 AM

Answers

  • Hello again Stewart555!

    Both Shapes.Move() & Shapes.Animate() move "sprites" to a specified coordinate at the GraphicsWindow. But in the case of Shapes.Animate(), there are 2 notable extra characteristics:

    • It tries to reach its goal (the coordinates) during a specified time, rather than just show up there immediately.
    • The rest of the code doesn't wait for the animation to complete, it goes on normally, oblivious to the Shape movement. This is the part which makes Shapes.Animation() so difficult to deal with, and why Shapes.Move() is generally the best choice to move sprites around!

    So, what happens in your code example is: just after you order your rectangle1 to go to 200,200 during 3s, you change its mind to 300,300 instead. And w/o leaving any room to breathe, you boss it to finally go to 400,400. And this last one is what only happens, ignoring the rest!

    As you can see, the last Shapes.Animate overwrites the previous ones, because the program continues to run your code w/o waiting for each of those animations!

    To solve this dilemma, just add up a delay = time of animation just after Shapes.Animation()! Here's how:

    Duration= 3000    ' 3 seconds '
    
    GraphicsWindow.Height= 550
    rectangle1= Shapes.AddRectangle(100,100)
    Shapes.Move(rectangle1  100,100)
    
    Shapes.Animate(rectangle1  200,200  Duration)
    Program.Delay(Duration)
    
    Shapes.Animate(rectangle1  300,300  Duration)
    Program.Delay(Duration)
    
    Shapes.Animate(rectangle1  400,400  Duration)
    Program.Delay(Duration)
    • Marked as answer by Stewart555 Sunday, March 18, 2012 12:48 PM
    Sunday, March 18, 2012 6:11 AM
    Answerer
  • The issue you may have is as GoToLoop suggested that the animate method is called taking a certain amount of time  (3 sec in your case) and the code doesn't wait for this before carrying on.  If you want the code to wait until the animation is finished before doing the next thing, then add Program.Delays after each animate.

    rec1 = Shapes.AddRectangle (50, 50)
    
    Shapes.Move (rec1, 100, 100)
    Shapes.Animate (rec1, 200, 200, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 300, 300, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 250, 250, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 150, 150, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 300, 100, 3000)
    Program.Delay(3000)

    • Marked as answer by Stewart555 Sunday, March 18, 2012 7:49 PM
    Sunday, March 18, 2012 7:09 PM

All replies

  • K Thanks It Worked.

    Sunday, March 18, 2012 1:06 AM
  • Hello again Stewart555!

    Both Shapes.Move() & Shapes.Animate() move "sprites" to a specified coordinate at the GraphicsWindow. But in the case of Shapes.Animate(), there are 2 notable extra characteristics:

    • It tries to reach its goal (the coordinates) during a specified time, rather than just show up there immediately.
    • The rest of the code doesn't wait for the animation to complete, it goes on normally, oblivious to the Shape movement. This is the part which makes Shapes.Animation() so difficult to deal with, and why Shapes.Move() is generally the best choice to move sprites around!

    So, what happens in your code example is: just after you order your rectangle1 to go to 200,200 during 3s, you change its mind to 300,300 instead. And w/o leaving any room to breathe, you boss it to finally go to 400,400. And this last one is what only happens, ignoring the rest!

    As you can see, the last Shapes.Animate overwrites the previous ones, because the program continues to run your code w/o waiting for each of those animations!

    To solve this dilemma, just add up a delay = time of animation just after Shapes.Animation()! Here's how:

    Duration= 3000    ' 3 seconds '
    
    GraphicsWindow.Height= 550
    rectangle1= Shapes.AddRectangle(100,100)
    Shapes.Move(rectangle1  100,100)
    
    Shapes.Animate(rectangle1  200,200  Duration)
    Program.Delay(Duration)
    
    Shapes.Animate(rectangle1  300,300  Duration)
    Program.Delay(Duration)
    
    Shapes.Animate(rectangle1  400,400  Duration)
    Program.Delay(Duration)
    • Marked as answer by Stewart555 Sunday, March 18, 2012 12:48 PM
    Sunday, March 18, 2012 6:11 AM
    Answerer
  • STEWART!!!!!!!!!!!!!!!!!!!!!!!!! DON'T FORGET TO GIVE POINTS TO YOUR ANSWERERS AND MARK POSTS AS ANSWERS, AS EVERY YOUR POST IS MARKED AS UNANSWERED, WE ANSWER NOT JUST FOR ANSWER, BUT FOR POINTS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Sunday, March 18, 2012 8:22 AM
  • kk ganiman
    Sunday, March 18, 2012 12:45 PM
  • gotoloop,

    i used your idea but it just isnt working in my program. i tried it in another program of its own and it worked fine. Remember when i asked how to clear a graphics window and you said that graphicswindow.clear was buggy, well i have one of those before the animations. Is it possible that is the problem? As well, i already tried your solution for clearing a graphics window and i cant seem to get it to work either.

    Sunday, March 18, 2012 1:32 PM
  • Stewart,

    The GraphicsWindow.Clear() will delete all shapes - you could put all the shape creation and initialization in a subroutine and call it after doing the GraphicsWindow.Clear().

    Perhaps also show the actual code you are having problems with and how to reproduce the problem, if it is feasible (not enormous or full of images etc that anyone helping won't have).

    Sunday, March 18, 2012 2:08 PM
  • The examples we give out here are just general templates. You have to try to understand how they work, so you can adapt them into your own programs.

    In the subject of Shapes and Controls, once you use GraphicsWindow.Clear(), all of them are erased from memory and vanished from the screen! You'd have to create them all over again!!!

    If you wanna spare them from such fate, just create a Subroutine which just draws a rectangle at least as big as GraphicsWindow, and fill it up using a BrushColor w/ the same color as the current BackgroundColor. Like I've shown you at your Subs and Interactivity thread.

    ' ---------------------------------------------------------------------------------------------------------------------' Sub ClearScreen ' Clears GraphicsWindow w/o erasing Shapes nor Controls objects.' Stack.PushValue("BackgroundColor" GraphicsWindow.BrushColor) GraphicsWindow.BrushColor= GraphicsWindow.BackgroundColor GraphicsWindow.FillRectangle(0,0, GraphicsWindow.Width, GraphicsWindow.Height) GraphicsWindow.BrushColor= Stack.PopValue("BackgroundColor") EndSub ' ---------------------------------------------------------------------------------------------------------------------'

    In short, if you don't mind losing your Shapes and/or Controls, use GraphicsWindow.Clear(). Otherwise, use my homemade ClearScreen() sub    (^_^)

    Sunday, March 18, 2012 4:45 PM
    Answerer
  • I understand how the first example works, it just doesnt work when i run the program.....

    Sunday, March 18, 2012 4:55 PM
  • Probably, you should publish a snip of your code here, so we can try to figure out what's not working for ya!

    It's hard to control GraphicsWindow.Animate() since your program proceeds leaving the animation happening behind it. You have to take care not to override its trajectory before its designated time has passed!

    Sunday, March 18, 2012 5:04 PM
    Answerer
  • alright

    Sunday, March 18, 2012 6:51 PM
  • here it is:

    rec1 = Shapes.AddRectangle (50, 50)

    Shapes.Move (rec1, 100, 100)

    Shapes.Animate (rec1, 200, 200, 3000)

    Shapes.Animate (rec1, 300, 300, 3000)

    Shapes.Animate (rec1, 250, 250, 3000)

    Shapes.Animate (rec1, 150, 150, 3000)

    Shapes.Animate (rec1, 300, 100, 3000)

    Sunday, March 18, 2012 6:58 PM
  • The issue you may have is as GoToLoop suggested that the animate method is called taking a certain amount of time  (3 sec in your case) and the code doesn't wait for this before carrying on.  If you want the code to wait until the animation is finished before doing the next thing, then add Program.Delays after each animate.

    rec1 = Shapes.AddRectangle (50, 50)
    
    Shapes.Move (rec1, 100, 100)
    Shapes.Animate (rec1, 200, 200, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 300, 300, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 250, 250, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 150, 150, 3000)
    Program.Delay(3000)
    Shapes.Animate (rec1, 300, 100, 3000)
    Program.Delay(3000)

    • Marked as answer by Stewart555 Sunday, March 18, 2012 7:49 PM
    Sunday, March 18, 2012 7:09 PM