I've been looking at starfield code and I copied some of it and have been fiddling with it for a while and when I try to reuse objects I get some weird behavior. Could someone tell me where I'm going wrong here? The code LOOKS the same as the code I based it off of with just a few tweaks, but I'm getting "lines" of stars instead of a starFIELD.
SuperStrict
Graphics 1280, 964
Const TARGET:Int = 60
Local frames:Int = 0
Local frameTime:Int
Local totalTime:Int=0
Local tCounter:Int = 0
Type TStar
Global StarList:TList
Field x:Float
Field y:Float
Global starimg:TImage
Field Speed:Int
Function CreateStarField(num:Int)
If StarList = Null Then StarList = CreateList()
For Local i:Int = 0 To num
Local tmpStar:TStar = New TStar
tmpStar.Speed = Rand(2, 10)
tmpStar.x = Rand(1280)
tmpStar.y = 0
ListAddLast StarList, (tmpStar)
DebugLog tmpStar.Speed + " " + tmpStar.x
Next
Plot 1, 1
starimg = CreateImage(1, 1)
GrabImage(starimg, 1, 1, 0)
End Function
Function UpdateStars()
For Local s:TStar=EachIn StarList
s.y:+s.Speed
s.RecycleStar()
s.DrawStar()
Next
End Function
Method RecycleStar()
If y > 964 Then y = -1
End Method
Method DrawStar()
DrawImage(starimg, x, y, 0)
End Method
End Type
TStar.CreateStarField(750)
Cls
While not KeyDown(KEY_ESCAPE)
frameTime = MilliSecs()
TStar.UpdateStars()
DrawText TARGET + " Frames:" + totalTime + "ms", 0, 12
tCounter:+(MilliSecs() - frameTime)
If frames = TARGET Then
totalTime = tCounter
frames = 0
tCounter = 0
Else
frames:+1
EndIf
Flip
Cls
Wend
|