UITransform.hitTest is not working correctly

Hello,

I’m using cocos creator 3.7.0, but method hitTest of UITransform is seem not working.

Basically, I have this code:

  1. register event
this.node.on(NodeEventType.TOUCH_END, this._onTouchEnded, this, true);
  1. process touch end:
protected _onTouchEnded (event: EventTouch, captureListeners?: Node[]) {
     // screen location
     let screenPoint = event.getLocation()  // {"x":779.2784423828125,"y":1581.3411254882812}
     let uiPos = event.getUILocation()        // {"x":519.5189615885416,"y":1054.2274169921875}
     // Have to Sprite with transform.getBoundingBoxToWorld()
     // 0 {"x":40,"y":968.699951171875,"width":283,"height":223}
     // 1 {"x":348,"y":968.699951171875,"width":283,"height":223}
     
     // Check collision by hitTest
     if (transform.hitTest(screenPoint)) --> not working

    // check collision by isHit
    if (transform.isHit(uiPos)) --> it's working ( but it's deprecated since v3.5.0 ) 

Please tell me, what is wrong with hitTest method ? And how to fix it

Thank you

I followed your code and did a test, hitTest and isHit both work fine, you can refer to my demo, if you have any more questions, please feel free to leave a comment and provide a demo to me, thanks!

NewProject.zip (28.5 KB)

Hi @muxiandong ,
I modified your demo project:

And here is the result:
On web simulator, both isHit and hitTest work normally. But when I build this project and deploy on real device, only isHit works, and hitTest doesn’t work.

  1. hitTest works on web simulator
    https://media.giphy.com/media/9BsBw9SDNtmNr7apRT/giphy.gif

  2. hitTest doesn’t work on real device
    https://media.giphy.com/media/99h4CJPoeacOM19RwH/giphy.gif

Please help me understand about that case.

Thank you

Hi, thank you for your feedback. The issue you mentioned, was able to reproduce, this should be a bug in the engine, I have created a new issue to the engine group UITransform.hitTest does not work properly on Android · Issue #14214 · cocos/cocos-engine · GitHub

Suggestion: For now, use UITransform.isHit to implement the functionality you need.

@muxiandong , hope it gets resolved soon ! :muscle: