好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

phaser3-瓦面地图

var config = {
    type: Phaser.WEBGL,
    width: 800,
    height: 600,
    backgroundColor: '#2d2d2d',
    parent: 'phaser-example',
    pixelArt: true,
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);
var controls;
var map;

function preload ()
{
    this.load.image('tiles', 'assets/tilemaps/tiles/tmw_desert_spacing.png');
    this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/desert.json');
}

function create ()
{
    map = this.make.tilemap({ key: 'map' });
    var tiles = map.addTilesetImage('Desert', 'tiles');
    var layer = map.createLayer('Ground', tiles, 0, 0);
    // 设置相机边界和地图边界保持一致
    this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
    // 创建并返回一个包含上、下、左、右4个热键的对象,还有空格键和shift键。
    var cursors = this.input.keyboard.createCursorKeys();
    var controlConfig = {
        camera: this.cameras.main,
        left: cursors.left,
        right: cursors.right,
        up: cursors.up,
        down: cursors.down,
        speed: 0.5
    };
    // 创建操作相机配置
    controls = new Phaser.Cameras.Controls.FixedKeyControl(controlConfig);

    var help = this.add.text(16, 16, 'Click a tile to replace all instances with a plant.', {
        fontSize: '18px',
        padding: { x: 10, y: 5 },
        backgroundColor: '#000000',
        fill: '#ffffff'
    });
    help.setScrollFactor(0);
}

function update (time, delta)
{
    // 需要实时更新
    controls.update(delta);
    // 获得当前活动输入实列
    if (this.input.manager.activePointer.isDown)
    {
        // 获取一个Camera并返回一个Vector2,其中包含这个Pointer的转换位置 在这个相机。这可以用来转换这个指针的位置到相机空间。
        var worldPoint = this.input.activePointer.positionToCamera(this.cameras.main);
        // 从给定层获取给定世界坐标处的贴图。 如果没有指定图层,则使用当前图层映射。
        var tile = map.getTileAtWorldXY(worldPoint.x, worldPoint.y);

        // 这将用植物(瓷砖id=38)替换选定瓷砖的所有实例。
        map.replaceByIndex(tile.index, 38);

        //也可以在特定区域内替换(tileX、tileY、width、height):
        //map.replaceByIndex(tile.index,38,5,5,15,15);
        //也可以在特定区域内替换(tileX、tileY、width、height):
        //map.replaceByIndex(tile.index,38,5,5,15,15);
    }

}

查看更多关于phaser3-瓦面地图的详细内容...

  阅读:50次

上一篇: phaser3-瓦片地图2

下一篇:phaser3-组概念