render method
精灵渲染
Implementation
@override
void render(Canvas canvas) {
/// 画布暂存
canvas.save();
/// 将子精灵转换为相对坐标
if (parent == null) {
canvas.translate(position.x, position.y);
} else {
canvas.translate(position.x - parent!.size.width / 2, position.y - parent!.size.height / 2);
}
/// 精灵矩形边界
/// var paint = new Paint()..color = Color(0x6000FF00);
/// canvas.drawRect(Rect.fromLTWH(- size.width/2,- size.height/2, size.width, size.height), paint);
/// 渲染动画帧
if (this.frames[this.currentAnimation] != null && this.frames[this.currentAnimation]!.length > 0) {
List<DFImageSprite> sprites = this.frames[this.currentAnimation]!;
sprites[this.currentIndex].flippedX = currentAnimationFlippedX;
sprites[this.currentIndex].color = color;
sprites[this.currentIndex].render(canvas);
}
/// 渲染子精灵
if (children.length > 0) {
children.forEach((sprite) {
sprite.render(canvas);
});
}
/// 渲染绑定精灵
bindSprites.forEach((sprite) {
if (sprite.visible &&
sprite.frames[this.currentAnimation] != null &&
this.frames[this.currentAnimation] != null) {
/// 绑定精灵帧数不一定完全对应 所以这里计算一下接近值,保证速度和最后动画结束时间一致
double offset = sprite.frames[this.currentAnimation]!.length / this.frames[this.currentAnimation]!.length;
sprite.currentIndex = (this.currentIndex * offset).round();
sprite.currentAnimation = this.currentAnimation;
sprite.currentAnimationFlippedX = currentAnimationFlippedX;
sprite.render(canvas);
}
});
/// 画布恢复
canvas.restore();
}