如何在Flutter Flame中逐帧运行精灵表?

huangapple go评论67阅读模式
英文:

How to run sprite sheet frame by frame in flutter flame?

问题

I wrote a code to play a sprite sheet frame by frame, but unfortunately, it's not playing correctly. It's moving to the right side. The sprites on the sprite sheet are dancing sprites, so when played frame by frame, it should show like a dancing animation video. I hope you understand what I mean. If you know how to resolve this issue and play the sprite sheet frame by frame, help me. Actually, I have no idea what the error is with the sprite sheet or code.

代码

class _GameScreenState extends State<GameScreen> {
  @override
  void initState() {
    SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
    super.initState();
  }
  late double width, height;
  @override
  Widget build(BuildContext context) {
    width = MediaQuery.of(context).size.width;
    height = MediaQuery.of(context).size.height;
    Size screenSize = MediaQuery.of(context).size;
    double aspectRatio = screenSize.height / screenSize.width;
    return Scaffold(
      backgroundColor: Colors.amber,
      body: AspectRatio(
        aspectRatio: aspectRatio,
        child: GameWidget(
          game: Game(context),
        ),
      ),
    );
  }
}

class Game extends FlameGame {
  BuildContext context;

  Game(this.context);

  SpriteAnimationComponent animationComponent1 =
  SpriteAnimationComponent(size: Vector2(840, 440));

  @override
  Color backgroundColor() => const Color(0xFFFFC107);

  @override
  Future<void>? onLoad() async {
    super.onLoad();
    await loadAnimations();
    add(animationComponent1);

    animationComponent1.position = Vector2(1, 1);
  }

  Future<void> loadAnimations() async {
    SpriteSheet spriteSheet =
    SpriteSheet(image: await images.load('spritesheet.png'),  srcSize: Vector2(1226, 1226));
    SpriteAnimation spriteAnimation =
    spriteSheet.createAnimation(row: 0, stepTime: 0.7, from: 5, to: 24);

    animationComponent1.animation = spriteAnimation;
  }
}

精灵表图像链接

https://drive.google.com/file/d/1obZBPhgn2UMhaBvXBvW3dkg7I02a2ksJ/view?usp=sharing

UI截图
如何在Flutter Flame中逐帧运行精灵表?

更新的代码视频链接

https://drive.google.com/file/d/1y6gm77-OkLkeJTpv9AGE1sgodqANO_nl/view?usp=share_link
英文:

I wrote a code to play a sprite sheet frame by frame, but unfortunately, it's not playing correctly. It's moving to the right side. The sprites on the sprite sheet are dancing sprites, so when played frame by frame, it should show like an dancing animation video. I hope you understand what I mean . If you know how to resolve this issue and play the sprite sheet frame by frame, help me. Actually, I have no idea what the error is with the sprite sheet or code.

code

class _GameScreenState extends State&lt;GameScreen&gt; {
  @override
  void initState() {
    SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
    super.initState();
  }
  late double width, height;
  @override
  Widget build(BuildContext context) {
    width = MediaQuery.of(context).size.width;
    height = MediaQuery.of(context).size.height;
    Size screenSize = MediaQuery.of(context).size;
    double aspectRatio = screenSize.height / screenSize.width;
    return Scaffold(
      backgroundColor: Colors.amber,
      body: AspectRatio(
        aspectRatio: aspectRatio,
        child: GameWidget(
          game: Game(context),
        ),
      ),
    );
  }
}

class Game extends FlameGame {
  BuildContext context;

  Game(this.context);

  SpriteAnimationComponent animationComponent1 =
  SpriteAnimationComponent(size: Vector2(840, 440));


  @override
  Color backgroundColor() =&gt; const Color(0xFFFFC107);

  @override
  Future&lt;void&gt;? onLoad() async {
    super.onLoad();
    await loadAnimations();
    add(animationComponent1);

    animationComponent1.position = Vector2(1, 1);

  }

  Future&lt;void&gt; loadAnimations() async {
    SpriteSheet spriteSheet =
    SpriteSheet(image: await images.load(&#39;spritesheet.png&#39;),  srcSize: Vector2(1226, 1226));
    SpriteAnimation spriteAnimation =
    spriteSheet.createAnimation(row: 0, stepTime: 0.7, from: 5, to: 24);

    animationComponent1.animation = spriteAnimation;
  }
}

sprite sheet image url

https://drive.google.com/file/d/1obZBPhgn2UMhaBvXBvW3dkg7I02a2ksJ/view?usp=sharing

screenshot of UI
如何在Flutter Flame中逐帧运行精灵表?

update code video url

https://drive.google.com/file/d/1y6gm77-OkLkeJTpv9AGE1sgodqANO_nl/view?usp=share_link

答案1

得分: 1

class _GameScreenState extends State<GameScreen> {
  @override
  void initState() {
    SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
    super.initState();
  }
  late double width, height;
  @override
  Widget build(BuildContext context) {
    width = MediaQuery.of(context).size.width;
    height = MediaQuery.of(context).size.height;
    Size screenSize = MediaQuery.of(context).size;
    double aspectRatio = screenSize.height / screenSize.width;
    return Scaffold(
      backgroundColor: Colors.amber,
      body: AspectRatio(
        aspectRatio: aspectRatio,
        child: GameWidget(
          game: Game(context, screenSize),
        ),
      ),
    );
  }
}

class Game extends FlameGame {
  BuildContext context;
  Size screenSize;
  Game(this.context, this.screenSize);

  SpriteAnimationComponent animationComponent1 =
      SpriteAnimationComponent(size: Vector2(840, 440));

  @override
  Color backgroundColor() => const Color(0xFFFFC107);

  @override
  Future<void>? onLoad() async {
    super.onLoad();
    await loadAnimations();
    add(animationComponent1);
    //new code
    animationComponent1.position = Vector2(
      screenSize.width / 2 - animationComponent1.size.x / 2,
      screenSize.height / 2 - animationComponent1.size.y / 2,
    );
  }

  Future<void> loadAnimations() async {
    SpriteSheet spriteSheet = SpriteSheet(
        image: await images.load('spritesheet.png'),
        srcSize: Vector2(1226, 1226));
    SpriteAnimation spriteAnimation =
        spriteSheet.createAnimation(row: 0, stepTime: 0.7, from: 5, to: 24);

    animationComponent1.animation = spriteAnimation;
  }
}
英文:
    class _GameScreenState extends State&lt;GameScreen&gt; {
      @override
      void initState() {
        SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
        super.initState();
      }
      late double width, height;
      @override
      Widget build(BuildContext context) {
        width = MediaQuery.of(context).size.width;
        height = MediaQuery.of(context).size.height;
        Size screenSize = MediaQuery.of(context).size;
        double aspectRatio = screenSize.height / screenSize.width;
        return Scaffold(
          backgroundColor: Colors.amber,
          body: AspectRatio(
            aspectRatio: aspectRatio,
            child: GameWidget(
              game: Game(context,screenSize),
            ),
          ),
        );
      }
    }
    
    class Game extends FlameGame {
  BuildContext context;
  Size screenSize;
  Game(this.context, this.screenSize);

  SpriteAnimationComponent animationComponent1 =
      SpriteAnimationComponent(size: Vector2(840, 440));

  @override
  Color backgroundColor() =&gt; const Color(0xFFFFC107);

  @override
  Future&lt;void&gt;? onLoad() async {
    super.onLoad();
    await loadAnimations();
    add(animationComponent1);
//new code
    animationComponent1.position = Vector2(
      screenSize.width / 2 - animationComponent1.size.x / 2,
      screenSize.height / 2 - animationComponent1.size.y / 2,
    );
  }

  Future&lt;void&gt; loadAnimations() async {
    SpriteSheet spriteSheet = SpriteSheet(
        image: await images.load(&#39;spritesheet.png&#39;),
        srcSize: Vector2(1226, 1226));
    SpriteAnimation spriteAnimation =
        spriteSheet.createAnimation(row: 0, stepTime: 0.7, from: 5, to: 24);

    animationComponent1.animation = spriteAnimation;
  }
}

huangapple
  • 本文由 发表于 2023年5月7日 17:32:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定