Search code examples
smallbasic

Moving a shape across the screen and making it disappear


this program that i have so far works but it freezes when it gets to the for loop. i have done something like this with another program but with shapes it doesnt like it.

GraphicsWindow.Height = 400
GraphicsWindow.Width = 600
GraphicsWindow.Title = "FairyTail"
GraphicsWindow.CanResize = "False"
animation()
Controls.ButtonClicked = action

Sub animation
GraphicsWindow.BrushColor = "Black"
Firstmove = Controls.AddButton("fireball", 300, 100)
Controls.Move(Firstmove, 0, 200)

endsub
 Sub action
If Controls.GetButtonCaption(Firstmove) = "fireball" Then
GraphicsWindow.BrushColor = "Red"
fireball = Shapes.AddEllipse(20, 20)
Shapes.Move(fireball, 135, 115)

For i = 135 To 465
if i <> 465 then 
  Shapes.animate(fireball, i, 115, 1000)
  i = i + 1
  Program.Delay(100)
Else 
    Shapes.Remove(fireball)
    endif 
   endfor 
endif

endsub

what im trying to do is move the fireball across the screen then remove it. but i dont know how to remove it after it animates.


Solution

  • There are a few problems with this program. The first one is this:

    For i = 135 To 465
    if i <> 465 then 
    Shapes.animate(fireball, i, 115, 1000)
    i = i + 1
    Program.Delay(100)
    Else 
    Shapes.Remove(fireball)
    endif 
    endfor 
    

    If you already have a For statement closing after "i" = 465, You don't really need the If statement.

    The second problem, (And the reason its not running), is this:

    Shapes.animate(fireball, i, 115, 1000)
    

    What this command does, is it moves the shape to the set x and y coordinate over a set time. So this means that it will move the shape from its currant position, to i,115 after 1000 MS.

    What you really need here is Shapes.Move.

    Also, Its usually a good Idea to make all loop execute outside the subroutines. This is because if you click the button twice, It will try to call the subroutine while the subroutine i still running (Because its looping inside it) This will cause problems. Here is the way I would make this Program:

     GraphicsWindow.Height = 400
     GraphicsWindow.Width = 600
     GraphicsWindow.Title = "FairyTail"
     GraphicsWindow.CanResize = "False"
     animation()
     Controls.ButtonClicked = action
    
     While 1 = 1 
     Program.Delay(10)
     If CanMoveFireball Then
     i = i + 1 '<--- Increase the position and move it to that position
     Shapes.Move(fireball,i,115)
     EndIf
    
     If i > 465 Then '<--- If the fireball is past 465 then remove it and say its OK to add another
     Shapes.Remove(fireball)
     CanMoveFireball = "False"
     EndIf
     EndWhile
    
     Sub animation
     GraphicsWindow.BrushColor = "Black"
     Firstmove = Controls.AddButton("fireball", 300, 100)
     Controls.Move(Firstmove, 0, 200)
     endsub
    
     Sub action
     If Controls.LastClickedButton = Firstmove Then
     If CanMoveFireball <> "True" Then '<--- Make sure you don't add another      fireball while the first one is moving
     GraphicsWindow.BrushColor = "Red"
     fireball = Shapes.AddEllipse(20, 20)
     Shapes.Move(fireball, 135, 115)
     i = 135
     CanMoveFireball = "True" '<--- Tell it it's OK to move Fireball
     EndIf
    
    Endif
    Endsub
    

    I hope this helps!!

    --Zock