1

"Working" example is here (this script deals with the triangle animation)

http://movable.pagodabox.com/

full code here: http://pastebin.com/rgPNxHgJ

This question is mainly about the proper syntax. I have the following:

shape.transitionTo({
     offset: {
          x: 10,
          y: 10
     }
})

What I want to do is have "x" and "y" be randomly selected from an array, for example:

    movementIn = [
        {x: 34, y: 66},
        {x: -34, y: -66}
    ],

    shape.transitionTo({
        offset: movementIn[Math.floor(Math.random() * movementIn.length)],
    });

But this doesn't seem to be working... it seems to be only choosing the first item every time. Am I doing something wrong here?

how do I select a random X and Y pair and insert it into the "offset" parameter?

Thanks!

3
  • Syntax looks correct ... maybe the problem is you're initializing the value rather than getting a new value every time? Hard to tell without the context. Commented Aug 5, 2012 at 1:33
  • 1
    The ], looks out of place (in both places, a syntax error in one and a semantic error in the other). Please paste a [minimal] test-case on jsfiddle [focusing on the specific question] and the exact code being used .. Commented Aug 5, 2012 at 1:37
  • stackoverflow.com/questions/4550505/… Commented Aug 5, 2012 at 1:54

1 Answer 1

1

"Works for me"

arr = ["a","b","c"]
res = ""
for (i = 0; i < 10; i++) {
   res += arr[Math.floor(Math.random() * arr.length)]
}
alert(res)

Do note that this is not the "correct" way to pick one item as the distribution is slightly skewed ..

There are some syntax and semantical issues with the code in the question that should be explored:

{
   although_SomeBrowsers: "accept me",
   iAmAnInvalidLiteral: "BecauseThereIsAnExtraComma",
}

I feel trolled, here you go:

arr = [{x:1,y:-1},{x:2,y:-2},{x:3,y:-3}]
for (i = 0; i < 10; i++) {
   AN_OBJECT = arr[Math.floor(Math.random() * arr.length)]
   // do whatever you want to do with what AN_OBJECT names
   alert("x: " + AN_OBJECT.x + " y: " + AN_OBJECT.y)
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks pst, but this isn't the same... in my array, there are two pairs, where each array item is an X and Y pair... I just think I'm not doing it quite right!
@j-man86 It is exactly the same. It doesn't matter what object is selected; there is still a singular object selected. Change res = "" to res = [] and res += to res.push and then you have an array of your arbitrary object. My point is the code posted Works Here, insofar as the errors in it are fixed. The idea is correct.
Maybe im not asking the right question: how do I select a random X and Y pair and insert it into the "offset" parameter?

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.