2017-02-20 30 views
0

下面是完整的代碼:如何讓形狀獨立移動而不出現抖動?

GraphicsWindow.Clear() 
GraphicsWindow.CanResize = "false" 
GraphicsWindow.Height = Desktop.Height-200 
GraphicsWindow.Width = Desktop.Width-200 

scount = Math.GetRandomNumber(25) 
For s = 1 To scount 
    Shx[s] = Math.GetRandomNumber(GraphicsWindow.Width-100) 
    Shy[s] = Math.GetRandomNumber(GraphicsWindow.Height-100) 
    shsize[s] = Math.GetRandomNumber(50) 
    Sh[s] = Shapes.AddEllipse(shsize[s],shsize[s]) 
    Shapes.Move(Sh[s],Shx[s],Shy[s]) 
EndFor 

loop: 
For s = 1 to scount 
    op[s] = Math.GetRandomNumber(2) 
    If op[s] = 1 Then 
    vel[s] = .5 
    EndIf 
    If op[s] = 2 Then 
    vel[s] = -.5 
    EndIf 
    Shx[s] = Shx[s] + vel[s] 
    Shy[s] = Shy[s] + vel[s] 
    Shapes.Move(Sh[s],Shx[s],Shy[s]) 
EndFor 
Goto loop 

我的猜測是這個問題在這裏:

op[s] = Math.GetRandomNumber(2) 
    If op[s] = 1 Then 
    vel[s] = .5 
    EndIf 
    If op[s] = 2 Then 
    vel[s] = -.5 
    EndIf 

什麼我需要做的,使形狀的獨立方向移動沒有他們引起的顫動?

回答

0

你可以做的另一件事是每秒幀數。讓所有對象都進行數學計算然後更新它。在完成所有數學運算之後,運行tHis,就像 通常是每秒60幀,因此1/60可以得到每幀的秒數。您還需要一個計時器來檢查時間流逝的時間量,以便您可以讓計算機在適當的時間移動每個形狀。如果你需要更多的代碼,我會很樂意提供一些。

GraphicsWindow.Clear() 
GraphicsWindow.CanResize = "false" 
GraphicsWindow.Height = Desktop.Height-200 
GraphicsWindow.Width = Desktop.Width-200 

scount = Math.GetRandomNumber(25) 
For s = 1 To scount 
    Shx[s] = Math.GetRandomNumber(GraphicsWindow.Width-100) 
    Shy[s] = Math.GetRandomNumber(GraphicsWindow.Height-100) 
    shsize[s] = Math.GetRandomNumber(50) 
    Sh[s] = Shapes.AddEllipse(shsize[s],shsize[s]) 
    Shapes.Move(Sh[s],Shx[s],Shy[s]) 
    op[s] = Math.GetRandomNumber(2) 
EndFor 

loop: 
Time = Clock.ElapsedMilliseconds 
For s = 1 to scount 

    If op[s] = 1 Then 
    vel[s] = .5 
    EndIf 
    If op[s] = 2 Then 
    vel[s] = -.5 
    EndIf 
    Shx[s] = Shx[s] + vel[s] 
    Shy[s] = Shy[s] + vel[s] 
    Shapes.Move(Sh[s],Shx[s],Shy[s]) 
EndFor 
frames() 
Goto loop 

Sub frames 
    timeend = Clock.ElapsedMilliseconds 
    TextWindow.WriteLine((timeend - Time)/1000) 
    framelength = 60 
    Timeperframe = 1/framelength 
    while (((timeend - Time)/1000) < Timeperframe) 
    timeend = Clock.ElapsedMilliseconds 

    EndWhile 
    Time = Clock.ElapsedMilliseconds 

    endsub 
+0

如果你想語句來執行無論每秒幀數如何,速度都保持不變,您只需根據每秒幀數編輯速度即可。所以可以說,無論幀是什麼,每秒鐘總是需要5像素移動,而每秒移動5個像素,這就是每秒移動的像素數量。所以如果你有每秒10幀,你會移動0.5像素每一幀增加到5像素在那秒。 :-希望這有助於。 – Matthew

0

我把:

op[s] = Math.GetRandomNumber(2) 
If op[s] = 1 Then 
vel[s] = .5 
EndIf 
If op[s] = 2 Then 
vel[s] = -.5 
EndIf 

在初始化循環,形狀也不再抖動。我的猜測是,「vel」變量每次都被重新分配給形狀,導致抖動。

+0

這是真的,你可能也做在主迴路中,如果部分,但工作我認爲更好的你做了什麼,這樣你不必如果每次 – Matthew