首页 > 其他分享 >cocos creator 模拟点击某个节点

cocos creator 模拟点击某个节点

时间:2023-02-24 19:33:27浏览次数:43  
标签:cocos const pt creator cc glPos inputManager 节点 rect

    static touchNode (node:cc.Node) {
        const glPos = node.convertToWorldSpaceAR(cc.v2());
        setTimeout(() => {
            this.touchSimulation(glPos);
        }, 0);
    }

    // 模拟点击
    static touchSimulation (glPos: cc.Vec2) {
        let rect;
        // @ts-ignore
        const inputManager = cc.internal.inputManager;
        if (cc.sys.isBrowser) {
            const canvas = document.getElementById('GameCanvas');
            rect = this.getHTMLElementPosition(canvas);
        } else {
            rect = cc.view.getFrameSize();
            rect.left = 0;
            rect.top = 0;
        }

        const vp = cc.view.getViewportRect();
        const sx = cc.view.getScaleX();
        const sy = cc.view.getScaleY();
        const ratio = cc.view.getDevicePixelRatio();
        const htmlx = (glPos.x * sx + vp.x) / ratio + rect.left;
        const htmly = rect.top + rect.height - (glPos.y * sy + vp.y) / ratio;
        const pt = cc.v2(htmlx, htmly);

        // cc.log(`模拟点击坐标:${pt.x}, ${pt.y}`);
        const touch = inputManager.getTouchByXY(pt.x, pt.y, rect);
        const touchEnd = inputManager.getTouchByXY(pt.x, pt.y, rect);
        inputManager.handleTouchesBegin([touch]);
        inputManager.handleTouchesEnd([touchEnd]);

        // setTimeout(() => {
        //     inputManager.handleTouchesEnd([touch]);
        // }, 100);
    }

 

标签:cocos,const,pt,creator,cc,glPos,inputManager,节点,rect
From: https://www.cnblogs.com/dzqdzq/p/17152871.html

相关文章