So I create game and I want to make movement of the enemy. The simple movement for now is just going right-left all the time. I want to make it in a way that I put next move into array so I don't want to make infinite loop but rather a mechanism of applying new animations.
AnimationController enemy;
Animation _enemyAnimation;
void initEnemyAnim(double from, double to, Duration in_time ) {
enemy = AnimationController(
vsync: this,
duration: in_time);
_enemyAnimation = Tween<double>(begin: from, end: to).animate(CurvedAnimation(parent: enemy, curve: Curves.linear));
_enemyAnimation.addListener(() {
_changeMove(_enemyAnimation.value); // this function steers my character position
});
}
void runAi() async {
double from = 0.0;
double to = 800.0;
await Future.delayed(Duration(milliseconds: 8200)); // it's intro, doesnt matter
const oneSec = Duration(seconds:1);
double temp = 0.0;
initEnemyAnim(from, to, Duration(milliseconds: 1000));
enemy.forward();
temp=to;
to=from;
from=temp;
Timer.periodic(oneSec, (Timer t) {
debugPrint("run $_enemyAnimation.value");
enemy.reset();
initEnemyAnim(from, to, Duration(milliseconds: 1000));
enemy.forward();
temp=to;
to=from;
from=temp;
});
}
the position of enemy is range between 0 and 800.
I use TickerProviderStateMixin for AnimationController
for delegating a movement I use Timer.periodic.
The thing is that every 2 periodic actions there is like 200milliseconds(or more actually) of pause.
My enemy is moving towards right at start. It's allright. It's going left and instantly its goin right. Excellent. Then it is some kind of pause. I don't know why It behaves like windshield wipers. It's going left right pause left right pause.
I want to fix it in a way that it moves left right left right ...
When I use to add next animation using
_enemyAnimation.addStatusListener((status) {
if(status == AnimationStatus.completed){ }
}
It also has this lag
Do you have idea how to chain and add up to animation in a way that there is no break between them ?