问答详情
源自:-

背景无法绘制出来

Cannot read property 'drawImage' of undefined,请问大家如何修复这个bug

提问者:拼命柯南 2015-11-18 15:17

个回答

  • 拼命柯南
    2015-11-18 18:22:02

    function drawBackground()

    {

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

    }

    这是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()

    {

    //获得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;


    }

    function gameloop()

    {

    requestAnimFrame(gameloop);//setInterval,setTimeout,frame per second

    var now =Date.now();

    deltaTime =now - lastTime;

    lastTime =now;


    drawBackground();

    }

    这是main.js

  • wslover
    2015-11-18 17:14:22

    调用这个函数的对像不对吧,把你的代码贴上来吧