两个精灵之间的碰撞令人担忧

时间:2015-01-21 18:20:28

标签: javascript phaser-framework

我为破碎的英语道歉,希望你能理解我

您好,我需要您的帮助,因为我现在已经停留了好几个小时,而且它开始让我发疯。我需要错过一些东西,但不知道是什么。

我目前正在开发一款使用Phaser 2.2的游戏,我尝试制作碰撞系统。

原则是简单的绿色区域不应该是可访问的和墙壁

The exemple

这是完整的代码

var game = new Phaser.Game (512, 512, Phaser.CANVAS 'phaser-example', {preload: preload, create: create, update: update render: render});

preload function () {

     game.load.tilemap ('bar', 'assets / maps / bar.json', null, Phaser.Tilemap.TILED_JSON)
     game.load.image ('cafe_tiles', 'assets / tiles / cafe.png');
     game.load.image ('wall_tiles', 'assets / tiles / cafewall.png');
     game.load.atlas ('player', 'assets / sprites / tremel.png', 'assets / sprites / tremel.json');

}

var map;
var layer_wall;
var layer_sol;
var layer_couche_1;
var layer_couche_2;
var layer_couche_3;

var keys;
var sprite;
var marker;
var player;

function create () {

     game.physics.startSystem (Phaser.Physics.ARCADE);
 
     map = game.add.tilemap ('bar');
    
     map.addTilesetImage ('bar', 'cafe_tiles');
     map.addTilesetImage ('cafewall', 'wall_tiles');
    
     layer_sol map.createLayer = ('ground');
     layer_wall map.createLayer = ('wall');
     layer_couche_1 map.createLayer = ('couche_1');
     layer_couche_2 map.createLayer = ('couche_2');
     layer_couche_3 map.createLayer = ('couche_3');
    
     map.setCollisionBetween (1, 1000, true, layer_wall)
     map.setCollisionBetween (1, 1000, true, layer_couche_1)
     map.setCollisionBetween (1, 1000, true, layer_couche_2)
     map.setCollisionBetween (1, 1000, true, layer_couche_3)
     layer_couche_1.debug = true;
     layer_couche_2.debug = true;
     layer_couche_3.debug = true;
     layer_wall.debug = true;
     layer_sol.resizeWorld ();
    
     player = game.add.sprite (200, 200, 'player', 'tremel1');
     player.animations.add ('walk_down' Phaser.Animation.generateFrameNames ('tremel', 1, 4, '', 1), 30, true);
     player.animations.add ('walk_up' Phaser.Animation.generateFrameNames ('tremel', 13, 16, '', 1), 30, true);
     player.animations.add ('walk_left' Phaser.Animation.generateFrameNames ('tremel', 5, 8, '', 1), 30, true);
     player.animations.add ('walk_right' Phaser.Animation.generateFrameNames ('tremel', 9, 12, '', 1), 30, true);
       
     game.physics.arcade.enable (player);
     player.body.collideWorldBounds = true;

     game.input.keyboard.createCursorKeys keys = ();
    
}

function update ()
{
  game.physics.arcade.collide (player, layer_couche_1)

  if (keys.left.isDown)
  {
    player.x - = 3;
    player.animations.play ('walk_left', 8, true);
  }
  else if (keys.right.isDown) {
    player.x + = 3;
    player.animations.play ('walk_right', 8, true);
  }
  else if (keys.up.isDown)
  {
    player.y - = 3;
    player.animations.play ('walk_up', 8, true);
  }
  else if (keys.down.isDown)
  {
    player.y + = 3;
    player.animations.play ('walk_down', 8, true);
  }
  else
  {
    player.animations.stop ()
  }
}

正如我在网上多次看到的那样,在Phaser doc上管理碰撞必须报告碰撞

 map.setCollisionBetween (1, 1000, true, layer_wall)
 map.setCollisionBetween (1, 1000, true, layer_couche_1)
 map.setCollisionBetween (1, 1000, true, layer_couche_2)
 map.setCollisionBetween (1, 1000, true, layer_couche_3)

然后添加物理播放器

 game.physics.arcade.enable (player);

最后通常只需在update()方法中添加它

 game.physics.arcade.collide (player, layer_couche_1)

除了game.physics.arcade.collide(播放器,layer_couche_1)我总是错误引用而且我不知道pk?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

player.x是精灵位置,player.body.x是街机物理位置,所以在你的更新方法中你需要更新player.body.x而不是player.x