簡體   English   中英

JSON文件中的相位器資產加載問題

[英]Phaser assets loading issue from JSON file

我有一個移相器游戲的以下代碼。 它只是通過使用AJAX從JSON文件中加載信息來獲得關卡的直觀構想。

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>hello phaser!</title>
        <script src="phaser.min.js"></script>
        <script src="jquery.min.js"></script>
    </head>
    <body>
    <div id="results"></div>
    <script type="text/javascript">



        var game = new Phaser.Game(1024, 768, Phaser.AUTO, '', { preload: preload, create: create,renderer:renderer});
        var background;

        function preload () 
        {

                game.load.image('bg','bg.png');
                game.load.image('mothership', 'bird.png');
                game.load.image('turret','turret.png');





        }

        function create () 
        {
            //game.add.sprite(31,725,"mothership");
            //game.add.sprite(100,100,"turret");
            //background=game.add.sprite(0,0,"bg");
            //background.scale.setTo(5,7);


        }

        function renderer()
        {

        }
        $.ajax({
                        url: "TestFile5.json",
                        dataType: 'json',
                        contentType:"application/json",
                        success: function(response) {
                            //here you can use the response
                                //response can be passed to what ever js file you need.


                                game.add.sprite(response.mothership.posx, response.mothership.posy, "mothership");

                                game.add.sprite(response.Turret0.posx, response.Turret0.posy, "turret");
                                game.add.sprite(response.Turret1.posx, response.Turret1.posy, "turret");
                                game.add.sprite(response.Turret2.posx, response.Turret2.posy, "turret");

                        },
                        error: function(response1){
                                alert('response error')
                        }
                    });






    </script>
</Body>
</html>

我已經將其與JSON文件和圖像一起加載到了我的在線服務器上,但似乎沒有使用AJAX回調中的game.add.sprite()函數加載圖像。 我得到帶有占位符圖像的屏幕。 位置正確,但未加載圖像。 是什么原因造成的,我該如何解決?

檢查瀏覽器中的“開發工具”面板(IE中為F12,Chrome中為ctrl + shift + j),然后檢查“網絡”面板,以100%確保資產確實已加載。 如果圖像加載失敗,您還將在控制台中也看到一個條目,因為Phaser會在該處引發警告。

另外值得一提的是,Phaser可以加載json文件。 因此,您可以使用Boot狀態啟動游戲,該Boot狀態會在其中加載所有位置數據的json進行解析,並使其准備好進入Game狀態,從而可以使用其中的定位/紋理值。 這樣您就不會有任何潛在的示波器問題(您可能最終會在這里遇到)。

我在另一個論壇上找到了解決此問題的方法,因為我遇到了同樣的問題。 http://www.html5gamedevs.com/topic/1936-import-files-and-data-json-parser/

基本上,我必須使用Phaser的game.load.text('assetList','path/to/file.json')方法,然后使用JSON.parse(game.cache.getText('assetList')); 所有人都開始工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM