我试图在一个单独的类中使用动画循环每10毫秒移动一次射弹。这个循环的前两次迭代打印出重力等于0,因为它应该只能在第一个循环上为零,因为时间= 0开始。
public static void move()
{
System.out.println( "Time: " + time );
System.out.println( "gravity: " + gravity );
System.out.println( "g: " + g );
dX = Slingshot.getPullX();
dY = Slingshot.getPullY();
gravity = 0.5 * g * time * time;
newX = (double) Slingshot.getStart().x + time * c * dX;
newY = (double) Slingshot.getStart().y + ( time * c * dY + gravity );
time = time + 1;
new Projectile( newX, newY, dX, dY );
trail = new Ellipse( ( int ) newX + 5, ( int ) newY + 5 );
trail.setColor( Color.RED );
trail.setSize( 2, 2 );
projectileTrail.add( trail );
}
以下是循环几次迭代后的输出:
Time: 0.0
gravity: 0.0
g: 2.0
Time: 1.0
gravity: 0.0
g: 2.0
Time: 2.0
gravity: 1.0
g: 2.0
Time: 3.0
gravity: 4.0
g: 2.0
Time: 4.0
gravity: 9.0
g: 2.0
Time: 5.0
gravity: 16.0
g: 2.0
答案 0 :(得分:2)
您在计算结果之前打印结果,因此第二个循环显示您在第一个循环中计算的结果。
将println
移动到循环的末尾。