[Phaser] 2. Starter

개요

Blank State

var StateMain={      
    preload:function()
    {      
    },  
    create:function()
    {      
    },  
    update:function()
    {              
    }      
}

Switch states

Start for Desktop

var game;
window.onload = function()
{
    game=new Phaser.Game(480,640,Phaser.AUTO,"ph_game");
    game.state.add("StateMain",StateMain);
    game.state.start("StateMain");
}

Start for Desktop/Mobile

var game;
var useLandscape=false;
window.onload = function () {
    var isMobile=navigator.userAgent.indexOf("Mobile");
    if (isMobile>-1)
    {
        isMobile=true;
    }
    else
    {
        isMobile=false;
    }
    if (isMobile==false) {
        //desktop laptop
        if (useLandscape == true) {
            game = new Phaser.Game(640, 480, Phaser.AUTO, "ph_game");
        } else {
            game = new Phaser.Game(480, 640, Phaser.AUTO, "ph_game");
        }
    } else {
        //mobile device
        game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, "ph_game");
    }
    game.state.add("StateMain",StateMain);
    game.state.start("StateMain");
}

Basic HTML

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="UTF-8">
        <title>Game Title Here</title>
        <script src="js/phaser.min.js"></script>
        <script src="js/main.js"></script>  
    </head>
    <body>
    </body>
</html>

Full Screen Meta Tag

<meta name="viewport" content="user-scalable=0, initial-scale=1,minimum-scale=1, maximum-scale=1, width=device-width, minimal-ui=1">

Sample Init State

var StateInit = {


    preload: function () {
        game.load.image("loadingEmpty", "images/loading/progress_none.png");
        game.load.image("loadingFull", "images/loading/progress_all.png");
        game.scale.forceOrientation(true, false);
        game.scale.enterIncorrectOrientation.add(this.wrongWay, this);
        game.scale.leaveIncorrectOrientation.add(this.rightWay, this);
    }
    , create: function () {
        game.state.start("StateLoad");
    }
    , update: function () {
    }
    , rightWay: function () {
        document.getElementById("wrong").style.display = "none";
    }
    , wrongWay: function () {
        document.getElementById("wrong").style.display = "block";
    }
}

Sample Load State

var StateLoad = {
    preload: function () {
        var empty = game.add.image(0, 0, "loadingEmpty");
        var full = game.add.image(0, 0, "loadingFull");
        empty.x = game.world.centerX;
        empty.y = game.world.centerY;
        empty.anchor.set(0.5, 0.5);
        full.anchor.set(0, 0.5);
        full.x = game.world.centerX - empty.width / 2;
        full.y = empty.y;
        game.load.setPreloadSprite(full);
        //PRELOAD EVERYTHING HERE
    },
    create: function () {
        game.state.start("StateTitle");
    },
    update: function () {
    }
}