0

I have a list of pictures that I would like to use as a background in my game. So it is like that

var images = [image1.jpg, image2.jpag, image3.jpg]

var randomImagesIndex = getRandom(0, images.length)

box.style.backgroundImage = `url('images[randomImagesIndex]')

The result in the Console is like that:

bastards[0]:1          GET file:///C:/Users/asouk/Documents/Projects/Trizub/images[0] net::ERR_FILE_NOT_FOUND

If I try to write directly lije that

box.style.backgroundImage = `url('image1.jpg')

it works! So the images are there. But there brouser does not open it, it does not change images[0] for example to image1.jpg. If I write something like this

box.style.backgroundImage = `url('images[0]')

I think that my mistake is very simple but I am new to JS and I can not fix it 2 days already.

Could anybody help me please?

1 Answers1

1

try:

var images = ['image1.jpg', 'image2.jpg', 'image3.jpg']

var randomImagesIndex = getRandom(0, images.length)
var image = images[randomImagesIndex]

box.style.backgroundImage = `url('${image}')`
André Alçada Padez
  • 10,987
  • 24
  • 67
  • 120