0

I would like to know how I can access the var randomnumber inside that cubesmixed-Array This is the code:

var cubesmixed = [paper.rect(randomnumber, randomnumber, 0, 0), 
                  paper.rect(randomnumber, randomnumber, 0, 0), 
                  ... going on till over 100]; 
var randomnumber;
for(var i = 0; i < 143; i++) {
var randomnumber=Math.floor(Math.random()*2000);
    cubesmixed[i].animate({ width: 25, height: 25 }, 500, "bounce");
    console.log(i);
}

inside that array the value of randomnumber is 0. How can I do this?

Cheers

1
  • What do you mean 'inside that array the value of randomnumber is 0'? You mean that raphael is generating rectangles at 0,0? Surely that'll be because randomnumber == undefined when the array is defined? Commented Oct 13, 2013 at 20:52

1 Answer 1

1

You declare randomnumber twice, both times after using it in the cubesmixed declaration. So currently each time you actually use randomnumber it'll be undefined. Currently you set randomnumber equal to a randomly generated number inside your for loop, but you don't actually use it within the loop.

If your actual goal to create an array of 143 items each of which have different random coordinates then you can do this:

var cubesmixed = []; 
var cube;
for(var i = 0; i < 143; i++) {
  cube = paper.rect(Math.floor(Math.random()*2000), Math.floor(Math.random()*2000), 0, 0);
  cube.animate({ width: 25, height: 25 }, 500, "bounce");
  cubesmixed.push(cube);
}

On the other hand if you just want all the items to have the same coordinates as each other move your current line:

var randomnumber=Math.floor(Math.random()*2000);

...from inside the loop to before the cubesmixed declaration.

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

1 Comment

thank you very much. You sir got exactly what I wanted to do (yeah I removed that second randomnumber immediatly to stop confusing). Your first example was the way to go, thanks!

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.