Hi! Please if you can help me.
I am learning from an older tutorial and I encounter this error ERROR: ‘cc.random0To1’ is deprecated, please use ‘Math.random’ instead
But after replacing ‘cc.random0To1’ with ‘Math.random’, candy don’t spawn in my game.
This is the code, I’m stuck here and I don’t know how to do it
cc.Class({
extends: cc.Component,
properties: {
background:{
default: null,
type: cc.Node
},
scoreLabel: {
default: null,
type: cc.Label
},
lifeLabel: {
default: null,
type: cc.Label
},
candies:{
default: [],
type: cc.Prefab
},
candyLayer:{
default: null,
type: cc.Layout
},
tempoMin: 0,
tempoMax: 0,
score: 0,
maxlife: 0,
maxCandy: 0,
isLaunch: false
},
onLoad: function () {
var candiesCount = 0;
var delta = 0;
var self = this;
self.scheduleOnce(function(){
self.initGame();
}, 1);
self.node.on('candy_die', function(event){
candiesCount--;
}, self);
this.candyLayer.node.setContentSize(this.background.width, this.background.height);
},
initGame: function(){
this.life = this.maxLife;
this.score = 0; //start the Score
this.lifeLabel.string = "x " + this.maxlife;
this.isLaunch = true;
},
spawCandy: function (dt){
if (this.isLaunch === false) {
return;
}
if(this.candiesCount > this.maxCandy) {
return;
}
this.delta += dt;
if (this.delta < 1) {
return;
}
this.delta = 0;
var candyLayer = this.candyLayer.node;
var winsize = candyLayer.getContentSize();
var candiesAmount = this.candies.length -1;
var candy = cc.instantiate(this.candies[Math.cell(candiesAmount * Math.random())]);
candy.setPosition(this.newCandyPosition());
var tempo = Math.random() * this.tempoMax + this.tempoMin;
var self = this;
var moveby = cc.moveBy(tempo, 0, -winsize.height-30);
var sequence = cc.sequence(moveby, cc.removeSelf(true), cc.callFunc(function() {self._candiesCount--;}, self));
candy.runAction(sequence);
candyLayer.addChild(candy);
this.candiesCount++;
},
newCandyPosition: function(){
var winsize = this.candyLayer.node.getContentSize();
var x = winsize.width * Math.random();
var y = winsize.height - 100;
return cc.p(x,y);
},
update: function (dt) {
this.spawCandy(dt);
},
});
1 post - 1 participant