0

我试图让一个声音文件在我的玩家角色移动时连续播放(由 WASD 键控制),但是目前它只在我改变方向时播放,有什么方法可以让它循环直到按钮释放。

这是我目前正在做的事情:

Crafty.c('Player', {
init: function(){
this.requires('Actor, 2DMove, Collision, spr_player, SpriteAnimation, Keyboard')
.stopOnSolids()
.onHit('NPC1Event', this.Level2)
.onHit('Enemy', this.attackEnemy)
.onHit('TeleporterHubWorld', this.Level2)
.onHit('TeleporterSpawn', this.Level2)
.onHit('TeleporterYR10TP1', this.Level2)
.onHit('TeleporterYR10TP1_1', this.Level2)
.onHit('TeleporterYR10TP1_1.1', this.Level2)
.onHit('TeleporterYR10TP1_1.2', this.Level2)
.onHit('TeleporterYR10TP1_2', this.Level2)
.onHit('TeleporterYR10TP1_2.1', this.Level2)
.onHit('TeleporterYR10TP1_2.2', this.Level2)
.onHit('TeleporterYR10TP1_3', this.Level2)
.onHit('TeleporterYR10TP2', this.Level2)
.onHit('TeleporterYR10TP3', this.Level2)
.onHit('TeleporterYR11TP1', this.Level2)
.onHit('TeleporterYR11TP2', this.Level2)
.onHit('TeleporterYR11TP3', this.Level2)
.animate('PlayerUp', 0, 0, 3)
.animate('PlayerDown', 0, 1, 3)
.animate('PlayerRight', 0, 2, 3)
.animate('PlayerLeft', 0, 3, 3)
.bind('KeyDown', function () { if (this.isDown('SPACE')) Crafty.scene('World'); })
.bind('KeyDown', function () { if (this.isDown('F')) Crafty.scene('BattleScreen');      });

// this makes the Sprite animate on player input, need to analyse before I complete
var animation_speed = 10;
this.bind('NewDirection', function(data) {
  if (data.x > 0) {
    this.animate('PlayerRight', animation_speed, -1);
    Crafty.audio.play('attack');
  } else if (data.x < 0) {
    this.animate('PlayerLeft', animation_speed, -1);
    Crafty.audio.play('attack');
  } else if (data.y > 0) {
    this.animate('PlayerUp', animation_speed, -1);
    Crafty.audio.play('attack');
  } else if (data.y < 0) {
    this.animate('PlayerDown', animation_speed, -1);
    Crafty.audio.play('attack');
  } else {
    this.stop();
  }
});
},

stopOnSolids: function() {
this.onHit('Solid', this.stopMovement);

return this;
},

attackEnemy: function(data) {
Enemy = data[0].obj;
Enemy.collect();
},

Level2: function(data) {
Teleporter1 = data[0].obj;
Teleporter1.collect();
//Replace with loop at some point
monsterBeat[0] = false;
monsterBeat[1] = false;
},

//Does some fancy maths to continue movement when stoped by an object
//This is copied from another piece of code, so will have to recreate for myself eventually
stopMovement: function() {
if (this._movement) {
this.x -= this._movement.x;
if (this.hit('Solid') != false) {
this.x += this._movement.x;
this.y -= this._movement.y;
if (this.hit('Solid') != false) {
this.x -= this._movement.x;
}
}
} else {
this._speed = 0;
}
}
});
4

1 回答 1

1

我找到了一种方法,在声音文件名之后的 audio.play 中,您可以设置您希望它重播的次数,并将其设置为 -1 使其永远播放,然后我将其设置为如果播放没有移动声音文件将被停止,可能有更好的方法来做到这一点,但这就是我使用的方式。继承人的代码:

var animation_speed = 10;
this.bind('NewDirection', function(data) {
  if (data.x > 0) {
    this.animate('PlayerRight', animation_speed, -1);
        Crafty.audio.play('Footstep', -1);
  } else if (data.x < 0) {
    this.animate('PlayerLeft', animation_speed, -1);
        Crafty.audio.play('Footstep', -1);
  } else if (data.y > 0) {
    this.animate('PlayerUp', animation_speed, -1);
        Crafty.audio.play('Footstep', -1);
  } else if (data.y < 0) {
    this.animate('PlayerDown', animation_speed, -1);
        Crafty.audio.play('Footstep', -1);
  } else {
    this.stop();
    Crafty.audio.stop('Footstep')
  }
于 2014-01-30T09:40:31.500 回答