Odd starfield issue

BlitzMax Forums/BlitzMax Beginners Area/Odd starfield issue

Kanati(Posted 2011) [#1]
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




Kanati(Posted 2011) [#2]
Never mind. I'm an idiot. :D

I fiddle with this code for a week off and on and didn't catch it. Post it here and while reading the post, I see it immediately.

			tmpStar.y = 0


should obviously be

			tmpStar.y = Rand(960)


I'm an idiot.


col(Posted 2011) [#3]
Hiya, as you are creating and capturing an image as a 1x1 image, I think you'll find its actually faster to just use the Plot command.

Nice starfield BTW :)