2017-02-04 63 views
0

我正在嘗試用移相器製作遊戲。我用一個objectlayer創建了一個tilemap。然而,當我試圖將對象從對象層添加到組「檢查器」時,重疊不起作用。 這裏是我的代碼:createFromObjects()在phaser中不工作?

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

我想在某處你的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