Question

I'm trying to get a sound file to play continuously as my player character moves about (controlled by the WASD Keys), however currently it only plays when I change direction, is there any way I can get it to loop until the button is released.

Here is what i'm currently doing:

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;
}
}
});
Was it helpful?

Solution

I found one way to do it, inside the audio.play after the sound file name, you can set how many times you want it replay, and setting it to -1 makes it play forever, i then set it up that if the play is not moving the soundfile will be stopped, theres probably a better way to do this but this its the way im using. Heres the code:

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')
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top