简体   繁体   English

Phaser碰撞:执行一次功能

[英]Phaser collision: execute function one time

I have a little problem in the collisions while using Phaser API . 在使用Phaser API时,我在碰撞中遇到了一些问题。

I have this code: 我有这个代码:

game.physics.arcade.collide(sprite1, sprite2, this.collision, null, this);

   collsion: function(){
      //Play a sound;
   }

The problem here is when the two sprites collide the sound keeps playing. 这里的问题是当两个精灵碰撞时声音继续播放。 It only stops when they are not colliding. 它只在不碰撞时停止。 But I want to play the sound just the one time. 但我想只播放一次声音。

Edited after testing myself. 在测试自己后编辑

Try the overlap : 尝试重叠

create : function() {
...
this.timeDelay = 0;
}

game.physics.arcade.overlap(sprite1, sprite2, this.overlap, null, this);

overlap: function(){
    if (game.time.now > this.timeDelay){
                game.sound.play("sound");
                // wait 1 second
                timeDelay = game.time.now + 1000
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM