using a While loop to control the value of a keyframe in a timeline in Javafx
i want to create an animation where the ball will move in one of the 4 sectors of a circle randomly.this will happen 5 times.so,i create a while loop(i<5) and call the random function.i then create an if loop and attach some x and y values according to the random fn value.i then create an timeline object inside the while loop and in the key frame value,i use these x and y values.but when i run the program,what happens is that all the values are being created(x and y values seen through println) but only the last x and y value(for i=5)is being rendered in the screen.the animations for the previous values(1<=i<=4)are not being rendered.Please advise where have i gone wrong?
public function run(args:String[]) {
var i=0; while(i<5) { var z=gety(); println(z); // var relativeTime:Duration=0s; if(z==1) {xbind=120; ybind=80; } else if(z==2) {xbind=120; ybind=120; } else if(z==3) { xbind=80; ybind=120; } else if(z==4) { xbind=80; ybind=80; }
var t:Timeline=Timeline{
//time: bind pos with inverse;
repeatCount: Timeline.INDEFINITE
autoReverse: true
keyFrames:
[
KeyFrame{
time: 0s values: [ x => 100.0,y => 100.0]},
KeyFrame{time: 2s values:[x => xbind tween Interpolat开发者_运维知识库or.LINEAR,
y => ybind tween Interpolator.LINEAR,]
},
]
}//end timeline
i++;
t.play();
Thread.sleep(2000);
}//end while
}
Updating bound values for animation in a loop seems to be a relatively common anti-pattern. Updating bound values within a Timeline is a better solution. Study this post for a code fragment. Note also that using a Stage means you don't need to put logic in a run() function.
精彩评论