How can I switch between two states(aminations) after a set interval and add transition animation

I just started learning flutter and flame, I am working on a simple platform game and I want add an enemy turtle with spikes in its shell. I want the spikes to go in and out after 3 seconds, and the main character can only unalive it after it’s spikes are in. How can do that.

I tried using the Future.delayed funtion but I cannot make it work

enum TurtleState { hit, idleSpikesIn, idleSpikesOut, spikesIn, spikesOut }

class Turtle extends SpriteAnimationGroupComponent
    with HasGameRef<PixelAdventure>, CollisionCallbacks {
  final double offNeg;
  final double offPos;
  Turtle({
    super.position,
    super.size,
    this.offPos = 0,
    this.offNeg = 0,
  });

  late final SpriteAnimation _hitAnimation;
  late final SpriteAnimation _idleSpikeInAnimation;
  late final SpriteAnimation _idleSpikeOutAnimation;
  late final SpriteAnimation _spikeOutAnimation;
  late final SpriteAnimation _spikeInAnimation;
  late final Player player;

  static const _bounceHeight = 260.0;
  static const stepTime = 0.05;
  static const tileSize = 16;

  final textureSize = Vector2(44, 26);

  double moveSpeed = 80;
  double moveDirection = -1;
  double rangeNeg = 0;
  double rangePos = 0;

  bool gotStomped = false;
  bool spikesIn = false;

  @override
  FutureOr<void> onLoad() {
    player = game.player;
    add(RectangleHitbox(
      position: Vector2.zero(),
      size: Vector2(44, 26),
    ));

    debugMode = true;

    _loadAllAnimations();
    _calculateRange();
    return super.onLoad();
  }

  @override
  void update(double dt) {
    if (!gotStomped) {
      _updateState();
      _movement(dt);
    }
    super.update(dt);
  }

  void _loadAllAnimations() {
    _idleSpikeOutAnimation = _spriteAnimation('Idle 1', 14);
    _hitAnimation = _spriteAnimation('Hit', 5)..loop = false;
    _idleSpikeInAnimation = _spriteAnimation('Idle 2', 8);
    _spikeOutAnimation = _spriteAnimation('Spikes out', 8)..loop = false;
    _spikeInAnimation = _spriteAnimation('Spikes in', 8)..loop = false;

    animations = {
      TurtleState.idleSpikesIn: _idleSpikeInAnimation,
      TurtleState.idleSpikesOut: _idleSpikeOutAnimation,
      TurtleState.hit: _hitAnimation,
      TurtleState.spikesOut: _spikeOutAnimation,
      TurtleState.spikesIn: _spikeInAnimation,
    };
    current = TurtleState.idleSpikesOut;
  }

  SpriteAnimation _spriteAnimation(String state, int amount) {
    return SpriteAnimation.fromFrameData(
      game.images.fromCache('Enemies/Turtle/$state (44x26).png'),
      SpriteAnimationData.sequenced(
        amount: amount,
        stepTime: stepTime,
        textureSize: textureSize,
      ),
    );
  }

  void _calculateRange() {
    rangeNeg = position.x - offNeg * tileSize;
    rangePos = position.x + width + offPos * tileSize;
  }

  void _updateState() {
    if ((moveDirection > 0 && scale.x > 0) ||
        (moveDirection < 0 && scale.x < 0)) {
      flipHorizontallyAroundCenter();
    }
    const spikeDuration = Duration(seconds: 3);
    Future.delayed(spikeDuration, () {
      current = TurtleState.idleSpikesOut;
      spikesIn = false;
    });
    Future.delayed(spikeDuration, () {
      spikesIn = true;
      current = TurtleState.spikesIn;
    });
  }

  void _movement(double dt) {
    if (position.x >= rangePos) {
      moveDirection = -1;
    } else if (position.x <= rangeNeg) {
      moveDirection = 1;
    }
    position.x += moveSpeed * moveDirection * dt;
  }

  void collidedWithPlayer() async {
    if (player.velocity.y > 0 && player.y + player.height > position.y) {
      gotStomped = true;
      current = TurtleState.hit;
      player.velocity.y = -_bounceHeight;
      await animationTicker?.completed;
      removeFromParent();
    } else {
      player.collidedWithEnemy();
    }
  }

New contributor

The Naqvi Games is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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