I'm not just looking for someone to help me figure out why its jumping and not counting up.
Well, it would be too hard for me to figure out anyway.
You have two options as I see it
Sin function (better)
If your top is 465, and your bottom is 20, then your amplitude should be (465 - 20) / 2, or 222.5. Your y-intersect should be your amplitude plus the bottom, so 242.5. Declare a double t
, and do this in your update:
public void update() {
paddleLocation.y = (int)((222.5) * Math.sin(t) + (242.5))
t += Math.PI * .01; // This number is arbitrary
}
Fix your code (I probably missed something, but try it out)
Your code is really sloppy and unintuitive, sorry. But I'll try my best to direct you.
I think the problem is that you have an unnecessary loop inside of your update
method. This is causing the "jump". The update
method should be called frequently, and you're just acting on one variable here, so you don't need the loop. Instead, try this:
import java.awt.Point;
import javax.swing.ImageIcon;
public class CharlesPaddle extends Paddle {
/************************************************** ***************/
public CharlesPaddle(int _x) {
super(_x);
ImageIcon iiPaddle = new ImageIcon(this.getClass().getResource("paddle.png"));
paddleImage = iiPaddle.getImage();
}
/************************************************** ***************/
public void update() {
goingUp = updateDirection(goingUp);
if (goingUp)
paddleLocation.y += 1;
else
paddleLocation.y -= 1;
System.out.println("y position: " + paddleLocation.y);
}
/************************************************** ***************/
public void init() {
paddleLocation = new Point();
paddleVector = new Point();
paddleVector.x = 0;
paddleVector.y = speed;
paddleLocation.x = xLocation;
paddleLocation.y = 200;
}
/************************************************** ***************/
public boolean updateDirection(boolean goingUp) // This name makes more sense
{
if (goingUp && paddleLocation.y >= 465) // It's just peaked,
return false; // so return false (going down)
else if (!goingUp && paddleLocation.y <= 20) // It's just gone to the bottom,
return true; // so return true (going up)
else // It's somewhere in between,
return goingUp; // so don't change anything!
}
}
Either of those should work.
I renamed your methods and variables because they didn't make sense. I also removed some of your redundant commenting.
In your main method, you should then call this update() repeatedly with either a for or while loop. You can also print out the paddleLocation in your update method, or do whatever you want with it.