為什么我的果實(shí)上浮時(shí)是一條直線
如圖,果實(shí)果實(shí)上浮時(shí)是一條直線,而且上浮時(shí)離開頁面直線就消失了,回到頁面又出現(xiàn),
代碼:fruit.js
// JavaScript Document
var fruitObj = function() {
this.alive = []; ? ? ?//bool
this.x = [];
this.y = [];
this.l = []; ? ? ? ? ? ?//果實(shí)的半徑(大小)
this.spd = []; ? ? ? ? ?//每個(gè)果實(shí)自己的速度?
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] = true;
this.x[i] = 0;
this.y[i] = 0;
this.l[i] = 0;
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.l[i]<=14){
this.l[i] += 0.01 *deltaTime;
}
else{
this.y[i] -= 0.07 *deltaTime;
}
ctx2.drawImage(this.orange, this.x[i]-this.l[i] / 2, this.y[i]-this.l[i] / 2, this.l[i], this.l[i]);
}
}
fruitObj.prototype.born = function(i){
var aneID = Math.floor(Math.random()*ane.num);
this.x[i] = ane.x[aneID];
this.y[i] = canHeight - ane.len[aneID];
this.l[i] = 0;
}
main.js
// JavaScript Document
var can1,can2,ctx1,ctx2;
var lastTime;
var deltaTime;
var bgPic = new Image();
var canWidth,canHeight;
var ane;
var fruit;
document.body.onload = game;
function game(){
init();
lastTime=Date.now();
deltaTime = 0;
gameloop();
}
function init(){
//獲取canvas context?
? ? can1=document.getElementById("canvas1");//fishes,dust,ui ?circle
? ? ctx1=can1.getContext('2d'); ?
? ? can2=document.getElementById("canvas2"); ?//background,ane,fruits
? ? ctx2=can2.getContext('2d');
bgPic.src = "./src/background.jpg";
canWidth = can1.width;
canHeight = can1.height;
ane = new aneObj();
ane.init();
fruit = new fruitObj();
fruit.init();
}
function gameloop(){
window.requestAnimFrame(gameloop);
var now = Date.now();
deltaTime = now - lastTime;
lastTime = now;
drawBackground();
ane.draw();
fruit.draw();
}
求解,萬分感謝
2016-11-01
代碼沒有問題,是不是瀏覽器卡了? ?我?guī)湍銣y試了 ?在我的上面正常顯示
2016-11-12
不是采納我的答案成功的么?
2016-11-06
是的。都是在gameloop繪制圖片和操作相關(guān)的啊
2016-11-06
ctxtwo.drawImage(bgPic,0,0);在gameloop()方法里面加上這句話就可以了。