cocos2d-x JS 彈出對話框觸摸監聽(吞噬點擊事件遮擋層)


在游戲中,我們經常會碰到一些彈窗,這些彈窗禁止點透,也就是禁止觸摸事件傳遞到底層,我們稱之為遮擋層,這些遮擋層,需要開發遮擋層,我們首先得了解cocos2d-js的觸摸傳遞機制。

根據官方文檔,我們可以得知,觸摸方式有五種,但是根據需求,我們需要做的是攔截觸摸監聽。

所以我們簡單封裝了這么一個類,如下所示:

cc.ModelLayerColor = cc.LayerColor.extend({
	m_touchListener:null,
	ctor:function(){
		this._super(); var touchListener = { event: cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches: true, onTouchBegan: this.onTouchBegan }; cc.eventManager.addListener(touchListener, this); this.m_touchListener = touchListener; }, onTouchBegan:function(touch, event) { var target = event.getCurrentTarget(); if(!target.isVisible() || (!this.isTouchInside(target,touch))){ return false; } return true; }, isTouchInside: function (owner,touch) { if(!owner || !owner.getParent()){ return false; } var touchLocation = touch.getLocation(); // Get the touch position touchLocation = owner.getParent().convertToNodeSpace(touchLocation); return cc.rectContainsPoint(owner.getBoundingBox(), touchLocation); } }); 這里要把swallowTouches設置為true,這樣onTouchBegan返回true才能夠吞噬觸摸,不繼續往優先級更低的層傳遞,從而實現遮擋層。 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM