如何使用Cocos2d-JS使用mySQL数据库连接

时间:2016-05-26 09:54:17

标签: cocos2d-js

我是cocos2d-JS的新手,我不知道如何做 连接mysql数据库 使用cocos2d-js。  我在谷歌和论坛上搜索但我找不到信息 请让我知道如何连接。
感谢。

2 个答案:

答案 0 :(得分:0)

以下是示例代码..希望它有助于解决您的问题..

描述:下面的代码用于使用XMLHttpRequest从MySQL检索高分,将它们解析为JSON并使用ccui.ListView在场景中显示它们(不要忘记在project.json文件中添加'extensions'模块包括ccui模块或ListView将无法正常工作..)

var HiScoreScene = cc.Scene.extend({
result: {},
ctor: function(){
    this._super();
    this.init();
},

init: function () {
    cc.log("Entering Select High Scores Scene");

    var winSize = cc.view.getDesignResolutionSize();
    var xhr = cc.loader.getXMLHttpRequest();
    var self = this;

    self.labelHighScores = new cc.LabelTTF("High Scores", "Comic Sans MS", 30);
    self.labelHighScores.setColor(cc.color(255,255,255));
    self.labelHighScores.x = winSize.width / 2;
    self.labelHighScores.y = winSize.height / 1.05;
    self.addChild(self.labelHighScores, 1);

    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
            cc.log("Getting High Scores Details..");
            self.result = JSON.parse(xhr.responseText);

            var listView = new ccui.ListView();
            listView.setDirection(ccui.ScrollView.DIR_VERTICAL);
            listView.setGravity(ccui.ListView.GRAVITY_CENTER_HORIZONTAL);
            listView.setTouchEnabled(true);
            listView.setBounceEnabled(true);
            listView.setBackGroundImage(res.HiScoreBG_png);
            listView.setContentSize(cc.size(500, 600));
            listView.setAnchorPoint(cc.p(0.5, 0.5));
            listView.setPosition(cc.p(winSize.width / 2, winSize.height / 2));
            self.addChild(listView);


            for (var i = 0; i < self.result.length ; i++) {
                var button = new ccui.Button();
                var button2 = new ccui.Button();
                var button3 = new ccui.Button();
                var button4 = new ccui.Button();
                var button5 = new ccui.Button();
                var button6 = new ccui.Button();

                //Set Player Name
                button.setName("PlayerName");
                button.setTouchEnabled(false);
                button.setTitleColor(cc.color.BLACK);
                button.setTitleFontName("Comic Sans MS");
                button.setTitleFontSize(25);
                button.setTitleText("Player Name : " + self.result[i].player_name);

                //Set Easy High Score
                button2.setName("EasyHighScore");
                button2.setTouchEnabled(false);
                button2.setTitleColor(cc.color.BLACK);
                button2.setTitleFontName("Comic Sans MS");
                button2.setTitleFontSize(25);
                button2.setTitleText("Easy : " + self.result[i].easy_hiscore);

                //Set Medium High Score
                button3.setName("MedHighScore");
                button3.setTouchEnabled(false);
                button3.setTitleColor(cc.color.BLACK);
                button3.setTitleFontName("Comic Sans MS");
                button3.setTitleFontSize(25);
                button3.setTitleText("Medium : " + self.result[i].med_hiscore);

                //Set Hard High Score
                button4.setName("HardHighScore");
                button4.setTouchEnabled(false);
                button4.setTitleColor(cc.color.BLACK);
                button4.setTitleFontName("Comic Sans MS");
                button4.setTitleFontSize(25);
                button4.setTitleText("Hard : " + self.result[i].hard_hiscore);

                //Set GodLike High Score
                button5.setName("GodHighScore");
                button5.setTouchEnabled(false);
                button5.setTitleColor(cc.color.BLACK);
                button5.setTitleFontName("Comic Sans MS");
                button5.setTitleFontSize(25);
                button5.setTitleText("GodLike : " + self.result[i].god_hiscore);

                button6.setName("Space");
                button6.setTouchEnabled(false);
                button6.setTitleColor(cc.color.BLACK);
                button6.setTitleFontName("Comic Sans MS");
                button6.setTitleFontSize(25);
                button6.setTitleText(" ");

                listView.pushBackCustomItem(button);
                listView.pushBackCustomItem(button2);
                listView.pushBackCustomItem(button3);
                listView.pushBackCustomItem(button4);
                listView.pushBackCustomItem(button5);
                listView.pushBackCustomItem(button6);
            }

        }
        else if(xhr.status == 403){
            cc.log("Access Forbidden !!");
        }
        else if(xhr.status == 404){
            cc.log("File Not Found !!");
        }
    };
    xhr.open("GET", "php/getHighScore.php", true);
    xhr.send();

    self.optionMenuItem1 = new cc.MenuItemFont("Back To Main Menu", mainMenu);
    self.optionMenuItem1.setFontName("Comic Sans MS");
    self.optionMenuItem1.setFontSize(25);

    self.menu = new cc.Menu(self.optionMenuItem1);
    self.menu.setPosition(winSize.width / 2, winSize.height / 22);
    self.addChild(self.menu, 1);

    return true;
}});

有关详细说明,您可能需要查看here

答案 1 :(得分:0)

Cocos2d-JS是javascript所以,它就像javascript,如果你想将值传递到MySQL数据库,请使用ajax请求example