tags:

views:

54

answers:

2

I am trying to animate random properties on the x and y's

 //generates a random right or left
var randX = function(){
    var randXresult;
 var startWith = Math.random()*5;
 if(startWith>3){
  randXresult="right";
  }
 else{randXresult="left"}
  return randXresult
 }
//generates a random top or bottom
var randY = function(){
    var randYresult;
 var startWith = Math.random()*5;
 if(startWith>3){
  randYresult="top";
  }
 else{randYresult="bottom"}
  return randYresult
 }

//generates a random number 
var getRandomNo = function(){
 var randomNo = Math.random()*9999;
  return randomNo
 }

 // i am only showing the first .toggle() function 
 $(".innerColContainer div a").toggle(
function(){
 $(this).parent().siblings().each(function(i){
  Xrandom = randX()           
  Yrandom = randY()

  alert(Xrandom+Yrandom) //this alerts the results i am looking for 
 //but how do i get the results in the .animate()
 $(this).animate({ Xrandom:getRandomNo(), Yrandom:getRandomNo()}, 1400);
  });

So my question is: How could i get that random "top" or "right" inside the .animate() since alert(Xrandom+Yrandom) has the value i am looking for.

+1  A: 
var props = {};
props[Xrandom]=getRandomNo();
props[Yrandom]=getRandomNo();
$(this).animate(props, 1400);
David Hedlund
thanks, but what if i want to add static properties. like `opacity`.
adardesign
i've tried it myself as well, and it does work. `props['opacity'] = '0.1'` works as well. what i suspect might not be working for you here, is that you can't set `left` when you've already set `right` or vice versa (same goes for top/bottom). you need to reset the value you won't be using. opacity should be fine tho
David Hedlund
Proof of concept, create a new html doc containing only this: http://pastebay.com/71880
David Hedlund
Thanks, it does work now! Also thanks for the http://pastebay.com link
adardesign
+1  A: 

If I'm reading correctly, "getRandomNo()" will not return an evenly distributed random number (which is usually what you want, but maybe it isn't in this case...only you can tell what you want it to do). 80% of the results will be evenly distributed between 5000 and 12999, and 20% of the results will be evenly distributed between 8000 and 9999. 8000-9999 will be twice as likely a result as 5000-7999 and 10000-12999.

Brian Schroth
you have a good point, but besides 80% of the results will be evenly distributed between 5000 and 12999 `getRandomNo()` is working when i try it with static properties. kudos
adardesign
looks like you've edited getRandomNo to work correctly. You might want to consider refactoring randX and randY as they don't really make much sense either (why is the variable named "startWith"? Why is it doing a random number between 0 and 5? Is it intentional that "top" will be picked 40% of the time while "Bottom" will be picked 60%?
Brian Schroth
sorry for the nitpicks instead of a real answer, but I would have thought d.'s answer would work so I have no solution for you.
Brian Schroth
Thanks Brien, your correct, how would i make top and bottom's chance randomly even?
adardesign
randomNum = Math.random() - 0.5; if(randomNum >= 0) { //do stuff } else { //do other stuff } would give you an even 50-50 distribution. Math.random gets you a value between 0 and 1 so you need to then do something for the cases where that value is less than .5 and something for the cases where that value is greater than .5.
Brian Schroth
Thanks much, this works.
adardesign