spritesheet /AnimViewTool/src/fw/anim/AnimSprite.as

Language ActionScript Lines 252
MD5 Hash fd258269d77a558f5ce1e49d418051d1
Repository https://bitbucket.org/ntibor22/spritesheet.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package fw.anim
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	// =============================  Created by: Amos Laber, Dec 2, 2011
	
	//
	// AnimSprite is a class to diplay an animated sprite sheet
	// it is initialized with a sprite sheet and can hold multiple 
	// animation sequnces that can be switched anytime
	//
	
	public class AnimSprite extends Bitmap 
	{
		protected var mAnimSheet:AnimTextureSheet;
		protected var mSequences:Array;
		
		protected var curAnim:AnimSeqData;  // current sequence
		protected var dirty:Boolean;
		protected var donePlaying:Boolean;
		protected var curIndex:uint;  // Frame index into tile sheet 
		protected var curFrame:uint;  // Frame index in a sequence (local)
		
		protected var frameDelay:Number; // for fps 
		
		
		
		// Internal, used to time each frame of animation.
		protected var frameTimer:Number;
		
		public static const LEFT:uint= 1;
		public static const RIGHT:uint= 2;
		
		
		
		public function AnimSprite(bitmapData:BitmapData=null)
		{
			super(bitmapData);
			
			frameTimer= 0;
			mSequences= [];
			
		}
		
		//
		// Initialize the sprite with the texture sheet.
		//  supportFlip: set to true if you intend to use right/left flipping
		//
		public function initialize(sheet:AnimTextureSheet):void
		{
			if (!sheet) return;
			
			mAnimSheet= sheet;
			frameDelay=0;
			
			// Create the frame buffer
			bitmapData = new BitmapData(mAnimSheet.maxRect.width, mAnimSheet.maxRect.height); 
			smoothing= true;
			
			curAnim = null;
			this.frame= 0;
			
			drawFrame(true);
		}
		
		private var _fps:int;
		
		public function get fps():int { return _fps; }
		public function forceFrameRate(fps:int):void
		{
			_fps = fps;
			frameDelay= 1.0 / fps;
		}
		
		public function destroy():void
		{
			if (bitmapData) {
				bitmapData.dispose();
			}
		}
		
		// Check if we are playing a sequence
		//
		public function isPlaying(index:int=0):Boolean
		{
			return !donePlaying;
		}
		
		public function get tileSheet():AnimTextureSheet { return mAnimSheet; }
		
		public function get numFrames():int { return mAnimSheet.numFrames; }
		public function get numSequences():int { return mSequences.length; }
		
		public function get seqFrame():uint { return curFrame; }
		
		public function get frame():uint { return curIndex; }
		
		public function set frame(val:uint):void
		{
			curFrame = val;
			
			if (curAnim)
				curIndex= curAnim.arFrames[curFrame];
			else curIndex= val;
			
			//curAnim = null;
			dirty = true;
		}
		
		
		
		public function getSequenceData(seq:int):AnimSeqData { return mSequences[seq]; }
		public function getSequence(seq:int):String { return mSequences[seq].seqName; }
		
		public function addSequence(name:String, frames:Array, frameRate:Number=0, looped:Boolean=true):void
		{
			mSequences.push(new AnimSeqData(name, frames, frameRate, looped));
		}
		
		public function findSequence(name:String):AnimSeqData
		{
			return findSequenceByName(name);
		}
		
		private function findSequenceByName(name:String):AnimSeqData
		{
			var aSeq:AnimSeqData;
			for (var i:int = 0; i < mSequences.length; i++) {
				aSeq = AnimSeqData(mSequences[i]);
				if (aSeq.seqName == name) {
					return aSeq;
				}
			}
			return null;
		}
		
		// Start playing a sequence
		//
		public function play(name:String=null):void
		{
			// Continue playing from last frame
			if (name == null) {
				donePlaying= false;
				dirty= true;
				frameTimer = 0;
				
				return;
			}
			
			curFrame = 0;
			curIndex = 0;
			frameTimer = 0;
			
			curAnim= findSequenceByName(name);
			if (!curAnim) {
				trace("play: cannot find sequence: " + name);
				return;
			}
			
			// trace("playing " + name +", frames: " + curAnim.arFrames.length);
			
			// Set to first frame
			curIndex= curAnim.arFrames[0];
			
			if (frameDelay==0) frameDelay= curAnim.delay;
			donePlaying= false;
			dirty= true;
			
			// Stop if we only have a single frame
			if (curAnim.arFrames.length==1) donePlaying= true;
		}
		
		
		// External use only (editor) 
		//
		public function stop():void
		{
			donePlaying= true;
		}
		
		// Manually advance one frame forwards or back
		// Used by the viewer (not the game)
		//
		public function frameAdvance(next:Boolean):void
		{
			if (next) {
				if (curFrame < curAnim.arFrames.length -1) ++curFrame;
			}
			else {
				if (curFrame > 0) --curFrame;
			}
			
			curIndex= curAnim.arFrames[curFrame];
			dirty= true;
		}
		
		
		public function drawFrame(force:Boolean=false):void
		{
			if(force || dirty)
				drawFrameInternal();
		}
		
	
		
		//  Call this function on every frame update
		//  
		public function updateAnimation(delta:int):void
		{
			//trace("updateAnimation delta=" + delta + ", frd=" + frameDelay)
			if (curAnim!=null && frameDelay > 0 && !donePlaying) {
				
				// Check elapsed time and adjust to sequence rate 
				frameTimer += (delta/1000.0);
				while(frameTimer > frameDelay) {
					frameTimer = frameTimer - frameDelay;
					advanceFrame();
				}
			}
			
			if (dirty) drawFrameInternal();
		}
		
		// 
		//
		protected function advanceFrame():void
		{
			if (curFrame == curAnim.arFrames.length -1) {
				if (curAnim.loop)
					curFrame = 0;
				else donePlaying= true; 
				
			}
			else ++curFrame;
			
			curIndex= curAnim.arFrames[curFrame];
			dirty= true;
		}
		
		// Internal function to update the current animation frame
		protected function drawFrameInternal():void
		{
			dirty = false;
			
			bitmapData.fillRect(bitmapData.rect, 0);
			mAnimSheet.drawFrame(curIndex, bitmapData);
		}
		
	}
}
Back to Top