背景出不来

来源:2-1 绘制背景

独一无

2015-11-01 10:56

var can1;

 var can2;

 var ctx1;

 var ctx2;

 var lastTime;

 var deltaTime;

 var bgPic=new Image();

 var canWidth;

 var canHeight;

document.body.onload=game;

function game(){

lastTime=Date.now();

deltaTime=0;

init();

gameloop();

}

function init(){

can1=document.getElementById("canvas1");

can2=document.getElementById("canvas2");

ctx1=can1.getContext("2d");

ctx2=can2.getContext("2d");

bgPic.src="src/background.jpg";

canWidth=canvas1.width;

canHeight=canvas1.height;

}


function gameloop(){

window.requestAnimFrame(gameloop);

var now=Date.now();

deltaTime=now-lastTime;

lastTime=now;

drawBackground();

}

http://img.mukewang.com/56357ea20001768a11230111.jpg

写回答 关注

3回答

  • MIS_Lu
    2015-11-30 19:53:07

    求解答!!!!!!!!!!!!!!!!!!

  • MIS_Lu
    2015-11-30 19:52:46

    和你一样的问题,我的代码是

    var can1,can2;

    var ctx1,ctx2;

    var lastTime,deltaTime;

    var bgPic = new Image();

    var canWidth;

    var canHeight;


    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');

    bgPic.src = "./src/background.jpg";

    canWidth = can1.width;

    canHeight = can1.height;

    }


    function gameloop () {

    window.requestAnimFrame(gameloop);

    var now = Date.now();

    deltaTime = now-lastTime;

    lastTime = now;

    drawBackground();


    }

    background.js的代码是:

    function drawBackground () {

    ctx2.drawImage(bgPic,0,0,canWidth,canHeight);


    }


  • 月下风物语
    2015-11-10 16:13:59

    你的这里:

    canWidth=canvas1.width;

    canHeight=canvas1.height;

    改成:

    canWidth=can1.width;

    canHeight=can1.height;

    试下


HTML5小游戏---爱心鱼(上)

学做HTML5游戏,轻轻松松带你上手,适合刚入手游戏开发的同学

92350 学习 · 551 问题

查看课程

相似问题