问答详情
源自:2-6 果实绘制(果实数量控制)

果实运动的速度越来越快,求解,已初始化

果实运动的速度越来越快,求解,已初始化

提问者:喵喵咪呀巴拉拉 2016-01-27 23:51

个回答

  • 睡觉青蛙
    2016-03-02 12:28:28

    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;

    其实我觉得这里把ane的实例化对象写进去是不妥的。,

        var aneId = Math.floor(Math.random() * ane.num); ane是实例化对象,写在fruitObj对象里是不对的


  • 喵喵咪呀巴拉拉
    2016-01-28 21:44:10

    var fruitObj = function() {
    	this.x = [];
    	this.y = [];
    	this.l = [];
    	this.alive = [];
    	this.speed = [];
    	this.orange = new Image();
    	this.blue = new Image();
    	this.fruitType = [];
    }
    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.l[i] = 0;
     		this.fruitType[i] = '';
     		this.speed[i] = Math.random() * 0.0003 + 0.00001; //[ 0.00001 , 0.00051)
     		this.born(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++) {
    		if( this.alive[i]){
    			if( this.fruitType[i] == 'blue' ){
    				var pic = this.blue;
    			}else{
    				var pic = this.orange;
    			}
    			if( this.l[i] <= 14){
    				this.l[i] += this.speed[i] * deltaTime;
    			}else{
    				this.y[i] -= this.speed[i] * deltaTime;
    			}
    			ctx2.drawImage( pic , this.x[i] - this.l[i] , this.y[i] - this.l[i] , 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.3 ){
    		this.fruitType[i] = 'blue'
    	}else{
    		this.fruitType[i] = 'orange';
    	}
    }
    fruitObj.prototype.fruitManger = function() {
    	var num = 0;
    	for (var i = 0; i < fruit.num; i++) {
    		if(fruit.alive[i]){
    			num++;
    		}
    	}
    	if( num < 15 ){
    		fruit.rebornFruit();
    		return;
    	}
    }
    fruitObj.prototype.rebornFruit = function() {
    	for (var i = 0; i < fruit.num; i++) {
    		if(!fruit.alive[i]){
    			fruit.born(i);
    			return;
    		}
    	};
    };


  • 喵喵咪呀巴拉拉
    2016-01-28 21:43:38

    是啊,而且在出生的时候,也给this.l[i]=0了

    var fruitObj = function() {

    this.x = [];

    this.y = [];

    this.l = [];

    this.alive = [];

    this.speed = [];

    this.orange = new Image();

    this.blue = new Image();

    this.fruitType = [];

    }

    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.l[i] = 0;

      this.fruitType[i] = '';

      this.speed[i] = Math.random() * 0.0003 + 0.00001; //[ 0.00001 , 0.00051)

      this.born(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++) {

    if( this.alive[i]){

    if( this.fruitType[i] == 'blue' ){

    var pic = this.blue;

    }else{

    var pic = this.orange;

    }

    if( this.l[i] <= 14){

    this.l[i] += this.speed[i] * deltaTime;

    }else{

    this.y[i] -= this.speed[i] * deltaTime;

    }

    ctx2.drawImage( pic , this.x[i] - this.l[i] , this.y[i] - this.l[i] , 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.3 ){

    this.fruitType[i] = 'blue'

    }else{

    this.fruitType[i] = 'orange';

    }

    }

    fruitObj.prototype.fruitManger = function() {

    var num = 0;

    for (var i = 0; i < fruit.num; i++) {

    if(fruit.alive[i]){

    num++;

    }

    }

    if( num < 15 ){

    fruit.rebornFruit();

    return;

    }

    }

    fruitObj.prototype.rebornFruit = function() {

    for (var i = 0; i < fruit.num; i++) {

    if(!fruit.alive[i]){

    fruit.born(i);

    return;

    }

    };

    };


  • 误尽此生
    2016-01-28 21:30:49

      if (this.alive[i]) {

               if (this.l[i] <= 14) {

               this.l[i] += this.spd[i] * deltaTime;

               }

               else{

            this.y[i] -= this.spd[i] * 7 * deltaTime;

               }

    你的代码是这样吗,我这样写的没有问题哦