/// <summary> /// returns a SpriteAnimation given an animationName where the animationName is the region's "filename" metadata /// in the TexturePacker atlas minus the framenumbers at the end /// </summary> /// <returns>The sprite animation.</returns> /// <param name="animationName">Animation name.</param> public SpriteAnimation getSpriteAnimation( string animationName ) { // create the cache Dictionary if necessary. Return the animation direction if already cached. if( _spriteAnimations == null ) _spriteAnimations = new Dictionary<string,SpriteAnimation>(); else if( _spriteAnimations.ContainsKey( animationName ) ) return _spriteAnimations[animationName]; if( spriteAnimationDetails.ContainsKey( animationName ) ) { var frames = spriteAnimationDetails[animationName]; var animation = new SpriteAnimation { fps = _animationFPS }; for( var i = 0; i < frames.Count; i++ ) animation.addFrame( subtextures[frames[i]] ); _spriteAnimations[animationName] = animation; return animation; } throw new KeyNotFoundException( animationName ); }
/// <summary> /// returns a SpriteAnimation given an animationName where the animationName is the folder that contains the images /// </summary> /// <returns>The sprite animation.</returns> /// <param name="animationName">Animation name.</param> public SpriteAnimation getSpriteAnimation( string animationName ) { Point point; if( _spriteAnimationDetails.TryGetValue( animationName, out point ) ) { var animation = new SpriteAnimation { fps = _animationFPS }; for( var i = point.X; i <= point.Y; i++ ) animation.addFrame( subtextures[i] ); return animation; } throw new KeyNotFoundException( animationName ); }
void IUpdatable.update() { if (_currentAnimation == null || !isPlaying) { return; } // handle delay if (!_delayComplete && _elapsedDelay < _currentAnimation.delay) { _elapsedDelay += Time.deltaTime; if (_elapsedDelay >= _currentAnimation.delay) { _delayComplete = true; } return; } // count backwards if we are going in reverse if (_isReversed) { _totalElapsedTime -= Time.deltaTime; } else { _totalElapsedTime += Time.deltaTime; } _totalElapsedTime = Mathf.clamp(_totalElapsedTime, 0f, _currentAnimation.totalDuration); _completedIterations = Mathf.floorToInt(_totalElapsedTime / _currentAnimation.iterationDuration); _isLoopingBackOnPingPong = false; // handle ping pong loops. if loop is false but pingPongLoop is true we allow a single forward-then-backward iteration if (_currentAnimation.pingPong) { if (_currentAnimation.loop || _completedIterations < 2) { _isLoopingBackOnPingPong = _completedIterations % 2 != 0; } } var elapsedTime = 0f; if (_totalElapsedTime < _currentAnimation.iterationDuration) { elapsedTime = _totalElapsedTime; } else { elapsedTime = _totalElapsedTime % _currentAnimation.iterationDuration; // if we arent looping and elapsedTime is 0 we are done. Handle it appropriately if (!_currentAnimation.loop && elapsedTime == 0) { // the animation is done so fire our event if (onAnimationCompletedEvent != null) { onAnimationCompletedEvent(_currentAnimationKey); } isPlaying = false; switch (_currentAnimation.completionBehavior) { case AnimationCompletionBehavior.RemainOnFinalFrame: return; case AnimationCompletionBehavior.RevertToFirstFrame: setSubtexture(_currentAnimation.frames[0]); return; case AnimationCompletionBehavior.HideSprite: _subtexture = null; _currentAnimation = null; return; } } } // if we reversed the animation and we reached 0 total elapsed time handle un-reversing things and loop continuation if (_isReversed && _totalElapsedTime <= 0) { _isReversed = false; if (_currentAnimation.loop) { _totalElapsedTime = 0f; } else { // the animation is done so fire our event if (onAnimationCompletedEvent != null) { onAnimationCompletedEvent(_currentAnimationKey); } isPlaying = false; return; } } // time goes backwards when we are reversing a ping-pong loop if (_isLoopingBackOnPingPong) { elapsedTime = _currentAnimation.iterationDuration - elapsedTime; } // fetch our desired frame var desiredFrame = Mathf.floorToInt(elapsedTime / _currentAnimation.secondsPerFrame); if (desiredFrame != currentFrame) { currentFrame = desiredFrame; setSubtexture(_currentAnimation.frames[currentFrame]); handleFrameChanged(); // ping-pong needs special care. we don't want to double the frame time when wrapping so we man-handle the totalElapsedTime if (_currentAnimation.pingPong && (currentFrame == 0 || currentFrame == _currentAnimation.frames.Count - 1)) { if (_isReversed) { _totalElapsedTime -= _currentAnimation.secondsPerFrame; } else { _totalElapsedTime += _currentAnimation.secondsPerFrame; } } } }
/// <summary> /// Sprite needs a Subtexture at constructor time so the first frame of the passed in animation will be used for this constructor /// </summary> /// <param name="animationKey">Animation key.</param> /// <param name="animation">Animation.</param> public Sprite(TEnum animationKey, SpriteAnimation animation) : this(null, animation.frames[0]) { addAnimation(animationKey, animation); }
public void stop() { isPlaying = false; _currentAnimation = null; }
public void stop() { isPlaying = false; subtexture = null; _currentAnimation = null; }