mardi 8 septembre 2015

Basic error with JavaScript; Revealing Module Pattern style

This is my first question in stackoverflow:

I have a problem with my code in Javascript. I'm new writting in this language and I don't know where is the problem in this Module, but the error is

Uncaught TypeError: Cannot read property 'init' of undefined.

I want to begin with HTML5/JS games and i'm a little nervous because this problem I have since few days.... Thank you, guys!

var game = (function() {
    //////////////////////////////////////////////////
    /////////////////Atributos del juego/////////////////
    ////////////////////////////////////////////////////////
    var canvas = null,
        ctx = null,
        gameover = false,
        pause = false,
        score = 0;

    //////////////////////////////////////////////////
    ////////////////////Métodos privados/////////////////
    ////////////////////////////////////////////////////////
    window.requestAnimationFrame = (function(callback) //Función autoejecutable que detecta la compatibilidad del navegador con la animación
        {
            return window.requestAnimationFrame ||
                window.webkitRequestAnimationFrame ||
                window.mozRequestAnimationFrame ||
                window.oRequestAnimationFrame ||
                window.msRequestAnimationFrame ||
                function(callback) {
                    window.setTimeout(callback, 1000 / 60);
                };
        })();

    function loop() //Actualiza los estados y dibuja los elementos durante la partida
    {
        update();
        draw();
    }

    function update() //Actualiza el estado del juego
    {
        window.requestAnimationFrame(update);

    }

    function draw() //Dibuja los elementos del juego en el canvas
    {
        ctx.drawImage(buffer, 0, 0); //Dibujamos el buffer en el contexto
    }

    //////////////////////////////////////////////////
    ////////////////////Métodos públicos/////////////////
    ////////////////////////////////////////////////////////
    function init() {
        var i = 0;
        alert(i);
    }

    return //Devuelve un objeto con todos los métodos públicos
    {
        init: init;
    };
}());

game.init();

Aucun commentaire:

Enregistrer un commentaire