2016-09-08 5 views
0

Phaser 사운드 문제를 도와 줄 사람이 있습니까?
TypeScript에서 사운드를 재생하는 버튼이 코딩되어 있지만 Chrome에서 버튼을 누르면 Phaser가 Chrome 콘솔에서 다음 경고를 표시합니다. Phaser.Cache.getSound: Key "sample" not found in Cache.
처음에는 mp3 파일에 ... 나는 버튼을 누르면하지가 완전히로드 된,하지만 난 시간의 충분한 양을 기다릴 경우에도 상황은 여전히 ​​동일Phaser로 오디오 재생

export class PlayState extends Phaser.State { 

    private sampleSound: Phaser.Sound; 
    private testButton: Phaser.Sprite; 

    preload() { 
     this.load.image("test", "assets/image/test.png") 
     this.load.audio("sample", "assets/audio/sample.mp3"); 
    } 

    create() { 
     // Add test button 
     this.testButton = this.add.sprite(50, 50, "test"); 
     this.testButton.anchor.setTo(0.5); 
     this.testButton.inputEnabled = true; 
     this.testButton.events.onInputDown.add(this.test); 

     // Add audio 
     this.sampleSound = this.add.audio("sample"); 
    } 

    private test =() => { 
     this.sampleSound.play(); 
    } 
} 
내가 확실히 자산/오디오 디렉토리 아래 sample.mp3 파일이

, 그래서 Phaser는 그것을 찾을 수 있어야합니다.

답변

1

MP3 파일 을 transpabilized javascript 파일의 폴더 아래에 놓습니다. 필자의 경우 TypeScript는 source/assets/audio/sample.mp3을 찾고 있었지만 transprile 된 javascript는 dist/assets/audio/sample.mp3을 찾고있었습니다.
(페이저 그것을 발견 할 수 있었다, 그래서 test.png은 이미 거기에 있었다. 그러나 나는 완전히 ... MP3 파일을 배치하는 것을 잊었다) 당신이 스크립트들은 후 실행된다는 점을 항상 인식, 타이프 라이터를 사용하는 경우

을 증발합니다.

1

사운드 파일을로드하는 것이 한 가지이며, 디코딩하는 것이 다른 것입니다. 캐시 오류가 발생하는 이유를 모르지만 대부분의 경우 파일을로드했지만 아직 디코딩하지 않은 것입니다. 이 같은

시도 뭔가 :

export class PlayState extends Phaser.State { 

    private sampleSound: Phaser.Sound; 
    private testButton: Phaser.Sprite; 

    preload() { 
     this.load.image("test", "assets/image/test.png"); 
     this.load.audio("sample", "assets/audio/sample.mp3"); 
    } 

    create() { 
     // Add audio 
     this.sampleSound = this.add.audio("sample"); 

     this.game.sound.setDecodedCallback(["sample"], this.createButton, this); 
    } 

    private createButton(): void { 
     // Add test button 
     this.testButton = this.add.sprite(50, 50, "test"); 
     this.testButton.anchor.setTo(0.5); 
     this.testButton.inputEnabled = true; 
     this.testButton.events.onInputDown.add(this.test); 
    } 

    private test =() => { 
     this.sampleSound.play(); 
    } 
} 

MP3 파일을 디코딩 한 후 당신은 단지 버튼을 만드는이 방법. 더 많은 사운드를로드해야 할 경우를 대비하여 첫 번째 인수에 더 많은 키를 추가하십시오.

소리를 재생하는 람다 함수는 (일관성을 위해서) 정규 인스턴스 메서드 일 수 있습니다.

테스트하지 않았지만이 테스트를 통과해야합니다.

+1

의견을 보내 주셔서 감사합니다. 사운드를 재생할 수없는 이유는 transpiled js 파일이 다른 assets 디렉토리를 찾고 있기 때문입니다. js 디렉토리에 mp3 파일을 배치하면 원래 코드가 작동하기 시작합니다. 이 mp3 파일은 디코딩 된 상태를 확인하지 않고 실행할 수있을만큼 라이트이지만'setDecodedCallback'을 사용하여 오디오 파일을 사용하기 전에 100 % 확실하게 디코딩합니다. 도와 주셔서 정말로 고맙습니다! – sora