Skip to content

Instantly share code, notes, and snippets.

@xuanye
Created March 18, 2020 07:48
Show Gist options
  • Save xuanye/6fcdcb23864f022f2aceb5aa5d2eda91 to your computer and use it in GitHub Desktop.
Save xuanye/6fcdcb23864f022f2aceb5aa5d2eda91 to your computer and use it in GitHub Desktop.
重写点击事件
// Override
PIXI.InteractionManager.prototype.onTouchStart = function(event)
{
var rect = this.interactionDOMElement.getBoundingClientRect();
if(PIXI.AUTO_PREVENT_DEFAULT)event.preventDefault();
var changedTouches = event.changedTouches;
for (var i=0; i < changedTouches.length; i++)
{
var touchEvent = changedTouches[i];
var touchData = this.pool.pop();
if(!touchData)touchData = new PIXI.InteractionData();
touchData.originalEvent = event || window.event;
this.touchs[touchEvent.identifier] = touchData;
touchData.global.x = (touchEvent.clientX - rect.left) * (this.target.width / rect.width);
touchData.global.y = (touchEvent.clientY - rect.top) * (this.target.height / rect.height);
if(navigator.isCocoonJS)
{
var h = this.interactionDOMElement.style.height;
var w = this.interactionDOMElement.style.width;
var heightRatio = parseInt(h.replace('px', '')) / GAME.height;
var widthRatio = parseInt(w.replace('px', '')) / GAME.width;
touchData.global.x = touchEvent.clientX / widthRatio;
touchData.global.y = touchEvent.clientY / heightRatio;
}
var length = this.interactiveItems.length;
for (var j = 0; j < length; j++)
{
var item = this.interactiveItems[j];
if(item.touchstart || item.tap)
{
item.__hit = this.hitTest(item, touchData);
if(item.__hit)
{
//call the function!
if(item.touchstart)item.touchstart(touchData);
item.__isDown = true;
item.__touchData = touchData;
if(!item.interactiveChildren)break;
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment