Git Product home page Git Product logo

openfl-aseprite's Introduction

MIT License Haxelib Version

OpenFL Aseprite

Load and render sprites and animations in Aseprite format. Based on ase library.

Demo 1 Source

Demo 2 Source

API Documentation

Installation

  1. Install the library with haxelib:
haxelib install openfl-aseprite
  1. Add this to the project.xml file:
 <haxelib name="openfl-aseprite" />

Usage

The main class of the library (aseprite.Aseprite) extends openfl.display.Sprite so you can use it just like any other OpenFL sprite.

import aseprite.Aseprite;
import openfl.Assets;

// <...>

var sprite:Aseprite = Aseprite.fromBytes(Assets.getBytes('path/to/asepriteAsset.aseprite'));

addChild(sprite);

Create a new sprite reusing the resources:

var sprite:Aseprite = Aseprite.fromBytes(Assets.getBytes('path/to/asepriteAsset.aseprite'));
var newSprite:Aseprite = sprite.spawn();

newSprite will share the bitmap data from the original sprite thus saving memory and time used to parse the aseprite file data

spawn method can also be used to create new sprites from slices:

var newSprite:Aseprite = sprite.spawn('Slice Name');

By default an Aseprite instance adds an ENTER_FRAME listener to advance the animation automatically. If you don't want it you can pass false as a second argument of the constructor or the fromBytes method. To advance the animation manually use advance or nextFrame methods or currentFrame property;

var sprite:Aseprite = Aseprite.fromBytes(Assets.getBytes('path/to/asepriteAsset.aseprite'), false); // Won't add an `ENTER_FRAME` listener

sprite.advance(300); // Advance the animation by 300 milliseconds

sprite.nextFrame(); // Go to the next frame of the animation

sprite.currentFrame = 32; // Explicitly set the current frame of the animation

Playback and tags

sprite.play(); // If no tag specified - play from the first frame to the end of the animation

sprite.play(3); // Play the animation 3 times

/**
   Play animation once with `onFinish` callback
 */
sprite.play(1, () -> {
  trace('Animation is finished');
});

sprite.play('tag_name'); // Start playing a specific tag if it's not playing already

sprite.play('tag_name', 3); // Play `tag_name` tag 3 times

/**
  Play `tag_name` tag 3 times with an `onFinish` callback
 */
sprite.play('tag_name', 3, () -> {
  trace('Playback is finished after 3 repeats');
});

sprite.pause(); // Pauses the playback

sprite.stop(); // Stops the playback and moves the playhead to the first frame of the animation or the current tag;

NOTE: The methods above only make sense if useEnterFrame = true

for(tag in sprite.tags) { // Loop through the animation tags
  trace(tag.name); // Print tag's name
}

sprite.currentTag = 'tag_name'; // Change the current tag. If other tag is currently playing - go to the first frame of the tag and continue playing. If not playing - only moves the playhead to the first frame of the tag.

sprite.currentTag = null; // Reset the current tag. Will play from the very first to the very last frame of the animation

Control the playback direction

import ase.AnimationDirection;

// <...>

sprite.direction = AnimationDirection.FORWARD; // Play from the first frame to the last one
sprite.direction = AnimationDirection.REVERSE; // Play from the last frame to the first one
sprite.direction = AnimationDirection.PING_PONG; // Play the animation back and forth

/**
  It's not necessary to use the constants from the `AnimationDirection` class. You can use integers instead:

  FORWARD = 0
  REVERSE = 1
  PING_PONG = 2
 **/

NOTE: Will override the playback direction of the current tag

Dox Documentation

From the project's directory:

$ cd scripts
$ haxe doc.hxml

HTML documentation fill be generated in the documentation directory in the project's directory

Author

Michael Miriti

License

This project is licensed under the MIT License - see LICENSE.md for details

openfl-aseprite's People

Contributors

miriti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

openfl-aseprite's Issues

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.