GithubHelp home page GithubHelp logo

aris_flamesprite's Introduction

Flutter Flame Sprite Test

Collision Detection of Animated Sprites

  • Sprite on the left is a static sprite.
  • The two sprites on the right are using SpriteAnimationComponent.

Sprite Animation

Future<void> onLoad() async {
  ...
    var flappySheet = await images.load('blue_bird_sheet.png');
    final flappySpriteSize = Vector2(100.0, 79.0);
    SpriteAnimationData flappySpriteData = SpriteAnimationData.sequenced(
        amount: 6, stepTime: 0.2, textureSize: Vector2(100, 79));
    flappyBird.size = flappySpriteSize;
    flappyBird.animation =
        SpriteAnimation.fromFrameData(flappySheet, flappySpriteData);
    add(flappyBird);

Animated Sprite

The animation below was created in the onLoad method.

screenshot

load and display sprite

  • sprite is loaded from image file with loadSprite('imageFileName')
  • SpriteComponent is instantiated above onLoad to make it accessible in update
class BirdGame extends BaseGame {
  Bird bird = Bird();
  @override
  Future<void> onLoad() async {
    var birdSprite = await loadSprite('bird.png');
    // pass sprite and screensize
    add(bird
      ..screensize = size
      ..sprite = birdSprite);
  }

collision detection and remove

collision

The bird and ship sprites are instantiated in the main game class.

  @override
  void update(double dt) {
    super.update(dt);
    if (bird.toRect().contains(ship.toRect().center)) {
      print('bird collided with ship and is dead ');
      remove(bird);
    }
  }

animation from spritesheet

animation

final spriteSize = Vector2(100.0, 88.0);
SpriteAnimationData spriteData = SpriteAnimationData.sequenced(
    amount: 2, stepTime: 0.3, textureSize: Vector2(100, 88));
final yellowFlappingAnimation = SpriteAnimationComponent.fromFrameData(
    spriteSize, spriteSheet, spriteData)
    ..x = 100
    ..y = 350;
add(yellowFlappingAnimation);

Notes

For the whole frames you can just create a list with the elements in it like so:

final frames = <SpriteAnimationFrameData>[
  SpriteAnimationFrameData(srcPosition: Vector2(0,0), srcSize: Vector2(16,36), stepTime: 0.2),
  SpriteAnimationFrameData(srcPosition: Vector2(16,0), srcSize: Vector2(16,36), stepTime: 0.2),
  SpriteAnimationFrameData(srcPosition: Vector2(32,0), srcSize: Vector2(16,36), stepTime: 0.2),
  ...
  ...
];

To move the instance out of the GameWidget you can do:

void main() {
  final myRealGame = MyRealGame();
  runApp(
    GameWidget(
      game: myRealGame,
    ),
  );
}

Sprite and text transparency Try setting a custom Paint in SpriteComponent to this.overridePaint = myPaint;

Version

  • Using flame: ^1.0.0-rc6 as of 1/24/2021

aris_flamesprite's People

Watchers

Moshennik  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.