In order to control the speed you are going to need to change your approach. The Thread is not a good regulator of time because it tells Java that it should sleep at a certain time. It doesn't guarantee that it will sleep at the time. It also depends on the OS on how it regulates... yadda, yadda. Yeah, it isn't good. Also, if you tell your Thread to sleep and have multiple objects all of them will act the same.
So... I don't think that this is a wise approach.
1 2 3 4 5
| if (canvas.isFire()) { Bullet player1Bullet = new Bullet(10,45, canvas.getImage(".\\images\\bullet.gif")); canvas.getBullets().add(player1Bullet); } Thread.sleep(1); |
The code that you would want to change the speed of the bullets with is not in the Thread. Choose a constant value for the thread and then, do the speed within the update.
1 2 3 4 5 6 7 8 9
| public void update(CanvasTest canvas) { if (getxPos() < 5 || getxPos() > Config.WIDTH-5) { canvas.getBullets().remove(this); this.rect = new Rectangle(getxPos(), getyPos(), 4,4); } else { setxPos(getxPos() + 1); rect.x += 1; } } |
setxPos(getxPos() + 1);
rect.x += 1;
Do you see the constant "1" in getxPos() + "1". If you make that "1" into an int variable, you can set the speed of it via pixels. That is a lot more accurate. If you make "1" into a double variable, you can make the bullet go faster and slower. But all your functions and storage will have to be doubles. Threads are really not very good at keeping different speeds for objects, but numbers are. Keep the Thread constant at 1 or 50 or whatever, and change the movement coordinates to control the speed.
Good Luck and hope this helps.