Actionscript:
-
var target:Number = 360;
-
var steps:Array = new Array();
-
for (var step:Number = 0; step <target; step += int(Math.random() * 36 + 36)){
-
steps.push(Math.min(target,step));
-
}
-
steps.push(target);
-
trace(steps);
-
/* outputs something similar to:
-
0,46,99,144,189,259,330,360
-
*/
This is something I've had to do a few times recently.... it randomly steps a number toward a given target...
Also posted in misc | Tagged actionscript, flash |
By Zevan | March 20, 2009
Actionscript:
-
function uniqueCompare(arr:Array, compare:Function):void {
-
var leng:int = arr.length;
-
for (var i:int = 0; i<leng; i++){
-
for (var j:int = i + 1; j<leng; j++){
-
compare(arr[i], arr[j]);
-
}
-
}
-
}
-
-
var numbers:Array = [1, 2, 3, 4, 5];
-
-
uniqueCompare(numbers, compareCallback);
-
-
function compareCallback(a:*, b:*):void {
-
trace("compare " + a + " to " + b);
-
}
-
-
/*
-
outputs:
-
compare 1 to 2
-
compare 1 to 3
-
compare 1 to 4
-
compare 1 to 5
-
compare 2 to 3
-
compare 2 to 4
-
compare 2 to 5
-
compare 3 to 4
-
compare 3 to 5
-
compare 4 to 5
-
*/
This snippet shows a function called uniqueCompare() that compares all elements in an array to all other elements in an array without repeating a comparison.
Also posted in misc | Tagged actionscript, flash |
By Zevan | February 27, 2009
Actionscript:
-
function randomize(a:*, b:*):int{
-
return Math.round(Math.random()*8) - 4;
-
}
-
-
var i:int;
-
var fruits:Array;
-
-
trace("Math.random()");
-
for (i = 0; i<4; i++){
-
// reset fruits array:
-
fruits = ["apple", "grape","pear","cherry"];
-
fruits.sort(randomize);
-
trace(fruits);
-
}
-
-
-
// seeds
-
var s1:Number= 0xFF00FF;
-
var s2:Number = 0xCCCCCC;
-
var s3:Number= 0xFF00F0;
-
-
function tRandomize(a:*, b:*):int{
-
return Math.round(rand()*8) - 4;
-
}
-
-
trace("\nTausworthe rand()");
-
for (i= 0; i<4; i++){
-
fruits = ["apple", "grape","pear","cherry"];
-
fruits.sort(tRandomize);
-
trace(fruits);
-
}
-
// from www.ams.org/mcom/1996-65-213/S0025-5718-96-00696-5/S0025-5718-96-00696-5.pdf
-
function rand():Number {
-
s1=((s1&4294967294)<<12)^(((s1<<13)^s1)>>19);
-
s2=((s2&4294967288)<<4)^(((s2<<2)^s2)>>25);
-
s3=((s3&4294967280)<<17)^(((s3<<3)^s3)>>11);
-
var r:Number = (s1^s2^s3) * 2.3283064365e-10;
-
r = (r<0) ? r+=1 : r;
-
return r;
-
}
-
-
/*
-
outputs:
-
Math.random()
-
grape,apple,pear,cherry
-
pear,cherry,apple,grape
-
grape,apple,pear,cherry
-
grape,apple,cherry,pear
-
-
Tausworthe rand()
-
apple,grape,pear,cherry
-
cherry,grape,pear,apple
-
grape,apple,cherry,pear
-
grape,pear,apple,cherry
-
*/
The above shows how to randomly sort or shuffle an array. This is useful in games. To achieve this I made use of the compareFunction argument of Array.sort(). Most sorting algorithms go through the array and compare values until the desired sort order is achieved. The compareFunction argument is a function that takes two values a and b and returns an integer that is negative positive or zero... see this info from the docs:
* A negative return value specifies that A appears before B in the sorted sequence.
* A return value of 0 specifies that A and B have the same sort order.
* A positive return value specifies that A appears after B in the sorted sequence.
So in the case of a randomizing an array you simply need to return a random int -1, 0 or 1. This is what I've done in the past (Math.round()*2) -1) ... but when I was writing this snippet it seemed like 0 caused less variation in the output of the array so I made the range from -4 to 4 instead. This could have just been my imagination, but it seems like having less chance of a zero caused the arrays to be a bit more shuffled.
The reason I also included a version that uses Tausworthe is because of the easy seeding. In some cases you may want to use seeded randomness to sort an array.
UPDATE:
Was digging around about this and found a much faster method for randomizing arrays... not a big deal if you have small arrays, but if you need to randomize 1000's of values this method is much faster than using Array.sort()
Also posted in misc | Tagged actionscript, flash |
By Zevan | January 31, 2009
Actionscript:
-
package {
-
import flash.display.Sprite;
-
-
[SWF(width=500, height=500, backgroundColor=0xEFEFEF, frameRate=30)]
-
public class Main extends Sprite{
-
private var _robotManager:RobotManager;
-
public function Main(){
-
_robotManager = new RobotManager(this, 12);
-
}
-
}
-
}
-
-
class RobotManager{
-
-
private var _robots:Array;
-
private var _top:Sprite;
-
private var _robotNum:int;
-
-
public function RobotManager(top:Sprite, robotNum:int){
-
_top = top;
-
_robotNum = robotNum;
-
_robots = new Array();
-
setupRobots();
-
top.addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
-
private function setupRobots():void{
-
for (var i:int = 0; i<_robotNum; i++){
-
_robots[i] = new Robot();
-
_robots[i].x = 100+(i % 3) * 150;
-
_robots[i].y = 100+int(i / 3) * 100;
-
_robots[i].addEventListener("death", onRobotDie);
-
_top.addChild(_robots[i]);
-
}
-
}
-
private function onRobotDie(evt:Event):void{
-
for (var i:int = 0; i<_robotNum; i++){
-
if (_robots[i] == evt.currentTarget){
-
_robots.splice(i, 1);
-
}
-
}
-
_robotNum--;
-
}
-
-
private function onRun(evt:Event):void{
-
collisions();
-
}
-
-
private function collisions():void{
-
var currBot:Robot;
-
for (var i:int = 0; i<_robotNum; i++){
-
currBot = _robots[i];
-
for (var j:int = 0; j<_robotNum; j++){
-
if (currBot != _robots[j]){
-
var dx:Number = currBot.x - _robots[j].x;
-
var dy:Number = currBot.y - _robots[j].y;
-
if (Math.sqrt((dx * dx) + (dy * dy)) <40){
-
currBot.die();
-
}
-
}
-
}
-
}
-
}
-
}
-
-
import flash.display.*
-
import flash.events.*;
-
-
class Robot extends Sprite{
-
private var _inc:Number
-
private var _td:Number;
-
private var _theta:Number;
-
private var _rad:Number;
-
-
public function Robot(){
-
_theta = Math.random() * (Math.PI * 2);
-
_td = _theta;
-
_inc = Math.random() * .2 + .05;
-
_rad = _inc * 10;
-
with(graphics){
-
beginFill(0x666666);
-
drawRect(-10,-10,20,20);
-
endFill();
-
beginFill(0x333333);
-
drawRect(-5, -5, 20, 10);
-
lineStyle(0,0xAAAAAA);
-
endFill();
-
drawCircle(0,0,20);
-
}
-
addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
-
private function onRun(evt:Event):void{
-
_td += _inc;
-
_theta += _inc * Math.cos(_td);
-
if (x <0 || y <0 || x> 500 || y> 500){
-
_theta += Math.PI;
-
}
-
x += _rad * Math.cos(_theta);
-
y += _rad * Math.sin(_theta);
-
rotation = _theta / Math.PI * 180;
-
}
-
-
public function die():void{
-
removeEventListener(Event.ENTER_FRAME, onRun);
-
addEventListener(Event.ENTER_FRAME, onDie);
-
}
-
-
private function onDie(evt:Event):void{
-
scaleX = scaleY += .1;
-
alpha -= .1;
-
if (scaleX> 2){
-
removeEventListener(Event.ENTER_FRAME, onDie);
-
if (parent){
-
parent.removeChild(this);
-
dispatchEvent(new Event("death"));
-
}
-
}
-
}
-
}
This code should be placed in a document class... not the timeline (hopefully you already know that)
The above creates 12 wandering robots that die when they collide with one another.
This is not a snippet really... but it's only slightly over 100 lines and it contains a few tricks I find myself using from time to time.
You can view the swf here.