cocos 创作者的自定义 html 按钮

Custom html button for cocos creator

我正在使用 Cocos creator 创建一个仅适用于网络浏览器的简单游戏,并且需要实现一个分享到 twiiter 按钮。但我需要以简单的方式 javascript 进行此操作。到目前为止我有这段代码。

cc.Class({
    extends: cc.Component,

    properties: {
        tweet: cc.Button
    },

    start() {
        const el = document.createElement('a');
        el.classList.add('twitter-share-button');
        el.href = 'https://twitter.com/intent/tweet?url=http://test.com;via=stack';
        el.id = 'twiiterBtn';

        // After appending to body, element exists.
        document.body.appendChild(el);

        window.twttr = (function(d, s, id) {
            var js,
                fjs = d.getElementsByTagName(s)[0],
                t = window.twttr || {};
            if (d.getElementById(id)) return t;
            js = d.createElement(s);
            js.id = id;
            js.src = 'https://platform.twitter.com/widgets.js';
            fjs.parentNode.insertBefore(js, fjs);
            t._e = [];
            t.ready = function(f) {
                t._e.push(f);
            };
            return t;
        })(document, 'script', 'twitter-wjs');

        window.twttr.ready(function(twttr) {
            twttr.events.bind('tweet', function(event) {
                console.log('tweet callback', event);
            });
        });

        this.tweet.node.on('click', this.dispatch);
    },

    dispatch() {
        // Everytime I click the button, this function gets called correctly
        // but the element doesn't exist anymore, Cocos creator removed it.
        console.log(document.getElementById('twiiterBtn'))
    }
});

start() 我创建元素并添加 twiiter 按钮所需的 class,window.twttr 工作正常,执行就绪函数,但 Cocos 创建者正在删除我的元素,并且由于该元素不再存在,回调函数不起作用。如果有人遇到类似情况并且知道我该如何解决。

我基本上需要实现 tweet btn 并获得回调。

谢谢

看来你无法知道用户是否真的发了推文。这里有更多关于它的信息:

但这是一个工作代码,当用户点击并打开弹出窗口时会回调

cc.Class({
    extends: cc.Component,

    properties: {
        tweet: cc.Button
    },

    start() {
        // TODO: text IS CUSTOM MESSAGE; via IS TWITTER USERNAME
        const url = encodeURI('https://twitter.com/intent/tweet?text=super custom message;via=TwitterAccount');
        const el = document.createElement('a');

        el.classList.add('twitter-share-button');
        el.href = url;
        el.target = '_blank';
        el.id = 'twitter-intent';

        this.tweet.node.on('click', function(){
            window.open(url, '_blank', 'location=yes,height=370,width=520,scrollbars=yes,status=yes');

            // TODO: send callback to server.
            console.log("call API")
        });
    }
});