Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tools;
- import com.haxepunk.graphics.Image;
- import com.haxepunk.HXP;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- /**
- * ...
- * @author Robin van Ee
- * http://rvanee.nl
- *
- *
- * Port of WolfEngine's SpriteRenderer component to HaxePunk
- */
- class WolfSprite
- {
- public var scale (default, set) : Point;
- function set_scale(val : Point) : Point
- {
- scale = val;
- Render();
- return scale;
- }
- public var frame (default, set) : Int = 0;
- function set_frame(f : Int) : Int
- {
- frame = f;
- Render();
- return frame;
- }
- public var image : Image;
- public var frameWidth : Int;
- public var frameHeight : Int;
- public var numFrames : Int;
- public var framesPerSecond : Int;
- private var clip : Array<Image>;
- private var sheetOffset : Int;
- private var _filename : String;
- private var realFrame : Float = 0;
- public function new(filename: String, fWidth : Int, fHeight : Int, ?offset : Int = 0, ?nFrames = HXP.INT_MAX_VALUE, ?FPS = 60)
- {
- _filename = filename;
- var sheet = new Image(filename);
- frameWidth = fWidth;
- frameHeight = fHeight;
- numFrames = nFrames;
- framesPerSecond = FPS;
- if (offset != null) sheetOffset = offset;
- clip = new Array<Image>();
- for(y in 0...cast(sheet.height / frameHeight))
- {
- for(x in 0...cast(sheet.width / frameWidth))
- {
- var tempRect : Rectangle = new Rectangle(x * frameWidth + (sheetOffset * x), y * frameHeight + (sheetOffset * y), frameWidth, frameHeight);
- var tempImage = new Image(filename, tempRect);
- clip.push(tempImage);
- }
- }
- scale = new Point(1, 1);
- sheet = null;
- Render();
- }
- public function play()
- {
- realFrame += framesPerSecond * HXP.elapsed;
- if (realFrame >= numFrames || realFrame >= clip.length) realFrame = 0;
- frame = cast(realFrame);
- Render();
- }
- private function Render()
- {
- image = clip[frame];
- image.scaleX = scale.x;
- image.scaleY = scale.y;
- image.centerOrigin();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment