溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Egret之美術(shù)字及HashMap

發(fā)布時(shí)間:2020-08-02 09:43:19 來(lái)源:網(wǎng)絡(luò) 閱讀:811 作者:Aonaufly 欄目:開(kāi)發(fā)技術(shù)

Egret之美術(shù)字及HashMap

將游戲中某些數(shù)字動(dòng)態(tài)的用上述美術(shù)數(shù)字替代 , 這么做的唯一原因就是為了好看。

制作資源 , 使用Sprite Sheet:

Egret之美術(shù)字及HashMap


創(chuàng)建一個(gè)FntManager , 用于動(dòng)態(tài)生成美術(shù)字,如下:

/**
* @author
*/
module app {
    export class FntManager{
        public constructor() {
        }
        
        private static _fntMap:HashMap = new HashMap();//存儲(chǔ)創(chuàng)建的位圖字體
        
        /*//創(chuàng)建位圖字體
         * param@1 父 容器
         * param@2 位圖字體資源
         * param@3 位圖顯示內(nèi)容
         * param@4 預(yù)留 位圖字體高度
         *
         * */
        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;
            }
            //多余的設(shè)置為空
            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");
        }
        //右對(duì)齊
        public static showFntRight(parent:eui.Group,res:string,content:string,picSize:number=24,picHeight=24):void
        {
            this.createUIAssetLable(parent,res,content,picHeight,picSize,"right");
        }
        //左對(duì)齊
        public static showFntLeft(parent:eui.Group,res:string,content:string,picSize:number=24,picHeight=24):void
        {
            this.createUIAssetLable(parent,res,content,picHeight,picSize,"left");
        }
        
        //左對(duì)齊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的實(shí)現(xiàn)::

/*
* MAP對(duì)象,實(shí)現(xiàn)MAP功能
*
* 接口:
* size()     獲取MAP元素個(gè)數(shù)
* 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的數(shù)組(ARRAY)
* keys()     獲取MAP中所有KEY的數(shù)組(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元素個(gè)數(shù)
        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的數(shù)組(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的數(shù)組(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;
 }
}



調(diào)用 :::

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  );


結(jié)果:

Egret之美術(shù)字及HashMap



提供一個(gè)泛型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;
        }

        /**
         * 添加一個(gè)鍵值對(duì)
         * @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;
        }

        /**
         * 移除一個(gè)鍵值對(duì),并返回值
         * @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;
        }

        /**
         * 移除第一個(gè)鍵值對(duì),并返回值(只有值)
         * @returns {V}
         */
        public removeFirst():V{
            if( this._content.length > 0 ){
                let $cell : IConten2MapHash<K , V> = this._content.shift();
                return $cell.value;
            }
            return null;
        }

        /**
         * 移除第一個(gè)鍵值對(duì),并返回鍵值對(duì)
         * @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;
        }

        /**
         * 清除所有鍵值對(duì)
         */
        public clear():void{
            this._content.length = 0;
        }

        /**
         * 復(fù)制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;
        }

        /**
         * 鍵值對(duì)是否為空
         * @returns {boolean}
         */
        public isEmpty():boolean{
            return this._content.length == 0;
        }

        /**
         * 鍵值對(duì)的個(gè)數(shù)(只讀)
         * @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;
        }

        /**
         * 添加、修改一個(gè)鍵值
         * 如果沒(méi)有則添加 / 如果有則修改
         * @param {K} $key 鍵
         * @param {V} $value 值
         * @returns {number} 存儲(chǔ)的長(zhǎng)度
         */
        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鍵值數(shù)據(jù)對(duì)接口
     * @author Husz
     */
    export interface IConten2MapHash<K , V >{
        /**鍵*/
        key : K;
        /**值*/
        value : V;
    }
}



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI