singlelist.bmx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ' Adding objects to an object-specific list...
  2. Type Particle
  3. Global ParticleList:TList ' The list for all objects of this type...
  4. Global Gravity# = 0.1
  5. Field x#
  6. Field y#
  7. Field xs#
  8. Field ys#
  9. ' The New method is called whenever one of these objects is created. If
  10. ' the list hasn't yet been created, it's created here. The object is then
  11. ' added to the list...
  12. Method New ()
  13. If ParticleList = Null
  14. ParticleList = New TList
  15. EndIf
  16. ParticleList.AddLast Self
  17. End Method
  18. Function Create:Particle (x, y)
  19. p:Particle = New Particle
  20. p.x = x
  21. p.y = y
  22. p.xs = Rnd (-4, 4)
  23. p.ys = 0
  24. Return p
  25. End Function
  26. Function UpdateAll ()
  27. ' Better check the list exists before trying to use it...
  28. If ParticleList = Null Return
  29. ' Iterate through list...
  30. For p:Particle = EachIn ParticleList
  31. p.ys = p.ys + Gravity
  32. p.x = p.x + p.xs
  33. p.y = p.y + p.ys
  34. DrawRect p.x, p.y, 8, 8
  35. If p.y > GraphicsHeight () p = Null
  36. Next
  37. End Function
  38. End Type
  39. ' D E M O . . .
  40. Graphics 640, 480
  41. Repeat
  42. Cls
  43. ' Create a Particle every now and then...
  44. If Rand (100) > 50
  45. p:Particle = Particle.Create (MouseX (), MouseY ())
  46. EndIf
  47. ' Update all Particle objects...
  48. Particle.UpdateAll ()
  49. Flip
  50. Until KeyHit (KEY_ESCAPE)
  51. End