Reputation: 33
I have an array of images and I want to use splice to retrieve an image from it. But when I store the data and use it as the image argument in the image() function, I get an error:
image()
was expecting `p5.Image|p5.Element__ for parameter #0 (zero-based index), received array instead
let arr;
let retrieve;
function preload() {
water = loadImage('Images/water.png');
ore = loadImage('Images/ore.png');
}
function setup() {
createCanvas(1800, 1500);
arr = [water, ore];
retrieve = arr.splice(0, 1);
}
function draw() {
background(0);
image(retrieve, 100, 100);
}
So I'm kind of confused because it seems to me that the variable arr
should hold an image --- even if this image was part of an array before.
If I make an array of numbers or text, everything seems to work just fine.
Any thoughts / suggestions?
Upvotes: 1
Views: 317
Reputation: 194
Read the docs of Array.prototype.splice()
The function returns an array (even if no elements are removed):
Return value
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
In your case to access the removed element itself you could use
retrieve = arr.splice(0, 1)[0];
Upvotes: 1