0

Oke, so i have a array of 6 objects (img.wolk) and i want them to move left to right randombly, at a random speed done that

But i also want that when they are out of the screen that they move to the other site and start over again, indefinitely.

So that when the object is all the way to the left that it teleports to the right and continues going left.

code

function wolkMovement() {
console.log(wolkMovement);
var wolken = document.querySelectorAll('img.wolk');
var speed;
var randomOrder = ['-50%', '100%'];

for (var i = 0; i < wolken.length; i++) {

    var derection;
    derection= randomOrder[Math.floor(Math.random() * randomOrder.length)];
    speed= Math.floor(Math.random() * (100000 - 2000 + 1) + 2000);
    //random the derection and the speed

    $(wolken[i]).animate({
            left: derection
        }, speed, function () {
        //teleport to other site and repeat
    });
}
}
   
Community
  • 1
  • 1
noe m
  • 167
  • 6
  • Either rerender the cloud at the other side, or make it invisible and move it manually. What is your question, sicne the code looks alright. – Shilly Oct 05 '15 at 15:32
  • that is exacly my question to move it to the other side and run the code again, because i cant seem to get it working with the array. Because when i try someting like that it only works for the last element in the array. :( – noe m Oct 05 '15 at 15:52
  • I'm guessing that [this question](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) may help you in that case - chances are you're attempting to use `i` within the callback function passed to `animate`, but by the time it runs, it's only ever got the value of the final iteration of the loop. – James Thorpe Oct 05 '15 at 15:54
  • aaah, i see so if i understand i correctly i must run a seperate function with the parameter "i" so its gets remembered. right? – noe m Oct 05 '15 at 16:01

0 Answers0