當咱們的遊戲開發進度接近尾聲的時候,不只要作教學引導的事情,還有一件對於中大型遊戲來講很是重要的事情就是紅點提示。它有別於教學引導,但也是引導的做用,指引性更明確,並且不會影響UI外觀和體驗的流暢。函數
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()) } } } }
初始化函數中主要作了兩件事情:
一是 產生提示所對應的界面和控件數據。
二是 產生界面對應的提示id信息。主要是爲了快速斷定某個界面是否存在某個提示
ui
添加函數也是作了兩件事情
一是排重:記錄哪些正在進行中的提示,若是已經存在將再也不添加。
二是更新記錄數據並通知界面處理紅點操做。
this
當一個新物品被點擊時,移除紅點提示
此方法與添加提示的方法是相對的。也作了更新記錄數據和通知ui的事情。3d
在添加和移除時更新記錄的數據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) } } } } }
若是是普通的控件,將WidgetRedTip託到 界面的預製體的根節點上便可。blog
若是是道具首先須要將ItemRedTip組件拖到道具預製體的根節點上。而後在道具中聲明一個ItemRedTip類型的屬性,在更新數據時調用。
索引
產生新道具時調用提示管理器的添加函數
遊戲
點擊道具時移除提示
事件
demo展現
圖片
只是一個簡單的demo,點擊添加物品按鈕,登錄按鈕上會出現紅點;直接點擊道具紅點會消失,全部道具都點過一遍後左上角按鈕的紅點纔會消失。
製做紅點的方式不少,我見過有人把全部紅點手動添加到指定的按鈕上,而後再經過代碼顯示和隱藏。我比較懶,因此我選擇動態添加紅點圖片。
歡迎掃碼關注公衆號《微笑遊戲》,瀏覽更多內容。