2017-02-23 1 views
0

Donc j'essaie simplement de faire une simple collision entre la chute d'objets et un sprite de joueur. Ce n'est rien de compliqué. J'essaie d'utiliser la fonction game.physics.arcade.overlap().Typescript - La fonction de chevauchement des collisions Phaser ne fonctionne pas

Voici mon code:

La classe du joueur ->

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

La classe chute d'objets ->

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

Le fichier du jeu principal où je suis en utilisant la fonction de recouvrement ->

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

Je ne connais pas le développement de Phaser avec TypeScript mais je veux vous demander quelque chose: Avez-vous des erreurs de la console? Avez-vous activé la physique correctement ?: 'this.game.physics.startSystem (Phaser.Physics.ARCADE);' –

Répondre

0

J'ai eu le même problème avec plusieurs fonctions phaser en tapuscrit. J'ai été capable de faire fonctionner les choses avec les fonctions lambda en ligne.

this.game.physics.arcade.overlap (this.player, this.rocks, (p, r) => {console.log ("Est-ce que ça marche ?!");}, null, this);