2014-01-29 4 views
0

나는 방향을 바꿀 때만 현재 플레이하고 있지만, 내 플레이어 캐릭터가 움직이면서 (WASD 키로 제어 됨) 지속적으로 사운드 파일을 재생하려고합니다. 방법은 단추를 놓을 때까지 반복 할 수 있습니다. 당신은 당신이 재생을 원하는 얼마나 많은 시간을 설정할 수 있습니다,crafty.js의 사운드 파일 반복하기

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

답변

1

나는 사운드 파일 이름 뒤에 audio.play 내부는 그 방법을 발견 : 여기

내가 현재 뭘하는지입니다 그리고 -1로 설정하면 영원히 재생할 수 있습니다. 그 다음에 플레이가 움직이지 않으면 사운드 파일이 멈추게 될 것입니다. 아마도이 방법을 사용하는 더 좋은 방법 이겠지만,이 방법은 제가 사용하는 방식입니다. Heres 코드 :

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') 
    }