-
Game Player - Developer
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?
"I love to make them as I love to play them"
-
Senior Member
_rotation = 360 / Math.ceil(Math.random()*4);
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:
-
Funkalicious
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
-
Pumpkin Carving 2008
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().
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Pumpkin Carving 2008
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;
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Game Player - Developer
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
"I love to make them as I love to play them"
-
Senior Member
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
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:
-
Pumpkin Carving 2008
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.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|