Phaser Tile回调P2碰撞

时间:2018-05-16 19:23:59

标签: javascript phaser-framework

我正在使用P2物理在Phaser中创建一个游戏。当玩家与瓷砖地图上的特定类型的瓷砖碰撞时,ID 405和403,我想要调用一个函数。播放器和磁贴之间的冲突正常工作,但磁贴回调函数不正常。

以下是我的代码的相关部分。

let myplayer;
class mainScene extends Phaser.State {

    constructor () {
        super({ key: 'mainScene' });
    }

    preload() {
        this.load.tilemap('map', 'client/maps/gameMap.csv');
        this.load.image('tileset', 'client/maps/tileMap.png');
    }

    create() {
        this.physics.startSystem(Phaser.Physics.P2JS);
        this.physics.p2.setImpactEvents(true);

        map = this.add.tilemap('map', 32, 32);
        map.addTilesetImage('tileset');

        layer = map.createLayer(0);
        layer.resizeWorld();

        map.setCollisionByIndex(10);
        map.setCollisionByIndex(405);
        map.setCollisionByIndex(403);

        map.setTileIndexCallback(405, this.hitHealth, this, 0);
        map.setTileIndexCallback(403, this.hitAmmo, this, 0);

        this.physics.p2.convertTilemap(map, layer);

        this.Wkey = this.input.keyboard.addKey(Phaser.Keyboard.W);
        this.Akey = this.input.keyboard.addKey(Phaser.Keyboard.A);
        this.Skey = this.input.keyboard.addKey(Phaser.Keyboard.S);
        this.Dkey = this.input.keyboard.addKey(Phaser.Keyboard.D);

        myplayer = new Phaser.Graphics(game);
        myplayer.beginFill(colour);
        myplayer.drawCircle(0, 0, 20);
        myplayer.endFill();

        game.physics.p2.enable(myplayer);
        myplayer.body.setCircle(10);
        myplayer.body.collideWorldBounds = true;
        game.world.add(myplayer);
    }

    update() {
            if (this.Wkey.isDown) {
                myplayer.body.moveUp(myplayer.speed);
            }
            if (this.Akey.isDown) {
                myplayer.body.moveLeft(myplayer.speed);
            }
            if (this.Skey.isDown) {
                myplayer.body.moveDown(myplayer.speed);
            }
            if (this.Dkey.isDown) {
                myplayer.body.moveRight(myplayer.speed);
            }
        }
    }

    hitHealth() {
        console.log("health hit");
    }

    hitAmmo() {
        console.log("ammo hit");
    }
}

var gameConfig = {
    type: Phaser.AUTO,
    width: 600,
    height: 600,
    parent: 'gameDiv',
    backgroundColor: '#f3cca3',
};

let game = new Phaser.Game(gameConfig);
game.state.add("main", mainScene);
game.state.start("main");

我做错了什么,我将如何解决它。

提前致谢。

0 个答案:

没有答案