`
kinglong
  • 浏览: 32312 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

ArtDigit 艺术数字类

阅读更多
此类主要用于使用动画元件来显示对应的数字,并支持数字缓冲滚动显示功能!并有比较成熟的资源数字样式显示管理功能

package game.assist.view {
	import com.greensock.TweenMax;

	import flash.display.MovieClip;

	import game.net.File;
	import game.collection.HashMap;

	import flash.display.Sprite;

	/**
	 * ArtDigit 艺术数字
	 * @author Kinglong
	 * @since 2012-7-26
	 */
	public class ArtDigit extends Sprite {
		/**
		 * 资源路径
		 */
		public static var resourcePath : String = "";
		/**
		 * 资源列表
		 */
		private static const resourceMap : HashMap = new HashMap();
		/**
		 * 回调列表
		 */
		private static const callbackList : Vector.<Function> = new Vector.<Function>();

		/**
		 * 字符转成帧号
		 * @param chat 字符
		 */
		private static function charToFrame(chat : String) : int {
			switch(chat) {
				case "-":
					return 11;
				case "+":
					return 12;
			}
			return int(chat) + 1;
		}

		/**
		 * 改变回调
		 */
		public var onChange : Function = new Function();
		/**
		 * 是否初始化
		 */
		private var _inited : Boolean;
		/**
		 * 资源是否加载
		 */
		private var _loaded : Boolean;
		/**
		 * 数字样式
		 */
		private var _style : String;
		/**
		 * 数值
		 */
		private var _value : int;
		/**
		 * 数值字符
		 */
		private var _chars : String;
		/**
		 * 加号是否显示
		 */
		private var _addSymbolVisible : Boolean;
		/**
		 * 数字列表
		 */
		private var _numberList : Array;
		/**
		 * 间隔
		 */
		private var _spacing : int;
		/**
		 * 样式类
		 */
		private var _styleClass : Class;
		/**
		 * 缓冲对象
		 */
		private var _vo : TweenVO;

		/**
		 * 构造
		 * @param style 样式
		 */
		public function ArtDigit(style : String = null) {
			_inited = false;
			_loaded = false;
			if (style) {
				init(style);
			}
			_value = 0;
			_spacing = 2;
			_chars = "";
			_numberList = [];

			_vo = new TweenVO();

			_addSymbolVisible = false;

			mouseChildren = false;
			mouseEnabled = false;
		}

		/**
		 * 初始化
		 * @param style 样式
		 */
		public function init(style : String) : void {
			if (inited) {
				return;
			}
			_style = style;
			if (!resourceMap.containsKey(style)) {
				loadStyle();
			} else if (resourceMap.get(style) is File) {
				callbackList.push(updateStyle);
			} else {
				updateStyle();
			}
			_inited = true;
		}

		/**
		 * 加载样式
		 */
		private function loadStyle() : void {
			var url : String = resourcePath + style + ".swf";
			var file : File = new File();
			resourceMap.put(style, file);
			callbackList.push(updateStyle);
			file.onComplete = function() : void {
				resourceMap.put(style, file.getClassByName("Style"));
				for each (var func:Function in callbackList) {
					if (func != null) {
						func();
					}
				}
				callbackList.length = 0;
			};
			file.load(url);
		}

		/**
		 * 更新样式
		 */
		private function updateStyle() : void {
			_styleClass = resourceMap.get(style);
			_loaded = true;
			this.value = value;
		}

		/**
		 * 加号是否显示
		 */
		public function set addSymbolVisible(value : Boolean) : void {
			_addSymbolVisible = value;
			render(this.value);
		}

		/**
		 * 数值
		 */
		public function set value(num : int) : void {
			_value = num;
			TweenMax.killTweensOf(_vo);
			_vo.value = num;
			render(num);
		}

		public function get value() : int {
			return _value;
		}

		/**
		 * 缓动到指定数值
		 * @param num 指定数值
		 */
		public function tweenTo(num : int) : void {
			if (!loaded) {
				value = num;
				return;
			}
			TweenMax.to(_vo, 0.8, {value:num, onUpdate:tweenUpdateHandler});
		}

		/**
		 * 缓动更新处理
		 */
		private function tweenUpdateHandler() : void {
			render(_vo.value);
		}

		/**
		 * 数字间隔
		 */
		public function set spacing(value : int) : void {
			if (_spacing == value) {
				return;
			}
			_spacing = value;
			_chars = "";
			render(value);
		}

		public function get spacing() : int {
			return _spacing;
		}

		/**
		 * 渲染
		 * @param num 数值
		 */
		private function render(num : int) : void {
			if (!loaded) {
				return;
			}
			var str : String = new String(num);
			if (num > 0 && _addSymbolVisible) {
				str = "+" + str;
			}
			if (_chars == str) {
				return;
			}
			_chars = str;
			renderStyle();
			onChange();
		}

		/**
		 * 渲染数字样式
		 */
		private function renderStyle() : void {
			var i : int;
			var mc : MovieClip;
			var len : int = Math.max(_numberList.length, _chars.length);
			var dx : int = 0;
			for (i = 0; i < len; i++) {
				if (i >= _numberList.length) {
					mc = new _styleClass();
					mc.stop();
					_numberList.push(mc);
				}
				if (i < _chars.length) {
					mc = _numberList[i];
					if (!contains(mc)) {
						addChild(mc);
					}
					mc.gotoAndStop(charToFrame(_chars.charAt(i)));
					mc.y = 0;
					mc.x = dx;
					dx += mc.width + spacing;
				}
				if (i >= _chars.length) {
					mc = _numberList[i];
					if (contains(mc)) {
						removeChild(mc);
					}
				}
			}
			if (_numberList.length > _chars.length) {
			} else if (_numberList.length < _chars.length) {
				for (i = _numberList.length; i < _chars.length; i++) {
					mc = new _styleClass();
					mc.stop();
					addChild(mc);
					_numberList.push(mc);
				}
			}
		}

		/**
		 * 当前样式
		 */
		public function get style() : String {
			return _style;
		}

		/**
		 * 是否初始化
		 */
		public function get inited() : Boolean {
			return _inited;
		}

		/**
		 * 样式是否加载
		 */
		public function get loaded() : Boolean {
			return _inited && _loaded;
		}

		/**
		 * 移动
		 */
		public function move(x : int, y : int) : void {
			this.x = x;
			this.y = y;
		}

		/**
		 * 销毁
		 */
		public function dispose() : void {
			TweenMax.killTweensOf(_vo);
			for each (var mc : MovieClip in _numberList) {
				mc.stop();
				if (contains(mc)) {
					removeChild(mc);
				}
			}
			_numberList = null;
			var index : int = callbackList.indexOf(updateStyle);
			if (index != -1) {
				callbackList.splice(index, 1);
			}
		}
	}
}
class TweenVO {
	public var value : int = 0;
}

分享到:
评论
2 楼 myjave 2012-09-29  
哦 是我搞错了
这个怎么使用
1 楼 myjave 2012-09-29  
这个类拷贝靠flash里报错

相关推荐

Global site tag (gtag.js) - Google Analytics