pub struct Animation { /* private fields */ }
Expand description

An animation, cycling between regions of a texture at a regular interval.

Calling advance or advance within State::draw will drive the animation, switching the texture region once the specified time has passed.

Examples

The animation example demonstrates basic usage of an Animation with a spritesheet.

The animation_controller example demonstrates how multiple Animations can be combined using a simple state machine.

Implementations§

source§

impl Animation

source

pub fn new( texture: Texture, frames: Vec<Rectangle>, frame_length: Duration ) -> Animation

Creates a new looping animation.

source

pub fn once( texture: Texture, frames: Vec<Rectangle>, frame_length: Duration ) -> Animation

Creates a new animation that does not repeat once all of the frames have been displayed.

source

pub fn draw<P>(&self, ctx: &mut Context, params: P)where P: Into<DrawParams>,

Draws the current frame to the screen (or to a canvas, if one is enabled).

source

pub fn advance(&mut self, ctx: &Context)

Advances the animation’s timer, switching the texture region if required.

This method uses the current delta time to calculate how much time has passed.

source

pub fn advance_by(&mut self, duration: Duration)

Advances the animation’s timer by a specified amount, switching the texture region if required.

If the specified duration is longer than the frame length, frames will be skipped.

source

pub fn restart(&mut self)

Restarts the animation from the first frame.

source

pub fn texture(&self) -> &Texture

Returns a reference to the texture currently being used by the animation.

source

pub fn set_texture(&mut self, texture: Texture)

Sets the texture that will be used by the animation.

This method will not change the frame definitions or current state of the animation, so it can be used for e.g. swapping spritesheets. If you need to change the slicing for the new texture, call set_frames.

source

pub fn frames(&self) -> &[Rectangle]

Gets the sections of the texture being displayed for each frame of the animation.

source

pub fn set_frames(&mut self, new_frames: Vec<Rectangle>)

Sets the sections of the texture being displayed for each frame of the animation.

This method will reset the animation back to frame zero.

source

pub fn frame_length(&self) -> Duration

Gets the amount of time that each frame of the animation lasts for.

source

pub fn set_frame_length(&mut self, new_frame_length: Duration)

Sets the amount of time that each frame of the animation lasts for.

source

pub fn repeating(&self) -> bool

Gets whether or not the animation is currently set to repeat when it reaches the end of the frames.

source

pub fn set_repeating(&mut self, repeating: bool)

Sets whether or not the animation should repeat when it reaches the end of the frames.

source

pub fn current_frame_index(&self) -> usize

Gets the index of the frame that is currently being displayed.

This index is zero-based, and can be used in combination with the frames method in order to track the progress of the animation.

source

pub fn set_current_frame_index(&mut self, index: usize)

Sets which frame of the animation should be displayed.

Usually you will want to control the animation by calling advance or advance, but this method can be useful for more fine-grained control.

Panics

The index is zero-based, and must be within the bounds of the animation’s frames, otherwise this method will panic.

source

pub fn current_frame_time(&self) -> Duration

Gets the duration that the current frame has been visible.

This can be used in combination with the frame_length method in order to track the progress of the animation.

source

pub fn set_current_frame_time(&mut self, duration: Duration)

Sets the duration that the current frame has been visible.

Usually you will want to control the animation by calling advance or advance,but this method can be useful for more fine-grained control.

The animation will not advance past the end of the current frame until the next call to advance or advance. If a value is given that is larger than frame_length, this animation may skip frames.

source

pub fn is_finished(&self) -> bool

Returns true if this animation will no longer advance.

Will always be false for repeating animations.

source

pub fn has_frames_remaining(&self) -> bool

Returns true if there are any frames remaining in the current cycle.

Trait Implementations§

source§

impl Clone for Animation

source§

fn clone(&self) -> Animation

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Animation

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,