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