了解画布的动画

时间:2019-01-22 00:03:19

标签: dart flutter

我是Dart和Flutter的初学者

我正在尝试制作一款游戏,可以帮助我理解这两种语言的许多功能。

我有一个画圆的屏幕,我想从屏幕的左向右平移它

class _GamePagePageState extends State<GamePage> with TickerProviderStateMixin {
    @override
    void initState() {
        super.initState();
        leftToRightTranslation(100);
    }
    void leftToRightTranslation(int speed) {
        new Timer.periodic(new Duration(milliseconds: speed), (timer) {
            bulle.xPos++;
        });
     }
}

class MyPainter extends CustomPainter{
    Color lineColor;
    double width;
    double xPos = 50, yPos = 50;
    MyPainter({this.lineColor,this.width});
    @override
    void paint(Canvas canvas, Size size) {
        Paint line = new Paint()
        ..color = lineColor
        ..strokeCap = StrokeCap.round
        ..style = PaintingStyle.stroke
        ..strokeWidth = width;
        Offset offset = new Offset(xPos, yPos);
        double radius  = 1;
        canvas.drawCircle(
            offset,
            radius,
            line
         );
     }
     @override
     bool shouldRepaint(MyPainter oldDelegate) {
         return oldDelegate.xPos != xPos;
     }
}

这是结果:

enter image description here

从左到右的翻译不会发生

我认为shouldRepaint方法必须在其变化时更新牛头位置。

当我执行热装弹时,布勒消失了,这意味着x位置实际上已经改变并且变得大于屏幕宽度

如何从屏幕的左侧到右侧进行翻译?

任何其他实现方法将不胜感激。

谢谢

0 个答案:

没有答案