遊戲開發中的紅點提示

前言

當咱們的遊戲開發進度接近尾聲的時候,不只要作教學引導的事情,還有一件對於中大型遊戲來講很是重要的事情就是紅點提示。它有別於教學引導,但也是引導的做用,指引性更明確,並且不會影響UI外觀和體驗的流暢。函數

開始

  1. 經過配置兩張數據表來記錄紅點提示的相關數據。
    image.png
    提示信息表,僅代表都有哪些提示類型。
    image.png
    每一個紅點提示使用的界面及控件名稱。
    第二列爲屬於哪一個提示,因此聲明爲索引類型。好比1來講,就是由GridLayerListViewTest的buttonBack和ItemView的bg兩個按鈕組成,也就是當有新道具添加時,GridLayerListViewTest的buttonBack和ItemView兩個控件都應該有紅點。
    第三列和第四列聲明爲類名和控件名
    第五列標識控件是否爲道具,由於道具和普通控件的記錄方式不一樣。
  2. 讀取配置數據,初始化信息。
init() {
        let redtipData: XlsxData = ModuleManager.dataManager.get(RedTipItemModel.CLASS_NAME)
        redtipData.forEach((key, data) => {
            let item = new RedTipItemModel()
            item.init(key, data)
            this.redtipMap.set(key, item)
        })

        let redtipStep: XlsxData = ModuleManager.dataManager.get(RedTipStepModel.CLASS_NAME)
        let indexs = redtipStep.getIndex(Redtip_step_dataEnum.tipID);
        for (const key in indexs) {
            if (indexs.hasOwnProperty(key)) {
                const list = indexs[key];
                for (let index = 0; index < list.length; index++) {
                    const stepID = list[index];
                    let step = new RedTipStepModel()
                    step.init(stepID, redtipStep.getRowData(stepID))
                    this.redtipMap.get(step.getTipID()).addStep(step)
                    // if (index == 0) {
                    let className = step.getClassName();
                    let classMap = this.classNameMap.get(className)
                    if (!classMap) {
                        classMap = []
                        this.classNameMap.set(className, classMap)
                    }
                    classMap.push(step.getTipID())
                }
            }
        }

    }

初始化函數中主要作了兩件事情:
一是 產生提示所對應的界面和控件數據。
image.png
二是 產生界面對應的提示id信息。主要是爲了快速斷定某個界面是否存在某個提示
image.pngui

  1. 當得到新物品時調用添加函數
    image.png

添加函數也是作了兩件事情
一是排重:記錄哪些正在進行中的提示,若是已經存在將再也不添加。
image.png
二是更新記錄數據並通知界面處理紅點操做。
image.pngthis

  1. 當一個新物品被點擊時,移除紅點提示
    image.png
    此方法與添加提示的方法是相對的。也作了更新記錄數據和通知ui的事情。3d

  2. 在添加和移除時更新記錄的數據code

/**
     * 
     * @param eventName 事件名稱
     * @param step 哪一個控件
     * @param id 道具id
     */
    private updateRecordCount(eventName: string, step: RedTipStepModel, id: number = -1) {
        let className = step.getClassName();
        let widgetName = step.getWidgetName();
        let tipID = step.getTipID();
        let widgetMap = this.getWidgetMap(className, widgetName)

        if (eventName == RedTipEventName.ADD_ITEM_TIP) {//添加
            if (!step.isItem()) {//若是控件不是道具,只是界面上的按鈕
                let list = widgetMap.get(tipID)
                if (!list) {
                    list = []
                    widgetMap.set(tipID, list)
                }
                list.push(id)
                if (list.length == 1) {
                    this.emit(eventName + className, step, id)
                }
            } else {//若是是道具
                let list = widgetMap.get(id)
                if (!list) {
                    list = []
                    widgetMap.set(id, list)
                }
                list.push(tipID)
                if (list.length == 1) {
                    this.emit(eventName + className, step, id)
                }
            }

        } else {//移除
            if (!step.isItem()) {
                let list = widgetMap.get(tipID)
                if (list.length > 0) {
                    list.shift()
                    if (this.isWidgetMapNull(widgetMap)) {
                        this.emit(eventName + className, step, id)
                    }
                }
            } else {
                let list = widgetMap.get(id)
                let index = list.indexOf(tipID)
                if (index >= 0) {
                    list.splice(index, 1)
                    if (list.length <= 0) {
                        this.emit(eventName + className, step, id)
                    }
                }
            }

        }
    }

使用

  1. 若是是普通的控件,將WidgetRedTip託到 界面的預製體的根節點上便可。blog

  2. 若是是道具首先須要將ItemRedTip組件拖到道具預製體的根節點上。而後在道具中聲明一個ItemRedTip類型的屬性,在更新數據時調用。
    image.png索引

  3. 產生新道具時調用提示管理器的添加函數
    image.png遊戲

  4. 點擊道具時移除提示
    image.png事件

  5. demo展現
    image.png圖片

只是一個簡單的demo,點擊添加物品按鈕,登錄按鈕上會出現紅點;直接點擊道具紅點會消失,全部道具都點過一遍後左上角按鈕的紅點纔會消失。

注意事項

  1. 因爲使用了事件通知的方式,因此須要注意監聽者的數量,若是道具成百上千,並且並無使用ListView的方式,那麼最好不要使用這種通知的方式,監聽者太多。
  2. 因爲使用的是控件名稱的方式,因此同一界面中的控件不能夠重名
  3. 若是滑動層沒有作分層管理,須要注意紅點圖片產生的dc數量。

結語

製做紅點的方式不少,我見過有人把全部紅點手動添加到指定的按鈕上,而後再經過代碼顯示和隱藏。我比較懶,因此我選擇動態添加紅點圖片。

歡迎關注公衆號《微笑遊戲》,瀏覽更多內容。

image 歡迎掃碼關注公衆號《微笑遊戲》,瀏覽更多內容。

相關文章
相關標籤/搜索