[SOLVED] TouchStart TouchEnd not working as expected!


In project TouchStart TouchEnd not working as expected!

In my project i have two buttons…

Both buttons have same script:

var ButtonClickScript = pc.createScript('buttonClickScript');

// initialize code called once per entity
ButtonClickScript.prototype.initialize = function() {
    this.entity.element.on('mousedown', function (evt) {
        console.log("mousedown = "+ this.entity.name);
    }, this);
    this.entity.element.on('mouseup', function (evt) {
        console.log("mouseup = "+ this.entity.name);
    }, this);
    this.entity.element.on('touchstart', function (evt) {
        console.log("touchstart = "+ this.entity.name);
    }, this);
    this.entity.element.on('touchend', function (evt) {
        console.log("touchend = "+ this.entity.name);
    }, this);

// update code called every frame
ButtonClickScript.prototype.update = function(dt) {

[On computer] When mouse down on button1 and mouse up on button2 it works fine and shows the proper msg in console.log…

mousedown = Button1 
mouseup = Button2

[On Mobile] When touch start on button1 and touch end on button2 it does not work as expected and shows the msg in console.log…

touchstart = Button1 
touchend = Button1

Here is the project link…

How i can fix this?

It would be great if anyone can help…
Thanks in Advance!

Unfortunately a known issue that hasn’t moved up the priority: https://github.com/playcanvas/engine/issues/2165

@yaustar ohk

Is there any alternate way to do this?
Can you please suggest what am i supposed to do?

It looks like you would have to patch the engine ElementInput class to handle touchEnd differently so fires the event on the element it is on.

I wonder if it’s possible to get the x, y coordinates of the touch end and then check which button those coordinates are within?

@yaustar I just checked engine code… and It seems alien code for me… :worried:

@Kulodo133 Yes i am trying that… But dont know how to locate that button according to x y coordinate… :thinking:

It is possible as the Element Input code does exactly this :slight_smile:

1 Like

yes, not my most helpful comment :slight_smile:

@yaustar @Kulodo133

Well, I am not able to get it how i can do this…
Can you please help codewise?

Anyone can help?

Sorry, I’m afraid I don’t have the bandwidth to look into patching. You will either need to work around it or continue to try to work out the engine code in order to patch it

1 Like

I can take a look this evening.

1 Like

@Ketan_ATA. I had a go and it seems to work.



@Kulodo133 Wow wonderful… :love_you_gesture:t2: Thank you very much for helping me to fix this… :smiley:

@yaustar Thank you very much to you as well for trying to help… :slight_smile:

1 Like