here's an example that demonstrates the issue, WASD + Left Mouse
comment out flushkeys to see what I mean.
Strict
Graphics 640,480,0,60
Type TBullet
Global BulletList:TList
Field X:Float
Field Y:Float
Field Life:Int
Field XOffset:Float
Field YOffset:Float
Method Update()
X:+ XOffset
Y:+ YOffset
If (X < -16) Or (X > GraphicsWidth()+16) Then
XOffset = -XOffset
EndIf
If (Y < -16) Or (Y > GraphicsHeight()+16) Then
YOffset = -YOffset
EndIf
If (Xoffset<1 And XOffset>-1) And (YOffset<1 And YOffset>-1) Then
free(Self)
EndIf
If life<= 0 Then free(Self)
life = life - 1
End Method
Method Draw()
SetColor 0,0,255
DrawOval(x-4,y-4,8,8)
End Method
Function Handle()
If BulletList <> Null Then
For Local bullet:TBullet = EachIn BulletList
bullet.Update()
bullet.Draw()
Next
SetColor 255,255,255
DrawText(CountList(BulletList), 10,10)
EndIf
End Function
Function Free(bullet:TBullet)
BulletList.Remove(bullet)
bullet = Null
End Function
Function Create:TBullet(X:Float,Y:Float,Xoffset:Float, YOffset:Float)
If BulletList = Null Then BulletList = New TList
Local tempBullet:TBullet = New TBullet
tempBullet.X = X
tempBullet.Y = Y
tempBullet.XOffset = XOffset
tempBullet.YOffset = YOffset
tempBullet.Life=Rand(40,140)
BulletList.AddLast(tempBullet)
Return tempBullet
End Function
End Type
Type TPlayer
Field X:Float
Field Y:Float
Method Limit(val:Float Var, MinVal:Float, MaxVal:Float)
If val < MinVal Then val = MinVal
If val > MaxVal Then val = MaxVal
End Method
Method Fire()
Local XOffset:Float
Local YOffset:Float
XOffset = (MouseX()-X)/2
YOffset = (MouseY()-Y)/2
Limit(Xoffset, -20,20)
Limit(YOffset, -20,20)
Local tempBullet:TBullet = TBullet.Create(X,Y,XOffset, YOffset)
End Method
Method Update()
If KeyDown(KEY_W) Then ' up
Y :- 5
EndIf
If KeyDown(KEY_S) Then ' down
Y :+ 5
EndIf
If KeyDown(KEY_A) Then ' left
X:-5
EndIf
If KeyDown(KEY_D) Then ' right
X:+5
EndIf
Limit(Y, 16, GraphicsHeight()-16)
Limit(X, 16, GraphicsWidth()-16)
If MouseDown(1) Then
fire()
EndIf
FlushKeys()'using flushkeys to simulate effect of auto-flush, remove to see difference
End Method
Method Draw()
SetColor 255,0,0
DrawRect(X-16, y-16, 32, 32)
End Method
Function Create:TPlayer(X:Float, Y:Float)
Local tempPlayer:TPlayer = New TPlayer
tempPlayer.X = X
tempPlayer.Y = Y
Return tempPlayer
End Function
End Type
SeedRnd MilliSecs()
Global Player1:TPlayer = TPlayer.Create(320,240)
While Not KeyDown(KEY_ESCAPE)
Cls
Player1.Update()
Player1.Draw()
TBullet.Handle()
Flip
Wend
End