3 回答
TA贡献1条经验 获得超0个赞
var momObj = function()
{
this.x;
this.y;
this.angle;
this.bigEye = new Image();
this.bigBody = new Image();
this.bigTail = new Image();
}
momObj.prototype.init = function()
{
this.x = canWidth * 0.5;
this.y = canHeight * 0.5;
this.angle = 0;
this.bigEye.src = "./src/bigEye0.png";
this.bigBody.src = "./src/bigSwim0.png";
this.bigTail.src = "./src/bigTail0.png";
}
momObj.prototype.draw = function()
{
this.x = lerpDistance(mx, this.x, 0.99);
this.y = lerpDistance(my, this.y, 0.99);
var deltaY = my - this.y;
var deltaX = mx - this.x;
var beta = Math.atan2(deltaY, deltaX)+ Math.PI;
this.angle = lerpAngle(beta, this.angle, 0.6);
ctx2.save();
ctx2.translate(this.x, this.y);
ctx2.rotate(this.angle);
ctx2.drawImage(this.bigEye, -this.bigEye.width * 0.5, -this.bigEye.height * 0.5);
ctx2.drawImage(this.bigBody, -this.bigBody.width * 0.5, -this.bigBody.height * 0.5);
ctx2.drawImage(this.bigTail, -this.bigTail.width * 0.5 + 30, -this.bigTail.height * 0.5);
ctx2.restore();
}
main.js
var can1;
var can2;
var ctx1;
var ctx2;
var canWidth;
var canHeight;
var lastTime;
var detaTime;
var bgPic = new Image();
var ane;
var fruit;
var mom;
var mx;
var my;
document.body.onload = game;
function game()
{
init();
lastTime = Date.now();
detaTime = 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');
can2.addEventListener('mousemove',onMouseMove, false);
bgPic.src = "./src/background.jpg";
canWidth = can1.width;
canHeight = canvas1.height;
ane = new aneObj();
ane.init();
fruit = new fruitObj();
fruit.init();
mom = new momObj();
mom.init();
mx = canWidth * 0.5;
my = canHeight * 0.5;
}
function gameloop()
{
window.requestAnimFrame(gameloop);
var now = Date.now();
detaTime = now - lastTime;
lastTime = now;
drawBackground();
ane.draw();
fruitMonitor();
fruit.draw();
ctx1.clearRect(0, 0, canWidth, canHeight);
mom.draw();
}
function onMouseMove(e)
{
if(e.offSetX || e.layerX)
{
mx = e.offSetX == undefined ? e.layerX : e.offSetX;
my = e.offSetY == undefined ? e.layerY : e.offSetY;
//console.log(mx);
}
}
- 3 回答
- 0 关注
- 1290 浏览
添加回答
举报