Hi guys. I’m trying to get my head around TLists and I think I’ve got the basics. But I’m not sure how to handle one object in the list or even a range of objects? Here is my example, this code creates 1000 particles on the screen and draws a line from point 500, 500 to each one. You can use the arrow keys to move the particles left and right. My question is how I edit my code so I can draw a line to just particle 100 or a range of particles 100 to 200. Maybe even use the up and down arrow keys to select a range. Hope you can help me!
' Adding objects to an object-specific list...
Global sp
Type Particle
Global ParticleList:TList ' The list for all objects of this type...
Field x#
Field y#
' The New method is called whenever one of these objects is created. If
' the list hasn't yet been created, it's created here. The object is then
' added to the list...
Method New ()
If ParticleList = Null
ParticleList = New TList
EndIf
ParticleList.AddLast Self
End Method
Function Create:Particle ()
p:Particle = New Particle
p.x = Rnd (1000)
p.y = Rnd (1000)
End Function
Function UpdateAll ()
' Better check the list exists before trying to use it...
If ParticleList = Null Return
' Run through list...
For p:Particle = EachIn ParticleList
p.x=p.x+sp
DrawLine 500,500,p.x,p.y
DrawRect p.x, p.y,8,8
If p.y > GraphicsHeight () p = Null
Next
End Function
End Type
Graphics 1024, 768
' Create 1000 Particles...
For loop=1 To 1000
p:Particle = Particle.Create ()
Next
Repeat
Cls
' Update all Particle objects...
Particle.UpdateAll ()
If KeyDown(KEY_RIGHT) Then sp=sp+1
If KeyDown(KEY_LEFT) Then sp=sp-1
If sp>5 Then sp=5
If sp<-5 Then sp=-5
Flip
Until KeyHit (KEY_ESCAPE)
End
|