打字稿 - 移相器碰撞重叠功能不起作用

时间:2017-02-23 13:17:32

标签: javascript typescript collision phaser-framework

所以我只想在坠落的物体和玩家精灵之间进行简单的碰撞。它没什么复杂的。

我试图使用game.physics.arcade.overlap()函数。

这是我的代码:

玩家类 - >

<?php 
if (!empty($_POST["mstid "])) {
echo $_POST['mstid ']; 
}
?>

坠落物体类 - &gt;

export class Player{
    game: Phaser.Game;
    player: Phaser.Sprite;

    constructor(game:Phaser.Game){
        this.game = game;
        this.player = this.game.add.sprite(400, 520, "Player");
        this.game.physics.arcade.enable(this.player);
    }

    create(){
    }

    update(){

        if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
        {
            if(this.player.x >= -10){
                this.player.x -= 7;
            }
        }
        else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
        {
            if(this.player.x <= 830){
                this.player.x += 7;
            }
        }
    }
}

我使用重叠功能的主游戏文件 - &gt;

    export class Rock {

    game:Phaser.Game;
    rocks: Phaser.Group;

    constructor(game:Phaser.Game){
        this.game = game;
    }

    create(){

        this.rocks = this.game.add.physicsGroup(Phaser.Physics.ARCADE);
        this.game.physics.arcade.enable(this.rocks);

        var x = 10;

        for(var i = 0; i < 9; i++){
            var rock = this.rocks.create(x, this.game.rnd.between(-30,-5), "Rock");
            rock.body.velocity.y = this.game.rnd.between(240,300);
            x += 105;
        }
    }

    update(player){
        this.rocks.forEach(this.checkPos, this);
    }

    checkPos(rock) {
        if (rock.y > 800)
        {
            rock.y = -100;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我在打字稿中遇到了几个相位函数的问题。我能够使用内联lambda函数来处理这些事情。

this.game.physics.arcade.overlap(this.player,this.rocks,(p,r)=&gt; {console.log(&#34;它有效吗?!&#34;);} ,null,this);