How to stack animations in Small Basic

601 views Asked by At

I have been trying to simulate jumping in Small Basic, which I had originally though to be simple but is trickier than I expected. Whenever I try to use animations (or move) in a for loop the program always seems to put whatever delay I have assigned at the start followed by a single animation/move. For example:

GraphicsWindow.Height = 480
GraphicsWindow.Width = 640

pX = 300
pY = 220

GraphicsWindow.KeyDown = KeyPressed

player = Shapes.AddEllipse(40, 40)
Shapes.Move(player, 300, 220)

Sub KeyPressed
  If GraphicsWindow.LastKey = "Space" Then
    For i = 1 To 10
      pY = pY - (10 - i)
      Shapes.Move(player, pX, pY)
      Program.Delay(100)
    EndFor
  EndIf
EndSub

I would expect this program to increase the circles why position at a decreasing rate, but instead it waits 1 second (the total number of milliseconds in the loop) and then moves up the whole way at once. How can I achieve what I want and fix this?

2

There are 2 answers

0
Matthew On

The reason is because, it waits for the whole sub to execute then it updates it. what you want is the sub to have a single statement and have the math in a for loop that is calling the subroutine.

0
codingCat On

+Matthew has the cause right. The threading in Small Basic is a little weird and unpredictable, and yes... the thread with the move command won't see the move requests until after the key press event has completed.

Here is an version of your code with the move placed into the main thread:

GraphicsWindow.Height = 480
GraphicsWindow.Width = 640

pX = 300
pY = 220

GraphicsWindow.KeyDown = KeyPressed

player = Shapes.AddEllipse(40, 40)
Shapes.Move(player, 300, 220)

top:
If moving = "true" then
  For i = 1 To 10
    pY = pY - (10 - i)
    Shapes.Move(player, pX, pY)
    Program.Delay(100)
  EndFor
  moving = "false"
endif
Goto top

Sub KeyPressed
  If GraphicsWindow.LastKey = "Space" Then
    moving = "true"
  EndIf
EndSub