0

我一直在尝试在 Small Basic 中模拟跳跃,我原本以为它很简单,但比我预期的要复杂。每当我尝试在 for 循环中使用动画(或移动)时,程序似乎总是将我在开始时分配的任何延迟放在一个动画/移动之后。例如:

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

我希望这个程序以递减的速度增加为什么位置的圆圈,但它会等待 1 秒(循环中的总毫秒数),然后一次向上移动。我怎样才能实现我想要的并解决这个问题?

4

2 回答 2

0

原因是,它等待整个子执行然后更新它。您想要的是 sub 有一个语句,并在调用子例程的 for 循环中进行数学运算。

于 2017-02-28T19:49:24.387 回答
0

+马修有正确的理由。Small Basic 中的线程有点奇怪和不可预测,是的......带有移动命令的线程在按键事件完成之前不会看到移动请求。

这是您的代码的一个版本,其中移动放置在主线程中:

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
于 2017-04-03T01:07:48.293 回答