Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17070

Cc.TableView in XMLHttpRequest

$
0
0

@tatchooliong wrote:

Hi.. I would like to create a table view to show the data as a result from JSON and XMLHttpRequest. It worked well when I executed the code outside of the XMLHttpRequest, while it failed when I put it in inside XMLHttpRequest.. Full code are shown as below:

var CustomTableViewCell = cc.TableViewCell.extend({
draw:function (ctx) {
    this._super(ctx);
}
});

var HiScoreScene2 = 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 Scores2", "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);

    var tableView = cc.TableView.create(self, cc.size(500, 450));
    tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
    tableView.setPosition(winSize.width / 2, winSize.height / 2 - 150);
    tableView.setDelegate(self);
    tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
    self.addChild(tableView);
    tableView.reloadData();

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

            // I WANT TO PUT THE TABLE VIEW IN HERE SO I COULD SHOW THE DATA I RETRIEVED FROM XMLHttpRequest.. BUT IT DIDNT WORK..


        }
        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;
},

 toExtensionsMainLayer:function (sender) {
    var scene = new ExtensionsTestScene();
    scene.runThisTest();
},

scrollViewDidScroll:function (view) {
},
scrollViewDidZoom:function (view) {
},

tableCellTouched:function (table, cell) {
    cc.log("cell touched at index: " + cell.getIdx());
},

tableCellSizeForIndex:function (table, idx) {
    if (idx == 2) {
        return cc.size(200, 200);
    }
    return cc.size(100, 100);
},

tableCellAtIndex:function (table, idx) {
    var strValue = idx.toFixed(0);
    var cell = table.dequeueCell();
    var label;
    if (!cell) {
        cell = new CustomTableViewCell();
        var sprite = cc.Sprite.create(res.Blood_png);
        sprite.setAnchorPoint(0, 0);
        sprite.setPosition(0, 0);
        cell.addChild(sprite);

        label = cc.LabelTTF.create("TESTING", "Helvetica", 20.0);
        label.setPosition(0,0);
        label.setAnchorPoint(0,0);
        label.setTag(123);
        cell.addChild(label);
    } else {
        label = cell.getChildByTag(123);
        label.setString(strValue);
    }

    return cell;
},

numberOfCellsInTableView:function (table) {
    return 15;
},
});

var mainMenu = function() {
    cc.log("Back To Main Menu Scene");
    var startScene = new StartScene();
    cc.director.runScene(new cc.TransitionFade(1, startScene));
};

Is there anyway to put it inside the xhr.onreadystatechange function? any help would be appreciated.. Thanks..

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17070


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>