Flutter Flame Game: Character Stays in ‘up’ Position After Restarting Game

I am developing a game using Flutter and Flame. In my game, the character has two states: middle and up. The character moves to the up state when the user initiates a jump and returns to the middle state after a delay.

The problem I am facing is that when the game ends due to a collision while the character is in the up state, and I restart the game, the character continues from the up state instead of resetting to the middle state.

Here is my code:

import 'dart:async';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/cupertino.dart';
import 'package:rungame/game/assets.dart';
import 'package:rungame/game/configuration.dart';
import 'package:rungame/game/skateboardera_girl.dart';
import 'package:rungame/game/skater_girl_movement.dart';

class SkaterGirl extends SpriteGroupComponent<SkaterGirlMovement>
    with HasGameRef<skateboarderGirl>, CollisionCallbacks {
  SkaterGirl();
  int score = 0;

  @override
  Future<void> onLoad() async {
    final skaterGirlCharacter = await gameRef.loadSprite(Assets.skatergirl);
    final skaterGirlCharacterUp = await gameRef.loadSprite(Assets.skatergirlUp);
    FlameAudio.audioCache
        .loadAll([Assets.flying, Assets.collision, Assets.point]);

    size = Vector2(120, 120);
    double bottomPadding = 190;
    position = Vector2(120, gameRef.size.y - size.y - bottomPadding);

    current = SkaterGirlMovement.middle;
    sprites = {
      SkaterGirlMovement.middle: skaterGirlCharacter,
      SkaterGirlMovement.up: skaterGirlCharacterUp,
    };

    add(RectangleHitbox());
  }

  void fly() {
    if (current != SkaterGirlMovement.up && !gameRef.isHit) {
      try {
        FlameAudio.play(Assets.flying);
        debugPrint('çaldı canım');
      } catch (e) {
        debugPrint('Error playing audio: $e');
      }
      add(
        MoveByEffect(
          Vector2(0, Config.gravity),
          EffectController(duration: 0.5, curve: Curves.decelerate),
          onComplete: () {
            if (current != SkaterGirlMovement.up && !gameRef.isHit) {
              current = SkaterGirlMovement.up;
              debugPrint('if up tan sonra ki ');
              Future.delayed(Duration(milliseconds: 1500), () {
                if (!gameRef.isHit) {
                  debugPrint("if game is hit -->  ${game.isHit}");
                  resetPosition();
                  current = SkaterGirlMovement.middle;
                  gameRef.skatergirl.score++;
                  try {
                    FlameAudio.play(Assets.point);
                    debugPrint('çaldı point');
                  } catch (e) {
                    debugPrint('Error playing audio: $e');
                  }
                  debugPrint("Score fly file: ${gameRef.skatergirl.score}");
                } else {
                  resetPosition();
                  return;
                }
              });
            }
          },
        ),
      );
    } else {
      debugPrint('Collision detected while flying, exiting fly function');
      current = SkaterGirlMovement.middle;
      return;
    }
  }

  void resetPosition() {
    debugPrint('reset position ');
    size = Vector2(120, 120);
    double bottomPadding = 190;
    position = Vector2(
      120,
      gameRef.size.y - size.y - bottomPadding,
    );
    current = SkaterGirlMovement.middle; // Orta pozisyon olarak ayarla
  }

  @override
  void onCollisionStart(
    Set<Vector2> intersectionPoints,
    PositionComponent other,
  ) {
    super.onCollisionStart(intersectionPoints, other);
    debugPrint('Collision detected ');

    gameOver();
  }

  void gameOver() {
    try {
      FlameAudio.play(Assets.collision);
      debugPrint('çaldı game over');
    } catch (e) {
      debugPrint('Error playing audio: $e');
    }

    gameRef.overlays.add('gameOver');
    gameRef.pauseEngine();
    game.isHit = true;
    resetPosition(); // Oyun bittiğinde karakteri orta pozisyona sıfırla

    debugPrint("game is hit -->  ${game.isHit}");
  }

  void reset() {
    gameRef.flowerGroup.resetFlowers();
    current = SkaterGirlMovement.middle;
    score = 0;
  }
}

I’ve tried to ensure that the character’s position and state are reset to middle in both resetPosition() and reset(), but the issue persists. How can I ensure that the character always starts in the middle state when the game restarts?

Any help would be greatly appreciated. Thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật