无法在Phaser 2.3.0中添加图像

时间:2015-07-01 10:02:38

标签: javascript html5 canvas phaser-framework

我正在尝试将图像添加到画布中但是我收到以下错误:

Uncaught TypeError: Cannot read property 'hasLoaded' of undefined

它说错误来自这一行:

this.add.image('block',100,255);

的index.html:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Camera Learning</title>
        <script src="js/phaser.min.js"></script>
        <script src="js/states/boot.js"></script>
    </head>
    <body>
        <script>
            (function(){
                var game = new Phaser.Game(450, 510, Phaser.AUTO, 'game');
                game.state.add('boot', boot);
                game.state.start('boot');
            })();
        </script>
    </body>
</html> 

boot.js:

boot = function(game){
    console.log("%cStarting my awesome game", "color:white; background:red");
};

boot.prototype = {

    preload: function(){
        this.load.image('block','./res/borderblock.png');
    },

    create: function(){
        console.log('adding');
        this.add.image('block',100,255);
        console.log('added');       
    }

};

2 个答案:

答案 0 :(得分:1)

this.add.image('block',100,255);应该是this.add.image(100, 255, 'block'); - 参数顺序很重要,当方法收到字符串而不是数字时,事情自然不起作用。在这种情况下,你有x,y和图像的键。

答案 1 :(得分:0)

要添加图片this.add.image(x,y,'key');,您也可以使用&#39; sprite&#39;比如this.add.sprite(x,y,'key');