xiao55555
2015-12-11 21:46
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{
padding-top:10px;
}
.main{
width:800px;
height:600px;
margin:0 auto;
}
#allcanvas{
width:800px;
height:600px;
margin:0 auto;
position:relative;
}
#canvas1{
position:absolute;
bottom:0;
left:0;
z-index:1;
}
#canvas2{
position:absolute;
bottom:0;
left:0;
z-index:1;
}
</style>
</head>
<body>
<div class="main">
<div id="allcanvas">
<div id="canvas1" height='600' width='800'></div>
<div id="canvas2" height='600' width='800'></div>
</div>
</div>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/commonFunctions.js"></script>
<script type="text/javascript" src="js/bg.js"></script>
</body>
</html>
// JavaScript Document
var can1;
var can2;
var ctx1;
var ctx2;
var lastTime;
var deltaTime;
var canWidth;
var canHeight;
var bgPic=new Image();
document.body.onload=game;
function game(){
init();
lastTime=Date.now();
deltaTime=0;
gameloop();
}
function init(){
//获得canvas context
can1=document.getElementById('canvas1');//fish dust UI circle
ctx1=can1.getContext('2d');
can2=document.getElementById('canvas2');//background one fruits
ctx2=can2.getContext('2d');
bgPic.src="./src/background.jpg";
canWidth=can1.width;
canHeight=can2.height;
}
function gameloop(){
window.requestAnimFrame(gameloop);//循环
var now=Date.now();
deltaTime=now-lastTime;
lastTime=now;
}
// JavaScript Document
function drawBackground(){
ctx2.drawImage(bgPic,0,0,canWidth,canHeight);
}
应该是canvas 不是div
window.requestAnimFrame(gameloop); 这里也写错了、
应该是window.requestAnimationFrame(gameloop);
你的问题解决了吗?我出现了和你一样的问题 你是怎么解决的
我们是一样的问题,但是,我去js文件下看到我的background.js根本就是0KB,所以,你看一下你的会不会是在编辑器下没有保存你的background.js,我的是这个问题,保存了背景就出来了
var can1;
var can2;
var ctx1;
var ctx2;
var canWidth;
var canHeight;
var lastTime;
var deltaTime;
var bgPic=new Image();
document.body.onload=game;
function game () {
init();
lastTime=Date.now();
deltaTime=0;
gameloop();
}
function init () {
can1=document.getElementById("canvas1");
ctx1=can1.getContext("2d");
can2=document.getElementById("canvas2");
ctx2=can2.getContext("2d");
canWidth=can1.width;
canHeight=can1.height;
bgPic.src="./src/background.jpg";
}
function gameloop () {
requestAnimationFrame(gameloop);//setInterval setTimeout
var now=Date.now(0);
deltaTime=now-lastTime;
lastTime=now;
drawBackground();
}
就是加下划线的地方报错 好烦
这里老是报错can1.getContext is not a function 不知道为什么 求帮助
最好写成相对路径
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{
padding-top:10px;
}
.main{
width:800px;
height:600px;
margin:0 auto;
}
#allcanvas{
width:800px;
height:600px;
margin:0 auto;
position:relative;
}
#canvas1{
position:absolute;
bottom:0;
left:0;
z-index:1;
}
#canvas2{
position:absolute;
bottom:0;
left:0;
z-index:1;
}
</style>
</head>
<body>
<div class="main">
<div id="allcanvas">
<div id="canvas1" height='600' width='800'></div>
<div id="canvas2" height='600' width='800'></div>
</div>
</div>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/commonFunctions.js"></script>
<script type="text/javascript" src="js/bg.js"></script>
</body>
</html>
// JavaScript Document
var can1;
var can2;
var ctx1;
var ctx2;
var lastTime;
var deltaTime;
var canWidth;
var canHeight;
var bgPic=new Image();
document.body.onload=game;
function game(){
init();
lastTime=Date.now();
deltaTime=0;
gameloop();
}
function init(){
//获得canvas context
can1=document.getElementById('canvas1');//fish dust UI circle
ctx1=can1.getContext('2d');
can2=document.getElementById('canvas2');//background one fruits
ctx2=can2.getContext('2d');
bgPic.src="./src/background.jpg";
canWidth=can1.width;
canHeight=can2.height;
}
function gameloop(){
window.requestAnimFrame(gameloop);//循环
var now=Date.now();
deltaTime=now-lastTime;
lastTime=now;
drawBackground();
}
// JavaScript Document
function drawBackground(){
ctx2.drawImage(bgPic,0,0,canWidth,canHeight);
}
没看到你调用 drawBackground 这个画背景的方法呀
HTML5小游戏---爱心鱼(上)
92350 学习 · 551 问题
相似问题