2016-03-29 16 views
0

簡単にするために、私はゲームを持っています。私は車に壁を打つようにしています。私は検索を試みましたが、以下に示すようにいくつか試してみましたが、なぜ私はそれを動作させることができないのか分かりません。ここでなぜ私の衝突検出がフェイザーで機能していませんか?

は私のcreate関数のコードです:

create: function() { 
     game.physics.startSystem(Phaser.Physics.ARCADE); 

     this.carSprite = game.add.sprite(game.world.centerX, game.world.centerY, 'car'); 
     game.physics.arcade.enable(this.carSprite); 
    this.timer = game.time.events.loop(300, 
     this.addRoad, this); 
    } 

はその後、私のupdate機能に:私は

addOneRoadSection: function (x, y) { 
this.scoreCalculator += 1; 
     if (this.scoreCalculator == 10) { 
      this.scoreCalculator = 0; 

      this.wallSprite = game.add.sprite(x + 100, y, 'wall'); 
      game.physics.arcade.enable(this.wallSprite); 
      this.wallAnimation = this.wallSprite .animations.add('wallAnimation'); 
      this.wallSprite.animations.play('wallAnimation', 30, true); 
      this.wallSprite.body.velocity.y = 100; 
      this.wallSprite.checkWorldBounds = true; 
      this.wallSprite.outOfBoundsKill = true; 
     } 
} 

update: function() { 
     game.physics.arcade.overlap(this.carSprite, this.wallSprite, this.scoring, null, this); 
} 

私はこのようなwallSpriteを作成していますaddOneRoadSectionを次のように呼んでください:

addRoad: function() { 
     this.addOneRoadSection(this.xValue, 0); 
} 

addRoadthis.timerを使用してcreateに呼び出されています。

これは、scoreCalculatorが10のときに壁を追加することを要約しています。それはうまく動作し、壁はアニメーションがうまくいきますが、衝突の検出はまったく機能しません。

ifのコード内のコードをcreate関数に移動しようとしましたが、衝突検出でうまく動作します(ただし、他のものが壊れてしまい、そこに保持できません)。私は間違って何をしていますか?私はthis.wallSpriteを毎秒約1回呼び出すので、それは新しいスプライトによって書かれているので、this.wallSpriteとして追加されてしまった疑いがありますが、それ以外はどうすればいいのでしょうか?

答えて

0

私はこれを理解しました。起こっていたことは、新しいスプライトを作成する私の私の私の最初の傾きと、他のスプライトを書くことが正しいことでした。私は、このような各スプライトに物理学を追加することができ、新たなwallSpriteaddOneRoadSectionに追加するときに、私は、配列を持っていることを今

create: function(){ 
//Create a wallSprite array 
var wallSprite = []; 
} 

createで:ここで

は、私は問題を解決する方法であります:

addOneRoadSection: function (x, y) { 

      this.wallSprite.push(game.add.sprite(x + 100, y, 'wall')); 
      game.physics.arcade.enable(this.wallSprite[this.wallSprite.length -1]); 
      this.wallAnimation = this.wallSprite[this.wallSprite.length - 1].animations.add('wallAnimation'); 
      this.wallSprite[this.wallSprite.length - 1].animations.play('wallAnimation', 30, true); 
      this.wallSprite[this.wallSprite.length -1].body.velocity.y = 100; 
      this.wallSprite[this.wallSprite.length - 1].checkWorldBounds = true; 
      this.wallSprite[this.wallSprite.length -1].outOfBoundsKill = true; 
} 

最後にupdateで私はちょうどこれを実行する必要があります。

for (var i = 0; i < this.wallSprite.length; i++) { 
     game.physics.arcade.overlap(this.car, this.wallSprite, this.scoring, null, this); 
    } 

ここで、すべてのスプライトに対して衝突検出機能が動作します。

関連する問題