2017-02-23 60 views
0

所以我只是试图在掉落的物体和玩家精灵之间进行简单的碰撞。它没什么复杂的。Typescript - 移相器碰撞重叠功能不起作用

我正在尝试使用game.physics.arcade.overlap()函数。

这里是我的代码:

Player类 - >

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; 
      } 
     } 
    } 
} 

掉落物品类 - >

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; 
     } 
    } 
} 

游戏主文件,其中我使用的重叠功能 - >

create(){ 

     this.difficulties = []; 
     this.difficulties.push(new Difficulty(0, 5)); 
     this.difficulties.push(new Difficulty(1, 7)); 
     this.difficulties.push(new Difficulty(2, 9)); 
     this.currentDifficulty = this.difficulties[0]; 
     this.shouldChangeDifficulty = true; 

     this.levelOne = new LevelOne(this.game); 
     this.levelOne.create(this.currentDifficulty); 
     this.currentLevel = this.levelOne; 

     this.player = new Player(this.game); 
     this.player.create(); 

     this.rocks = new Rock(this.game); 
     this.rocks.create(); 
    } 

    update(){ 
     this.player.update(); 
     this.rocks.update(this.player); 

     this.game.physics.arcade.overlap(this.player, this.rocks, this.collisionHandler, null, this); 
    } 

    collisionHandler (player, rock) { 
     console.log("Does it work ?!"); 
    } 
+1

我不知道使用TypeScript进行Phaser的开发,但我想问你一些问题:控制台中是否有错误?您是否正确激活了物理?:'this.game.physics.startSystem(Phaser.Physics.ARCADE);' –

回答

0

我在打字稿中遇到了几个相位器功能的问题。我能够使用内联lambda函数来处理事情。 (this.player,this.rocks,(p,r)=> {console.log(“Does it work ?!”);},null,this);