0

i start to learn javascript with easy game tutorial http://www.youtube.com/watch?v=U7anheKJSaIand on RoR got error in index.html.erb:

<script>

    var ctx, canvas;
    var data;

    window.onload = function main(){

        canvas = document.createElement("canvas");
        canvas.width = canvas.height = 200;
        ctx = canvas.getContext("2d");

        document.body.appendChild(canvas);
        init();
        tick();
    } 

    function init(){
        data = new Tile(20,20);
    }

    function tick(){
        window.requestAnimationFrame(tick);

        update();
        render();
    }

    function update(){}

    function render(){
        data.draw(ctx);
    }

    function Tile(x,y){
        var x = x, y = y;
        var tile = Tile.BlANK;

        if(tile == null){
            var _c = document.createElement("canvas");
            _c.width = _c.height = 100;
            _ctx = _c.getContext("2d");

            _ctx.fillStyle = "skyblue";
            //blank
            _ctx.fillRect(0,0,100,100);
            Tile.BlANK = new Image();
            Tile.BLANK.src = _c.toDataURL();

            tile = Tile.BLANK;
        }  

    this.update = function(){}  

    this.draw = function(ctx){
        ctx.drawImage(tile, x, y);
    }

}

</script>

and this is error:

Tile.BLANK.src = _c.toDataURL();

Uncaught TypeError: Cannot set property src of undefined

1 Answer 1

1

Your error is here

Tile.BlANK = new Image();

It should be:

Tile.BLANK = new Image();

The error comes from .BlANK and it's because there is a typo. The l (L) is lowecase, and therefore later when you are trying to access the .src in you can't, because the real .BLANK is still undefined.

This is in function Tile(x,y){...}, but you also have the typo some other places. Just check your code for lowercase letters in .BLANK.

Sign up to request clarification or add additional context in comments.

1 Comment

how improve attentiveness?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.