【问题标题】:Custom html button for cocos creatorcocos creator 的自定义 html 按钮
【发布时间】:2019-11-07 00:27:05
【问题描述】:

我正在使用 Cocos creator 为 web 浏览器创建一个简单的游戏,并且需要实现一个分享到 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 按钮所需的类,window.twttr 工作正常,ready 函数被执行,但 Cocos 创建者正在删除我的元素,因为该元素不再存在,回调函数不起作用。如果有人遇到类似情况并且知道我该如何解决。

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

谢谢

【问题讨论】:

    标签: javascript twitter cocos2d-js


    【解决方案1】:

    您似乎无法知道用户是否真的发了推文。这里有更多关于它的信息:https://stackoverflow.com/a/48803977/4066787

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

    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")
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2016-07-24
      • 2014-11-28
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2016-02-28
      • 2017-04-23
      • 1970-01-01
      相关资源
      最近更新 更多