Basically, I’d add some faster memory-heap-safe-Threads to the code, e.g. your sprite uses an Image as the data-buffer and doesn’t make it softly allocated. I’d add a PhantomReference to it:
1.
2. import java.awt.Image;
3.
4. public class Sprite {
5.
6. //protected Animation anim;
7. private Image anim;
// private PhantomRef
private Reference phantom;
// private Ref. Queue
private ReferenceQueue<? extends Reference> refQueue = new ReferenceQueue<PhantomReference>();
8. // position (pixels)
9. public float x;
10. public float y;
11. // velocity (pixels per millisecond)
12. private float dx,dy;
13. private float lastdx,lastdy;
14. public static float zx;
15. public static float zy;
16. /**
17. Creates a new Sprite object with the specified Animation.
18. */
19. //public Sprite(Animation anim) {
20. public Sprite(Image anim) {
21. this(anim, new ReferenceQueue<PhantomReference>());
22. }
23.
/***/
public Sprite(Image anim, ReferenceQueue<? extends Reference> refQueue) {
this.anim = anim;
this.phantom = new PhantomReference(anim, refQueue);
this.refQueue = refQueue;
}
/***/
public void finalize() {
anim = null;
phantom.clear();
super.finalize();
}
24. /**
25. Updates this Sprite's Animation and its position based
26. on the velocity.
27. */
28.
29. public Image update(long elapsedTime) {
30. x += dx * elapsedTime;
31. y += dy * elapsedTime;
32. zx = x;
33. zy = y;
34. return anim;// = setX(x);//anim;//. update();
35. }
36.
37. public float getMaxSpeed() {
38. return 0.2f;
39. }
40.
41. /*
42. Wakes up the creature when the Creature first appears
43. on screen. Normally, the creature starts moving left.
44. */
45.
46. public void wakeUp() {
47. // if (getState() == STATE_NORMAL && getVelocityX() == 0) {
48. setVelocityX(-getMaxSpeed());
49. // }
50. }
51.
52. /**
53. Gets this Sprite's current x position.
54. */
55. public float getX() {
56. return x;
57. }
58.
59. /**
60. Gets this Sprite's current y position.
61. */
62. public float getY() {
63. return y;
64. }
65.
66. /**
67. Sets this Sprite's current x position.
68. */
69. public void setX(float x) {
70. this.x = x;
71. }
72.
73. /**
74. Sets this Sprite's current y position.
75. */
76. public void setY(float y) {
77. this.y = y;
78. }
79.
80. /**
81. Gets this Sprite's width, based on the size of the
82. current image.
83. */
84. public int getWidth() {
85. //return anim.getImage().getWidth(null);
86. return anim.getWidth(null);
87. }
88.
89. /**
90. Gets this Sprite's height, based on the size of the
91. current image.
92. */
93. public int getHeight() {
94. // return anim.getImage().getHeight(null);
95. return anim.getHeight(null);
96. }
97.
98. /**
99. Gets the horizontal velocity of this Sprite in pixels
100. per millisecond.
101. */
102. public float getVelocityX() {
103. return dx;
104. }
105.
106. /**
107. Gets the vertical velocity of this Sprite in pixels
108. per millisecond.
109. */
110. public float getVelocityY() {
111. return dy;
112. }
113.
114. /**
115. Sets the horizontal velocity of this Sprite in pixels
116. per millisecond.
117. */
118. public void setVelocityX(float dx) {
119. if (getVelocityX()!=0){
120. this.lastdx=this.dx;
121. }
122. this.dx = dx;
123.
124. }
125. public float getlastdx() {
126. return lastdx;
127. }
128.
129.
130. /**
131. Sets the vertical velocity of this Sprite in pixels
132. per millisecond.
133. */
134. public void setVelocityY(float dy) {
135. this.lastdy=this.dy;
136. this.dy = dy;
137. }
138.
139. public float getlastdy() {
140. return lastdy;
141. }
142.
143. /**
144. Gets this Sprite's current image.
145. */
146. public Image getImage() {
147. //return anim.getImage();
148. return anim;
149. }
150.
151. /**
152. Clones this Sprite. Does not clone position or velocity
153. info.
154. */
155.
156. public Object clone() {
157. return new Sprite(anim);
158. }
159.
160. }
That’s my point.
You may try this code and test it. That might be faster with larger amount of sprites. Notice the finalize meth that explicitly clears the buffer. It also may be linked with other Sprites for animation throughout a common ReferenceQueue given as contructor arg. : : :