为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

正在回答

4 回答

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对象里是不对的


0 回复 有任何疑惑可以回复我~
#1

java家洼

你好,你说“ ane是实例化对象,写在fruitObj对象里是不对的”,我觉得你说的不对,因为ane是海葵的实例对象,不是果实的实例对象,在果实类定义中用到其他类的实例对象怎么就不行呢?要是用到自己类的实例对象才不行呢。你说是不是?
2016-06-23 回复 有任何疑惑可以回复我~
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;
		}
	};
};


0 回复 有任何疑惑可以回复我~

是啊,而且在出生的时候,也给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;

}

};

};


0 回复 有任何疑惑可以回复我~

  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;

           }

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


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信