How to use arrays in phaser 3

I have an array with different audios

var SongURL = [
'assets/audio/menu-music.mp3',
'assets/audio/level-1.mp3',
'assets/audio/level-2.mp3',
'assets/audio/level-3.mp3',
'assets/audio/level-4.mp3',
'assets/audio/level-5.mp3',
'assets/audio/level-6.mp3',
'assets/audio/level-7.mp3',
'assets/audio/level-8.mp3',
'assets/audio/level-9.mp3',
'assets/audio/level-10.mp3',
'assets/audio/level-11.mp3',
'assets/audio/level-12.mp3'

this.load.audio('Music', SongURL);
]

I want when one of the boxes is clicked to change the songURL variable

r1.on('pointerdown', () =>   SongURL[1]);
r2.on('pointerdown', () =>   SongURL[2]);
r3.on('pointerdown', () =>   SongURL[3]);
r4.on('pointerdown', () =>   SongURL[4]);
r5.on('pointerdown', () =>   SongURL[5]);
r6.on('pointerdown', () =>   SongURL[6]);
r7.on('pointerdown', () =>   SongURL[7]);
r8.on('pointerdown', () =>   SongURL[8]);
r9.on('pointerdown', () =>   SongURL[9]);
r10.on('pointerdown', () =>   SongURL[10]);
r11.on('pointerdown', () =>   SongURL[11]);
r12.on('pointerdown', () =>   SongURL[12]);

Your first code block is wrong (if it’s no copy/paste error).

Try instead:

var SongURL = [
'assets/audio/menu-music.mp3',
'assets/audio/level-1.mp3',
'assets/audio/level-2.mp3',
'assets/audio/level-3.mp3',
'assets/audio/level-4.mp3',
'assets/audio/level-5.mp3',
'assets/audio/level-6.mp3',
'assets/audio/level-7.mp3',
'assets/audio/level-8.mp3',
'assets/audio/level-9.mp3',
'assets/audio/level-10.mp3',
'assets/audio/level-11.mp3',
'assets/audio/level-12.mp3'
]

this.load.audio('Music', SongURL);
1 Like