简体   繁体   English

使用街机物理学不在Phaser工作的斜坡

[英]Slopes not working in Phaser using arcade physics

I'm currently having this issue when I am trying to get a sprite to go up a slope within phaser using the phaser-arcade-slopes.min.js plugin. 我正在尝试使用phaser-arcade-slopes.min.js插件让sprite在phaser内的斜坡上运行时遇到此问题。 I am using a .csv tilemap also with a tile size of 32x32. 我正在使用.csv tilemap,其平铺大小为32x32。 I'm unsure if my names are just incorrect or I am using the wrong type of file for the tilemap itself. 我不确定我的名字是不正确还是我正在使用错误类型的文件来处理tilemap本身。 I have been getting errors such as - Tilemap.createLayer: Invalid layer ID given: null & Cannot read property 'resizeWorld' of undefined(…). 我一直在收到错误--Tilemap.createLayer:给出的无效图层ID:null&无法读取undefined(...)的属性'resizeWorld'。 Any help would be much appreciated. 任何帮助将非常感激。 "snow_tiles_32.png" is the name of the tileset I created and I'm using "tiles.csv" the tilemap created inside Tiled. “snow_tiles_32.png”是我创建的tileset的名称,我正在使用“tiles.csv”在Tiled中创建的tilemap。

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

function preload() {
this.game.load.tilemap('tilemap', 'assets/tilemaps/csv/tiles.csv', null, Phaser.Tilemap.CSV);
this.game.load.spritesheet('tiles', 'assets/tilemaps/tiles/snow_tiles_32.png', 32,32);
this.game.load.spritesheet('player', 'assets/penguin.png', 32,48);
}

var player;
var cursors;

function create() {
this.game.physics.startSystem(Phaser.Physics.ARCADE);
this.game.plugins.add(Phaser.Plugin.ArcadeSlopes);
cursors = game.input.keyboard.createCursorKeys();

this.map = this.game.add.tilemap('tilemap');
this.map.addTilesetImage('snow_tiles_32', 'tiles');

this.game.stage.backgroundColor = '#80e3ff';

this.ground = this.map.createLayer('collision');
this.ground.resizeWorld();

this.game.slopes.convertTilemapLayer(this.ground,{
    2:  'FULL',
    3:  'HALF_BOTTOM_LEFT',
    4:  'HALF_BOTTOM_RIGHT',
    6:  'HALF_TOP_LEFT',
    5:  'HALF_TOP_RIGHT',
    15: 'QUARTER_BOTTOM_LEFT_LOW',
    16: 'QUARTER_BOTTOM_RIGHT_LOW',
    17: 'QUARTER_TOP_RIGHT_LOW',
    18: 'QUARTER_TOP_LEFT_LOW',
    19: 'QUARTER_BOTTOM_LEFT_HIGH',
    20: 'QUARTER_BOTTOM_RIGHT_HIGH',
    21: 'QUARTER_TOP_RIGHT_HIGH',
    22: 'QUARTER_TOP_LEFT_HIGH',
    23: 'QUARTER_LEFT_BOTTOM_HIGH',
    24: 'QUARTER_RIGHT_BOTTOM_HIGH',
    25: 'QUARTER_RIGHT_TOP_LOW',
    26: 'QUARTER_LEFT_TOP_LOW',
    27: 'QUARTER_LEFT_BOTTOM_LOW',
    28: 'QUARTER_RIGHT_BOTTOM_LOW',
    29: 'QUARTER_RIGHT_TOP_HIGH',
    30: 'QUARTER_LEFT_TOP_HIGH',
    31: 'HALF_BOTTOM',
    32: 'HALF_RIGHT',
    33: 'HALF_TOP',
    34: 'HALF_LEFT'
});

this.map.setCollisionBetween(2,34, true, 'collision');

//player
this.player = this.game.add.sprite(100,50,'player');
this.game.physics.arcade.enable(player);

this.player.body.bounce.y = 0.2;
this.player.body.gravity.y = 2000;
this.player.body.collideWorldBounds = true;

this.player.animations.add('left', [0,1,2,3], 10, true);
this.player.animations.add('right', [5,6,7,8], 10, true);
this.game.slopes.enable(this.player);
this.game.camera.follow(this.player);

}

function update() {

this.game.physics.arcade.collide(this.player, this.ground);

this.player.body.velocity.x = 0;

if(cursors.left.isDown){
    this.player.body.velocity.x = -150;
    this.player.animations.play('left');
}
else if (cursors.right.isDown){
    this.player.body.velocity.x = 150;
    this.player.animations.play('right');
}
else{
    this.player.animations.stop();
    this.player.frame = 4;
}

if(cursors.up.isDown && player.body.onFloor()){
    this.player.body.velocity.y = -350;
}

} }

LayerID you provide as parameter for createLayer() must match map layer defined in map file, see documentation . 您作为createLayer()参数提供的LayerID必须与map文件中定义的地图图层匹配,请参阅文档 So my best guess would be to open map file in tiled once again and check what layer names you have. 因此,我最好的猜测是再次打开平铺的地图文件并检查您拥有的图层名称。

Additionally there's example how to use .csv files (here)[ http://phaser.io/examples/v2/tilemaps/csv-map] . 此外,还有一些示例如何使用.csv文件(此处)[ http://phaser.io/examples/v2/tilemaps/csv-map]

Hope that'll help somehow. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM