2017-02-23 13 views
0

私は落下するオブジェクトとプレイヤーのスプライトの単純な衝突を試みています。その何も複雑ではない。Typescript - フェイザーの衝突オーバーラップ関数doesnt work

私はgame.physics.arcade.overlap()関数を使用しようとしています。ここで

は私のコードです: - >

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

私はタイスクリプトのいくつかの位相器機能で同じ問題を抱えていました。インラインラムダ関数を使って作業することができました。

this.game.physics.arcade.overlap(this.player、this.rocks、(p、r)=> {console.log( "これは機能しますか?");}、null、this);