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

来源:2-6 果实绘制(果实数量控制)

喵喵咪呀巴拉拉

2016-01-27 23:51

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

写回答 关注

4回答

  • 睡觉青蛙
    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对象里是不对的


    java家洼

    你好,你说“ ane是实例化对象,写在fruitObj对象里是不对的”,我觉得你说的不对,因为ane是海葵的实例对象,不是果实的实例对象,在果实类定义中用到其他类的实例对象怎么就不行呢?要是用到自己类的实例对象才不行呢。你说是不是?

    2016-06-23 21:18:41

    共 1 条回复 >

  • 喵喵咪呀巴拉拉
    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;

               }

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


HTML5小游戏---爱心鱼(上)

学做HTML5游戏,轻轻松松带你上手,适合刚入手游戏开发的同学

92350 学习 · 551 问题

查看课程

相似问题