beooooook
2017-08-29 10:31
var fruitObj = function() { this.alive = []; this.x = []; this.y = []; this.orange = new Image(); this.bule = 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.born(i); } this.orange.src = "./src/fruit.png"; this.bule.src = "./src/bule.png"; } fruitObj.prototype.draw = function() { for(var i = 0;i < this.num; i++) { ctx2.drawImage(this.orange, this.x[i] , this.y[i]); } } function.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]; }
以下是main.js
var can1; var can2; var ctx1; var ctx2; var canWidth; var canHeight; var lastTime; var deltaTime; var bgPic = new Image(); 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);//setInterval,setTimeout,frame per second var now =Date.now(); deltaTime =now - lastTime; lastTime = now; drawBackground(); ane.draw(); fruit.draw(); }
函数名写错了
我也出不来,我是把初始化里面的x和y等于0去掉了,就可以了
我也检查的脑袋都炸了
this.bule.src = "./src/bule.png";
这个应该是blue吧,,,
HTML5小游戏---爱心鱼(上)
92350 学习 · 551 问题
相似问题