createFromObjects()无法在移相器中工作?

时间:2017-02-04 11:34:56

标签: javascript phaser-framework

我'我试图在移相器中制作游戏。我创建了一个带有对象层的tilemap。但是,当我'我试图将对象层中的对象添加到组" checkers"重叠不起作用。 这是我的代码:

function preload()
{
    game.load.spritesheet("bird","assets/flappybird.png",92,64,3);
    game.load.tilemap('map', 'assets/map.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles', 'assets/obstacle.png');
    game.load.image('checktiles', 'assets/empty.png');
    game.stage.backgroundColor = '#c9c9c9';

}
function create()
{
     game.physics.startSystem(Phaser.Physics.ARCADE);
     map = game.add.tilemap('map');
     //OBSTACLES
     map.addTilesetImage('obstacle', 'tiles');
     layer = map.createLayer('Tilelaag 1');
     layer.resizeWorld();
     map.setCollisionBetween(1, 12);
     //CHECKERS
     checkers = game.add.group();
     checkers.enableBody=true;
     map.createFromObjects('Object Checkers', 5, 'checktiles', 0, true, false, checkers);
    //PLAYER
    player=game.add.sprite(92,game.world.heigth-300,"bird");
    player.scale.setTo(0.5,0.5);
    game.physics.arcade.enable(player);
    player.body.gravity.y=300;
    player.animations.add("fly",[0,1,2],10,true);
    player.body.collideWorldBounds = true;
    player.body.velocity.x=150;
    //CURSORS
    cursors=game.input.keyboard.createCursorKeys();
    spacebar=game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    //CAMERA
    game.camera.follow(player);
}
function update()
{
    var hit=game.physics.arcade.collide(layer,player);
    game.physics.arcade.overlap(player, checkers,Addpoint,null,this);
    player.animations.play("fly");
    if(spacebar.isDown)
    {
        player.body.velocity.y = -150;
    }
    if(hit)
    {
        game.state.restart();
    }
}
function Addpoint()
{
    console.log("Called");
}

1 个答案:

答案 0 :(得分:1)

我想你的JSON文件中的某个地方就在图层数据之后应该是这样的:

...
{
         "draworder":"topdown",
         "height":0,
         "name":"Object Checkers",
         "objects":[
                {
                 //First object
                 "height":43.5,
                 "id":2,
                 "name":"",
                 "rotation":0,
                 "type":"",
                 "visible":true,
                 "width":43.5,
                 "x":158.25,
                 "y":178,
                 "gid":5
                }, 
                {
                 //Second object
                 "height":43.5,
                 "id":9,
                 "name":"",
                 "rotation":0,
                 "type":"",
                 "visible":true,
                 "width":43.5,
                 "x":117.5,
                 "y":257.75,
                 "gid":5
                }, 
                {
                 //Third object (if it exists), etc...
                 ...

id,旋转,名称,宽度,长度和其他属性当然可以变化,但GID不会。您需要在数组的每个元素上使用GID属性(在本例中为5),您可以手动将其放在每个对象中。

在验证之后,它会做什么:

var map, layer, checkers;

function create()
{
     game.physics.startSystem(Phaser.Physics.ARCADE);
     //Map
     map = game.add.tilemap('map');
     //I understand that here is the name of the tileset as specified in the map data.
     map.addTilesetImage('tiles');
     layer = map.createLayer('Tilelaag 1');
     layer.resizeWorld();
     map.setCollisionBetween(1, 12);
     //Checkers
     checkers = game.add.group();
     checkers.enableBody = true;
     /*
        First parameter: string of map objects layer.
        Second parameter: Gid.
        Third parameter: sprite string to use.
        Fourth parameter: value of the frame to initialize the object.
        Fifth parameter: default status of the sprite.
        Sixth parameter: autocull. The sprites that are autoculled are culled from the camera if out of its range..
        Seventh parameter: the group is specified
     */
     map.createFromObjects('Object Checkers', 5, 'checktiles', 0, true, false, checkers);
     console.log(checkers.length);
    //create player, controls, etc...
    ...
}

可能会影响对象的位置,您可以使用:

map.createFromObjects('Object Checkers', 5, 'checktiles', 0, true, false, checkers, Phaser.Sprite, false);

example