果实颜色判断出错
rt,应该是collision中判断fruit.type[i]时出现的问题,有时大鱼吃到橙色果实时score.double会置2,有时吃到蓝色果实double会置1,因为鱼身颜色是根据score.double判断的,因此也会出现吃到橙色果实时变蓝,吃到蓝色果实变红的情况。
collision代码如下,求解,多谢各位了
function collision(){ for(var i = 0; i < fruit.num; i++){ if(fruit.alive[i]){ //calculate dist var dist = calLength2(mom.x, mom.y, fruit.x[i], fruit.y[i]); if(dist < 200){ //eaten fruit.dead(i); fruit.born(i); //score.fruitNum = score.double == 2? 2 * score.fruitNum : score.fruitNum + 1; score.fruitNum++; mom.bigBodyCnt = mom.bigBodyCnt >= 7 ? 7 : mom.bigBodyCnt + 1; //score.double = fruit.type[i] == "blue"? 2 : 1; //?????????note: sometimes the double doesn't work console.log("fruit[" + i + "]: " +fruit.type[i]); if(fruit.type[i] == "blue"){ score.double = 2; } } } } } function fishCollision(){ var dist = calLength2(mom.x, mom.y, child.x, child.y); if(dist < 900){ //child recover child.childBodyCnt = 0; //score reset mom.bigBodyCnt = 0; score.addScore(); //score.reset(); } }
==============================
刚刚对每个被吃掉的果实类型进行了打印,发现有些的颜色和type里的blue/orange对不上,怀疑也许是由于我的fruit再生不是用的老师的monitor机制,而是被吃掉或飘出屏幕就执行dead()然后born()。但是感觉这期间也对fruit.type进行了重新初始化,不该有错啊。
fruitObj.prototype.draw = function(){ for(var i = 0; i < this.num; i++){ if(this.alive[i]){ var pic = this.type[i] == "blue"? this.blue : this.orange; if(this.size[i] <= 15){ //grow this.size[i] += this.spd[i] * interval; }else{ this.y[i] -= this.spd[i] * 7 * interval; } ctx2.drawImage(pic, this.x[i] - this.size[i] * 0.5, this.y[i] - this.size[i] * 0.5, this.size[i], this.size[i]); if(this.y[i] < -10){ this.dead(i); this.born(i); } } } } fruitObj.prototype.born = function(i){ var weedId = Math.floor(Math.random() * weed.num); this.x[i] = weed.x[weedId] ; this.y[i] = canHeight - weed.len[weedId]; this.size[i] = 0; var ran = Math.random(); this.type[i] = ran < 0.2 ? "blue" : "orange"; this.alive[i] = true; } fruitObj.prototype.dead = function(i){ this.alive[i] = false; this.type[i] = "" }