0

I want display these images on canvas using SetInterval. so I want store them in an array but it does not work, because the images are not displayed on the canvas, which means the array is empty Is there any body with the idea on how to achieve that?

var imagesArr=[];
var n=0;
game.context.img1 = loader.loadImage("images/systems.png");
game.context.img2 = loader.loadImage("images/CPU.png");
game.context.img3 = loader.loadImage("images/abacus.png");
var image1 = game.context.img1; 
var image2 = game.context.img2;
var image3 = game.context.img3;
imagesArr.push(image1);
imagesArr.push(image2);    
imagesArr.push(image3);

"This is the answer to my question"

imgs=setInterval(function(){
    n=(n+1)%imagesArr.length;
    game.context.drawImage(imagesArr[n], 59, 220, 270, 260);
    if (n>=2)  clearInterval(imgs);
    }, 2000);
2
  • 1
    imagesArr.push.image1? I don't know javascript, but shouldn't that be imagesArr.push(image1)? Commented Dec 2, 2013 at 2:49
  • imagesArr.push(image1,image2,image3); Commented Dec 2, 2013 at 2:50

2 Answers 2

2

Instead of

imagesArr.push.image1;
imagesArr.push.image2;    
imagesArr.push.image3;

Use

imagesArr.push(image1);
imagesArr.push(image2);
imagesArr.push(image3);

Or as a shortcut

imagesArr.push(image1, image2, image3);

To access a specific element in the array

var n = 2;
imagesArr[n]; // returns image3

Read more about Array.prototype.push

Sign up to request clarification or add additional context in comments.

2 Comments

@DevlshOne my problem is how to display it on canvas.
@user2896649 then make another question and ask how to display it on a canvas :)
0

several errors in your code. 1) its not imagesArr.push.image1, its imagesArr.push(image1) 2) the resulting array is 1-dimentional, so you cant do imagesArr[x][y], just use imagesArr[x]

2 Comments

Yes I notice that error after I have submitted the question. Thanks for the observation. With regards to your answer, I want display all the images using SetInterval one image at a time.
make another question then. You cant keep changing the question and incorporating the answers in the question. You said it was empty and thats why they were not showing. Also this was the first and correct answer yet you chose another one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.