A Flash Developer Resource Site

1. random rotation AS2

HI

Im making a Jigsaw puzzle, I want that every time the game loads the pieces rotate to a random position, but no a range of rotation, I mean, I know how to do that

Code:
```onClipEvent (load) {
this._rotation=random(360)+1;
}```
I want my pieces to random select between 4 possible rotations, 0, 90, 180, 270, so the player can rotate the pieces to put them in the right place

Is the an easy way to do that?

2. _rotation = 360 / Math.ceil(Math.random()*4);

3. Originally Posted by bluemagica
_rotation = 360 / Math.ceil(Math.random()*4);
Nice trick blue,; I've got to remember that one. Let's see if I can get that division out of there.

_rotation = Math.ceil(Math.random()*4) * 360 * .25

4. Just to be picky, I took a few minutes to read up on random optimization. Truth be told, Math.Random is horridly slow. Not sure it gets much faster than this:

Code:
```const MAX_RATIO:Number = 1 / uint.MAX_VALUE;
var r:uint = Math.random() * uint.MAX_VALUE;

function XORandom():Number{
r ^= (r << 21);
r ^= (r >>> 35);
r ^= (r << 4);
return (r * MAX_RATIO);
}

_rotation = int(XORandom()*4) * 90;```
This also stays true to Scheletro's original question of 0,90,180,270 instead of 90,180,270,360, though there's no difference, really. Long live bitwise!

EDIT: Over one million iterations, calls to XORandom are over 75% faster than calls directly to Math.Random().

5. Hmm... just thought about this... Swapping the MAX_VALUE for 4 / uint.MAX_VALUE allows you to completely remove the multiplication by 4 from the call line:

Code:
```const MAX_RATIO:Number = 4 / uint.MAX_VALUE;
var r:uint = Math.random() * uint.MAX_VALUE;

function XORandom():Number{
r ^= (r << 21);
r ^= (r >>> 35);
r ^= (r << 4);
return (r * MAX_RATIO);
}

_rotation = int(XORandom()) * 90;```

6. hi guys, thanks for the help, but I find more issues

Bluemagica - Some times the pieces appear in an intermediate position like 135, strange

ImprisionedPride - I have some syntax errors, lol, I dont quite understand your code to fix it

7. weird, that shouldn't happen! Try tracing out all the values....

or you might wanna try
Code:
`_rotation = (90*Math.floor(Math.random()*4)); //should give 0,90,180,270`
trace your rotation right before and after that step, something else in your code might be modifying the rotation.

As for IP's code, that is a optimised code for doing randoms, and there shouldn't really be syntax errors, you should give some info about the error. I have been doing bitwise stuff to optimise my own game too, so i kind of understand what is going on there, but you better lay off that unless you really need it. Bitwise operations can really help optimise a lot of stuff, but understanding them can be a pain. It's better to just remember these as snippets, like for example
Code:
```var i:int = 10;
i = -i; //-10, normal slow process
Or
i = (i ^ -1) + 1;// -10.  about 300% faster than previous one```

8. You shouldn't any errors even though my code was written for AS3. It's also worth noting that my previous posts' numbers were from the FP10 and AS3; I'm not sure how much overhead you save instead of Math.random() in AS2.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap