課程
/前端開發(fā)
/HTML/CSS
/HTML5小游戲---愛心魚(上)
果實(shí)運(yùn)動(dòng)的速度越來越快,求解,已初始化
2016-01-27
源自:HTML5小游戲---愛心魚(上) 2-6
正在回答
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;
其實(shí)我覺得這里把a(bǔ)ne的實(shí)例化對(duì)象寫進(jìn)去是不妥的。,
? ? var?aneId?=?Math.floor(Math.random()?*?ane.num); ane是實(shí)例化對(duì)象,寫在fruitObj對(duì)象里是不對(duì)的
java家洼
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; } }; };
是啊,而且在出生的時(shí)候,也給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;
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'
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() {
if(!fruit.alive[i]){
fruit.born(i);
? 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;
你的代碼是這樣嗎,我這樣寫的沒有問題哦
舉報(bào)
學(xué)做HTML5游戲,輕輕松松帶你上手,適合剛?cè)胧钟螒蜷_發(fā)的同學(xué)
1 回答給每個(gè)果實(shí)速度時(shí),敲好代碼后,瀏覽器不顯示果實(shí)了。
1 回答drawBackground()是否放在初始化的時(shí)候呢
1 回答果實(shí)運(yùn)動(dòng)過程過閃爍
2 回答我的果實(shí)不管是大小還是速度什么的都好像被放大了一樣,賊大賊快了
2 回答果實(shí)都出不來,求大佬指出
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-03-02
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;
其實(shí)我覺得這里把a(bǔ)ne的實(shí)例化對(duì)象寫進(jìn)去是不妥的。,
? ? var?aneId?=?Math.floor(Math.random()?*?ane.num); ane是實(shí)例化對(duì)象,寫在fruitObj對(duì)象里是不對(duì)的
2016-01-28
2016-01-28
是啊,而且在出生的時(shí)候,也給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
? 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;
? ? ? ? ? ?}
你的代碼是這樣嗎,我這樣寫的沒有問題哦