将游戏中某些数字动态的用上述美术数字替代 , 这么做的唯一原因就是为了好看。
制作资源 , 使用Sprite Sheet:
创建一个FntManager , 用于动态生成美术字,如下:
/**
* @author
*/
module app {
export class FntManager{
public constructor() {
}
private static _fntMap:HashMap = new HashMap();//存储创建的位图字体
/*//创建位图字体
* param@1 父 容器
* param@2 位图字体资源
* param@3 位图显示内容
* param@4 预留 位图字体高度
*
* */
public static createBitmapLabel(parent:eui.Group,res:string,content:string,picHeight):eui.BitmapLabel
{
if(content=="NaN"||content=="undefined")
{
console.error("res 为空");
}
if(parent==null)
return;
parent.removeChildren();
var bitmaplabel=new eui.BitmapLabel();
bitmaplabel.font = RES.getRes(res);
bitmaplabel.text = content;
bitmaplabel.y = (parent.height - picHeight) / 2;
parent.addChild(bitmaplabel);
this._fntMap.put(parent,bitmaplabel);
return bitmaplabel;
}
/**
* 不使用位图字体的位图渲染替代法
*/
private static createUIAssetLable(parent:eui.Group,res:string,content:string,picHeight:number,picWidth:number,align:string):void
{
if(parent==null)
return;
var child:number = parent.numChildren;
var len:number = content.length;
for(var i:number=0;i<len;i++)
{
if(i<child)
{
var tmpAsset:eui.Image = <eui.Image>parent.getChildAt(i);
}
else
{
var tmpAsset:eui.Image = new eui.Image();
parent.addChild(tmpAsset);
}
if( res != "" )
tmpAsset.source = RES.getRes( res+"_"+content[i]+"_png");
else
tmpAsset.source = RES.getRes( content[i]+"_png");
if(i>0)
{
var lastAss:eui.Image = <eui.Image>parent.getChildAt(i-1);
tmpAsset.x = lastAss.x+picWidth;
}
else
{
if(align=="left")
tmpAsset.x = 0;
else if(align=="middle")
tmpAsset.x= 0.5*(parent.width-len*picWidth);
else
tmpAsset.x= parent.width-len*picWidth;
}
tmpAsset.y = (parent.height - picHeight) / 2;
}
//多余的设置为空
if(child>len)
{
for(i=len;i<child;i++)
{
var tmpAsset:eui.Image = <eui.Image>parent.getChildAt(i);
tmpAsset.source = null;
}
}
}
//居中
public static showFnt(parent:eui.Group,res:string,content:string,picWidth:number=24,picHeight=24):void
{
this.createUIAssetLable(parent,res,content,picHeight,picWidth,"middle");
}
//右对齐
public static showFntRight(parent:eui.Group,res:string,content:string,picSize:number=24,picHeight=24):void
{
this.createUIAssetLable(parent,res,content,picHeight,picSize,"right");
}
//左对齐
public static showFntLeft(parent:eui.Group,res:string,content:string,picSize:number=24,picHeight=24):void
{
this.createUIAssetLable(parent,res,content,picHeight,picSize,"left");
}
//左对齐2
public static showFntLeft2(parent:eui.Group,res:string,content:string,picSize:number=24,picHeight=24):void
{
this.createUIAssetLable(parent,res,content,picHeight,picSize,"left");
}
/**
* 删除位图字体
*/
public static removeFnt(p:eui.Group):void
{
let bLabel:eui.BitmapLabel = <eui.BitmapLabel>this._fntMap.remove(p);
if(bLabel)
{
if(bLabel.parent)
(<eui.Group>bLabel.parent).removeChild(bLabel);
bLabel = null;
}
}
}
}
附上HashMap的实现::
/*
* MAP对象,实现MAP功能
*
* 接口:
* size() 获取MAP元素个数
* isEmpty() 判断MAP是否为空
* clear() 删除MAP所有元素
* put(key, value) 向MAP中增加元素(key, value)
* remove(key) 删除指定KEY的元素,成功返回True,失败返回False
* get(key) 获取指定KEY的元素值VALUE,失败返回NULL
* element(index) 获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL
* containsKey(key) 判断MAP中是否含有指定KEY的元素
* containsValue(value) 判断MAP中是否含有指定VALUE的元素
* values() 获取MAP中所有VALUE的数组(ARRAY)
* keys() 获取MAP中所有KEY的数组(ARRAY)
*
* 例子:
* var map = new Map();
*
* map.put("key", "value");
* var val = map.get("key")
* ……
*
*/
module app{
export class HashMap{
private elements : Array<IHashMapData> = new Array();
public constructor() {
}
//获取MAP元素个数
size():number {
return this.elements.length;
}
//判断MAP是否为空
isEmpty():boolean {
return (this.elements.length < 1);
}
//删除MAP所有元素
clear(){
this.elements = new Array();
}
//向MAP中增加元素(key, value)
put(_key : any , _value : any) {
this.elements.push( {
key : _key,
value : _value
});
}
//删除指定KEY的元素,并返回删除的元素值
remove(_key : any ):any {
try {
for (var i:number = 0; i < this.elements.length; i++) {
if (this.elements[i].key == _key) {
let value : any = this.elements[i].value;
this.elements.splice(i, 1);
return value;
}
}
} catch (e) {
}
return null;
}
//获取指定KEY的元素值VALUE,失败返回NULL
get(_key : any) {
try {
for (var i:number = 0; i < this.elements.length; i++) {
if (this.elements[i].key == _key) {
return this.elements[i].value;
}
}
} catch (e) {
return null;
}
}
//获取指定索引的元素,失败返回NULL
element(_index) : IHashMapData {
if (_index < 0 || _index >= this.elements.length) {
return null;
}
return this.elements[_index];
}
//判断MAP中是否含有指定KEY的元素
containsKey(_key : any):boolean {
var bln = false;
try {
for (var i:number = 0; i < this.elements.length; i++) {
if (this.elements[i].key == _key) {
bln = true;
}
}
} catch (e) {
bln = false;
}
return bln;
}
//判断MAP中是否含有指定VALUE的元素
containsValue(_value : any):boolean {
var bln = false;
try {
for (var i:number = 0; i < this.elements.length; i++) {
if (this.elements[i].value == _value) {
bln = true;
}
}
} catch (e) {
bln = false;
}
return bln;
}
//获取MAP中所有VALUE的数组(ARRAY)
values():Array<any> {
var arr = new Array();
for (var i = 0; i < this.elements.length; i++) {
arr.push(this.elements[i].value);
}
return arr;
}
//获取MAP中所有KEY的数组(ARRAY)
keys():Array<any> {
var arr = new Array();
for (var i:number = 0; i < this.elements.length; i++) {
arr.push(this.elements[i].key);
}
return arr;
}
}
interface IHashMapData{
key : any;
value : any;
}
}
调用 :::
module app {
export class FntView extends eui.Component implements eui.UIComponent{
private group_number : eui.Group;
public constructor() {
super();
this.skinName = "resource/eui_skins/FntView.exml";
}
protected childrenCreated():void{
super.childrenCreated();
this.showNumer( 1139 );
}
private showNumer( num : number ) : void{
let show : string = num.toString();
FntManager.showFnt( this.group_number , "" , show , 125 , 75 );
}
}
}
核心 :
let show : string = num.toString();
FntManager.showFnt( this.group_number , "" , show , 125 , 75 );
结果:
提供一个泛型HashMap:
module bg2tool{
/**
* HashMap K : 键 V : 值
* @author Husz
*/
export class HashMap< K , V>{
private _content: Array<IConten2MapHash<K,V>> = null;
public constructor(){
this._content = [];
}
/**
* 是否存在此键
* @param {K} key 键值
* @returns {boolean} 是否存在
*/
public containsKey(key:K):boolean{
let $cell : IConten2MapHash<K , V> = null;
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
if( $cell.key == key ){
return true;
}
}
return false;
}
/**
* 是否存在此值
* @param {V} value 值
* @returns {boolean} 是否存在
*/
public containsValue(value:V):boolean{
var length:number = this._content.length;
let $cell : IConten2MapHash<K , V> = null;
for(let $i:number = 0;$i < length;$i++){
$cell = this._content[$i];
if ($cell.value == value){
return true;
}
}
return false;
}
/**
* 添加一个键值对
* @param {K} key
* @param {V} value
* @returns {number}
*/
public add(key:K, value:V):number{
if (key == null){
console.log("[HashMap]Cannot put a value with undefined or null key!");
return this._content.length;
}
if (!this.containsKey(key)){
this._content.push(
{
"key" : key,
"value" : value
}
)
}
return this._content.length;
}
/**
* 移除一个键值对,并返回值
* @param {K} key
* @returns {V}
*/
public remove(key:K):V{
if (!this.containsKey(key)){
return null;
}
let $cell : IConten2MapHash<K , V> = null;
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
if( $cell.key == key ){
this._content.splice( $i , 1 );
return $cell.value;
}
}
return null;
}
/**
* 移除第一个键值对,并返回值(只有值)
* @returns {V}
*/
public removeFirst():V{
if( this._content.length > 0 ){
let $cell : IConten2MapHash<K , V> = this._content.shift();
return $cell.value;
}
return null;
}
/**
* 移除第一个键值对,并返回键值对
* @returns {bg2tool.IConten2MapHash<K, V>}
*/
public shift() : IConten2MapHash<K , V>{
if( this._content.length > 0 ){
let $cell : IConten2MapHash<K , V> = this._content.shift();
return $cell;
}
return null;
}
/**
* 清除所有键值对
*/
public clear():void{
this._content.length = 0;
}
/**
* 复制HashMap
* @returns {HashMap<K, V>}
*/
public clone():HashMap<K,V>{
var hashMap:HashMap<K,V> = new HashMap<K,V>();
let $cell : IConten2MapHash<K , V> = null;
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
hashMap.add( $cell.key , $cell.value );
}
return hashMap;
}
/**
* 键值对是否为空
* @returns {boolean}
*/
public isEmpty():boolean{
return this._content.length == 0;
}
/**
* 键值对的个数(只读)
* @returns {number}
*/
public get length():number{
return this._content.length;
}
/**
* 获取键
* @param {V} value
* @returns {K}
*/
public getKey(value:V):K{
let $cell : IConten2MapHash<K , V> = null;
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
if( $cell.value == value){
return $cell.key;
}
}
return null;
}
/**
* 获取所有键S
* @returns {Array<K>}
*/
public getKeys():Array<K>{
if( this._content.length == 0 ) return null;
let $cell : IConten2MapHash<K , V> = null;
let $keys : Array<K> = [];
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
$keys.push( $cell.key );
}
return $keys;
}
/**
* 获取值
* @param {K} key
* @returns {V}
*/
public getValue(key:K):V{
let $cell : IConten2MapHash<K , V> = null;
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
if( $cell.key == key ){
return $cell.value;
}
}
return null;
}
/**
* 获取所有值S
* @returns {Array<V>}
*/
public getValues():Array<V>{
if( this._content.length == 0 ) return null;
let $cell : IConten2MapHash<K , V> = null;
let $values : Array<V> = [];
for( let $i : number = 0 , $j : number = this._content.length ; $i < $j ; $i ++ ){
$cell = this._content[$i];
$values.push( $cell.value );
}
return $values;
}
/**
* 添加、修改一个键值
* 如果没有则添加 / 如果有则修改
* @param {K} $key 键
* @param {V} $value 值
* @returns {number} 存储的长度
*/
public addChangeVal($key:K, $value:V):number{
if ($key == null){
return this._content.length;
}
if (this.containsKey($key)){
this.remove($key);
}
this._content.push({key : $key, value : $value});
return this._content.length;
}
/**
* 销毁
*/
public destroy() : void{
this.clear();
this._content = null;
}
}
/**
* HashMap键值数据对接口
* @author Husz
*/
export interface IConten2MapHash<K , V >{
/**键*/
key : K;
/**值*/
value : V;
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。