为什么我的代码是照着老师的敲得,但是果实却只能生长一个,一开始的30个不见了
为什么我的代码是照着老师的敲得,但是果实却只能生长一个,一开始的30个不见了
为什么我的代码是照着老师的敲得,但是果实却只能生长一个,一开始的30个不见了
2016-01-11
function drawBackground(){
ctx2.drawImage(bgPic,0,0,canW,canH);
}
/**ane海葵*/
var aneObj = function(){
this.x = [];
this.len = [];
}
aneObj.prototype.num = 50; //数量
aneObj.prototype.init = function(){
for(var i=0;i<this.num;i++){
this.x[i] = i*16+Math.random()*20;
this.len[i] = 160+Math.random()*50;
}
};
//画海葵
aneObj.prototype.draw = function(){
ctx2.save();
ctx2.globalAlpha = 0.6;//透明度
ctx2.lineWidth = 20;
ctx2.lineCap = "round";
ctx2.strokeStyle = "#9AFF9A";
for(var i=0;i<this.num;i++){
ctx2.beginPath();
ctx2.moveTo(this.x[i],canH);
ctx2.lineTo(this.x[i],canH-this.len[i]);
ctx2.stroke();
}
ctx2.restore();
};
/*fruit 果实*/
var fruitObj = function(){
this.alive = []; //bool是否有果实
this.x =[];
this.y=[];
this.L =[];//果实的坐标变动移动
this.sd =[];//果实的速度
this.fruitType = []; //果实类型
this.orange = new Image(); //黄色果实
this.blue = new Image();//蓝色果实
}
fruitObj.prototype.num = 30;
fruitObj.prototype.init = function(){
for(var i=0;i<this.num;i++){
this.alive[i] = false ;
this.x[i] =0;
this.y[i]=0;
this.sd[i]= Math.random()*0.01+0.003;
//this.born(i);
this.fruitType[i] ="";
}
this.orange.src = "./src/fruit.png";
this.blue.src = "./src/blue.png";
}
//画果实 果实成熟时出现果实 果实最大显示多少个果实
fruitObj.prototype.draw = function(){
for(var i=0;i<this.num;i++){
//draw 画果实
if(this.alive[i]) {
var pic;
if(this.fruitType[i] == "blue"){
pic = this.blue;
}else{
pic = this.orange;
}
if (this.L[i] <= 20) {
this.L[i] += this.sd[i] * deltaTime;
} else {
this.y[i] -= this.sd[i] * 2 * deltaTime;
}
ctx2.drawImage(pic, this.x[i] - this.L[i] * 0.5, this.y[i] - this.L[i] * 0.5, this.L[i], this.L[i]);
//
if (this.y[i] < 10) {
this.alive[i] = false;
}
}
}
}
fruitObj.prototype.born = function(i){
//记录坐标 海葵生长位置
var aneID = Math.floor(Math.random()*ane.num); //随机排重
this.x[i] = ane.x[aneID];
this.y[i] = canH - ane.len[aneID];
this.L[i] = 0;
this.alive[i] = true;
// 果实类型
var ran = Math.random();
if(ran<0.25){
this.fruitType[i]= "blue";
}else{
this.fruitType[i]= "orange";
}
}
//判断当前屏幕显示的果实数量
function fruitMonitor (){
var num = 0;
for(var i=0;i<fruit.num;i++){
if(fruit.alive[i])num++;
}
if(num<15){
senFruit();
return;
}
}
function senFruit(){
for(var i=0;i<fruit.num;i++){
if(!fruit.alive[i]){
fruit.born(i);
return;
}
}
}
按照我的对比一下
var fruitObj= function()
{
this.alive= [];//是否活着,这里是一个booleans(布尔值)
this.x=[];
this.y=[];
this.l=[];
this.spd=[];//果实的速度
this.blue=new Image();
this.orange= new Image();
}
fruitObj.prototype.num= 30;
fruitObj.prototype.init= function()//初始化
{
for (var i=0; i<this.num; i++)
{
//this.alive[i]= false;定义果实的初始值为false代表这个时期果实处于休眠
this.alive[i]= false;
this.x[i]= 0;
this.y[i]= 0;
this.spd[i]= Math.random()*0.017+0.003;//果实漂浮的速度随机[0.003,0.02)
this.born(i);
}
this.blue.src="./src/blue.png";
this.orange.src="./src/fruit.png";
}
fruitObj.prototype.draw= function()//画果实
{
//果实的两个状态,一个是长在海葵上面,另一个是成熟后脱离海葵;
for (var i=0; i<this.num; i++)
{
//draw画果实
// find an one(找个一个果实),grow(成长),fly up(上浮)
if(this.alive=[i])
{
if(this.l[i]<=15)
{
this.l[i]+= this.spd[i]*deltaTime;//使过程变得平滑
}
else
{
this.y[i]-=this.spd[i]*7*deltaTime;
}
ctx2.drawImage(this.orange,this.x[i]-this.l[i]*0.5,this.y[i]-this.l[i]*0.5,this.l[i],this.l[i]);
if(this.y[i]< 10)
{
this.alive[i]= false;
}
}
}
}
fruitObj.prototype.born= function(i)//果实的成长
{
//随机找一个海葵
var aneId=Math.floor(Math.random()*ane.num);
this.x[i]= ane.x[aneId];//果实的x值
this.y[i]= canHeight-ane.len[aneId];//果实的y值
this.l[i]= 0;
this.alive[i]= true;
}
fruitObj.prototype.update= function()//果实的更新
{
var num= 0;
for (var i=0; i<fruit.num; i++)
{
if (fruit.alive[i])
num++;
}
}
function fruitMonitor()//果实的监听功能
{
var num = 0;//果实的数据统计
for (var i= 0; i<fruit.num; i++)//判断屏幕上当前有多少个果实是活着的
{
if (fruit.alive[i])
num++;
//如果状态就是一个活着的状态,我们就来做一个数据统计
}
if (num<15)
//如果小于15个
{
//send fruit 让一个果实出生
sendFruit();
return;
}
}
function sendFruit()
{
for (var i=0; i<fruitObj.prototype.num; i++)
{
if(fruit.alive[i]= false)
{
fruit.born(i);
return;
}
}
}
举报