In prepping the objects for a game I'm making in javscript, I'm reading in a JSON object, deckData, and appending each as a child of this.discard. I'm then scheduling a job that checks to see if this.deck is empty. If it is, then the function should shuffle the array of cards listed as children of this.discard and then assign them as children of this.deck.
The whole process seems to work fairly smoothly except for the key line wh开发者_JAVA百科ere I try to assign the newly shuffled array over:
this.deck.children_ = this.shuffle(this.discard.children_);
I'm looking for a solution that will succesfully append all elements that were children of this.discard as children of this.deck. And I realize I'm not yet emptying the array from this.discard yet either, but one step at a time. Full code snippet as follows:
sideboard = function(game) {
this.discard = new lime.Layer();
this.appendChild(this.discard);
for (var key in deckData) {
var cardData = deckData[key];
this.addCard(cardData);
};
this.mythosDeck = new lime.Layer();
this.appendChild(this.mythosDeck);
lime.scheduleManager.schedule(this.deckShuffler, this);
};
sideboard.prototype.deckShuffler = function() {
if (this.deck.children_.length < 1) {
this.deck.children_ = this.shuffle(this.discard.children_);
}
};
sideboard.prototype.shuffle = function(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
};
Don't you just want to concatenate the two arrays and then reassign ? For example:
this.deck.children_ = this.deck.children_.concat(this.shuffle(this.discard.children_));
精彩评论