【问题标题】:follow camera in phaser.js在 phaser.js 中跟随相机
【发布时间】:2017-11-22 15:30:39
【问题描述】:

所以我刚刚弄清楚如何加载我在程序 tiled 中创建的 tilemap,它按预期工作,所以我尝试使用示例中的一些代码添加一个带有基本跟随相机的播放器,但我有除了正在查看 tilemap 的中间之外什么都没有发生,这是我尝试过的代码:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

function preload() {

    //  Tilemaps are split into two parts: The actual map data (usually stored in a CSV or JSON file) 
    //  and the tileset/s used to render the map.

    //  Here we'll load the tilemap data. The first parameter is a unique key for the map data.

    //  The second is a URL to the JSON file the map data is stored in. This is actually optional, you can pass the JSON object as the 3rd
    //  parameter if you already have it loaded (maybe via a 3rd party source or pre-generated). In which case pass 'null' as the URL and
    //  the JSON object as the 3rd parameter.

    //  The final one tells Phaser the foramt of the map data, in this case it's a JSON file exported from the Tiled map editor.
    //  This could be Phaser.Tilemap.CSV too.

    game.load.tilemap('gameMap', 'assets/tilemaps/gameMap2.json', null, Phaser.Tilemap.TILED_JSON);

    //  Next we load the tileset. This is just an image, loaded in via the normal way we load images:

    game.load.image('tiles', 'assets/tilemaps/gameMap.png');
    game.load.image('player','assets/img/player.gif');
}

var map;
var layer;
var player;
var cursors;

function create() {

    game.stage.backgroundColor = '#787878';
    game.world.setBounds(0, 0, 3200, 3200);
    game.physics.startSystem(Phaser.Physics.P2JS);
    player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');


        game.physics.p2.enable(player);

        cursors = game.input.keyboard.createCursorKeys();

        game.camera.follow(player);

    //  The 'mario' key here is the Loader key given in game.load.tilemap
    map = game.add.tilemap('gameMap');

    //  The first parameter is the tileset name, as specified in the Tiled map editor (and in the tilemap json file)
    //  The second parameter maps this name to the Phaser.Cache key 'tiles'
    map.addTilesetImage('spritesheet', 'tiles');

    //  Creates a layer from the World1 layer in the map data.
    //  A Layer is effectively like a Phaser.Sprite, so is added to the display list.
    layer = map.createLayer('Tile Layer 1');

    //  This resizes the game world to match the layer dimensions
    layer.resizeWorld();

}
function update() {

        player.body.setZeroVelocity();

        if (cursors.up.isDown)
        {
            player.body.moveUp(300)
        }
        else if (cursors.down.isDown)
        {
            player.body.moveDown(300);
        }

        if (cursors.left.isDown)
        {
            player.body.velocity.x = -300;
        }
        else if (cursors.right.isDown)
        {
            player.body.moveRight(300);
        }

    }
    function render() {

            game.debug.cameraInfo(game.camera, 32, 32);
            game.debug.spriteCoords(player, 32, 500);

        }

我是使用移相器的新手,我不知道为什么这对任何指针都不起作用?

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    在以这种方式实现了地图的创建之后,尝试创建角色的功能和相机的跟随:

    function create() {
    
       //Configure physical engine
       game.physics.startSystem(Phaser.Physics.P2JS);
    
       //Creating the tilemap
       game.stage.backgroundColor = '#787878';
    
       map = game.add.tilemap('mario');
    
       map.addTilesetImage('SuperMarioBros-World1-1', 'tiles');
    
       layer = map.createLayer('World1');
    
       layer.resizeWorld();
    
       //Configure player controls
       cursors = game.input.keyboard.createCursorKeys();
    
       //Player creation
       player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');
    
       game.physics.p2.enable(player);
    
       game.camera.follow(player);
    
    }
    

    我以example 为基础:

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 2016-02-24
      • 1970-01-01
      • 2022-10-04
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多