Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[Java] Speeding the ball up.

Started by monp Sep 16, 2010 at 12:04 AM 1 replies 900+ views
Original Post
monp
monp
I have been learning java from some tutorials and I do not understand why the ball didn't move faster whenever I increase the value of speed. Any help will be appreciated.

import java.applet.*;import java.awt.*;public class Bounce extends Applet implements Runnable{	//variables	int x_pos = 10;	int y_pos = 100;	int radius = 20;	int speed = 4;	int appx = 300;			private Image dbImage;	private Graphics dbg;		public void init()	{		setBackground(Color.blue);	}	public void start() 	{		Thread th = new Thread(this);		th.start();	}	public void stop() {}	public void destroy() {}	public void run()	{		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);		while(true)		{			if(x_pos > appx - radius)			{				speed = -4;			}			else if(x_pos < radius) 			{				speed = +4;			}			x_pos += speed;			repaint();			try			{				Thread.sleep(10);			}			catch(InterruptedException ex)			{						}						Thread.currentThread().setPriority(Thread.MAX_PRIORITY);		}	}		public void update(Graphics g)	{		if (dbImage == null)		{			dbImage = createImage(this.getSize().width, this.getSize().height);			dbg = dbImage.getGraphics();		}				dbg.setColor(getBackground());		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);				dbg.setColor(getForeground());		paint(dbg);				g.drawImage(dbImage, 0, 0, this);	}				public void paint(Graphics g)	{		g.setColor(Color.red);		g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);	}	}	
BreathOfLife
BreathOfLife
		else if(x_pos < radius) 			{				speed = +4;			} 


From the looks of this, the speed does increase, but only until it hits 20, at which point it will not be smaller than radius, and will no longer be increase.

[edit] Im not sure on how long a frame is, but I am assuming that the 4 frames needed to get the speed to 20 are really short, which explains why the acceleration isnt noticable.
Zahlman
Zahlman
Hint:

speed = +4; is not the same thing as speed += 4;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.