Braden's Code
From Physical Programming
Frame 1
Mouse.hide()
Hides cursor
pi = Math.PI;
Sets variable ‘pi’ to 3.1415... so that I don’t have to keep calling the maths function
This function takes a finds the position of a movie clip ‘b(x)’ and saves it x and y position to the variables ‘px’ and ‘py’ respectively
function getxy(mcName) {
px = _root['b'+mcName]._x;
py = _root['b'+mcName]._y;
}
This function takes a number from 0 to 5 and converts it into a direction by multiplying the number by 60˚. It then returns a set off coordinates at a distance of 20.5 in that direction from point (px,py). This is used to find the next point on a hexagonal grid. The new point is saved as (offx, offy)
function offset(dir) {
direct = dir*60; radians = (direct*pi/180);
offx = (Math.sin(radians)*20.5)+px;
offy = (Math.sin(radians-(pi/2))*20.5)+py;
This function creates a new object at point (offx,offy) named ‘b’ + an imputed number. The orientation other the object is found using the seconded input, a number between 0 and 5 which is multiplied by 60˚ to give a direction.
}
function newBlock(mcName, dirct) {
_root['b1'].duplicateMovieClip('b'+mcName,level); _root['b'+mcName]._x = offx; _root['b'+mcName]._y = offy; _root['b'+mcName]._rotation = (dirct*60);
This function saves the names of the last object created to the array ‘blockArray’. It then counts how many objects have been made and saves the number to the variable ‘numBlocks’.
}
function save(num) {
blockArray.push('b'+num); numBlocks = blockArray.length;
}
_root['b1']._alpha = 0; hides object ‘b1’ _root['b1']._x = _root._xmouse; _root['b1']._y = _root._ymouse; tells object b1 to follow the mouse _root['queen']._x = _root._xmouse; _root['queen']._y = _root._ymouse;
tells object ‘queen’ to follow the mouse
if (Key.isDown(Key.SPACE)) {
Triggers the following code when user presses space _root['b1']._alpha = 100; Makes ‘b1’ visible again n = 2; level = 2; px = _root['b1']._x; py = _root['b1']._y; saves b1’s x and y coordinates to variables px and py T = 100; This variable is the maximum number of objects that will be created a = 1; blockArray = ['b1']; Adds the first object ‘b1’ to blockArray
Cycles through all 6 sides of object ‘b1’ and has a 70% chance of creative an object connection onto each side. For each object it creates, it saves it’s name to array blockArray. for (d=0; d<6; d++) { ranPlace = Math.round(Math.random()*100); if (ranPlace<=70) { offset(d); newBlock(n,d); save(n); n++; level++; } } i = 0; gotoAndPlay(3);
}
once this code has been run, the program move onto frame 3 if space isn’t pressed and code does run, program moves to frame 2…
Frame 2 –
GotoAndPlay(1);
…and is immediately sent back to frame 1.
Frame 3 –
Mouse.show();
makes cursor visible again
getxy(i+1);
finds the position of the next object
This looks around the edges of the object to see if there are any adjacent objects on each of the six sides
for (d=0; d<6; d++) {
offset(d); for (t=0; t<numBlocks; t++) { temp_1 = eval(blockArray[t]); if (temp_1.hitTest(offx, offy, true)) { safe = false; break;
} } If there isn’t already an object on one side, there is a 60% chance that an object will be created in that position. This probability is reduced for each block by dividing by variable ‘a’. A begins at 1 and is stepped down by .05 each cycle. if (safe == true) { ranPlace = Math.round(Math.random()*100); if (ranPlace<=60/a) { newBlock(n,d); save(n); level++; n++;
} } If maximum number of blocks is exceeded, the loop is stopped safe = true; if (numBlocks>T) { break; }
}
if (numBlocks>T) {
break;
} a += .05;
i++;
Program steps through 5 frames to allow time for the animation of a new object being created…
Frame 8 –
If the program has looked at all existing objects, it can move onto the next frame,
if (i == numBlocks) {
Otherwise, it goes back to frame 3 to look at the next object
} else {
gotoAndPlay(3);
}
Frame 9 – This frame resets the count to start with block ‘b1’ again…
i = 0;
Frame 10 –
getxy(i+1); finds the position of the next object created = false; ran = (Math.round(Math.random()*6)); Randomly chooses a side of the object offset(ran); Finds the coordinates for that side if (ran == 1) { type = 'me'; } else if (ran == 2) { type = 'mf'; } else if (ran == 3) { type = 'ma'; } else if (ran == 4) { type = 'mb'; } else if (ran == 5) { type = 'mc'; } else { type = 'md'; Sets ‘type’ to ‘ma’, ‘mb’, ‘mc’, etc… depending on which side has been chosen } for (t=1; t<T; t++) { temp = eval(blockArray[t]);
Looks to see if there is already an object in this location. If so, both objects are removed and new object is created in their place. The type of object is determined by the value of ‘type’. if (temp.hitTest(offx, offy, true)) {
removeMovieClip(temp); removeMovieClip(eval(blockArray[i])); _root[type].duplicateMovieClip(type+n,level); _root[type+n]._x = offx; _root[type+n]._y = offy; The objects are removed from blockArray and replace with the string ‘empty’ so that the number of values in the array stays the same.
blockArray[i] = 'empty'; blockArray[t] = 'empty'; n++; level++; When the program finds an object in the location it’s looking at, it breaks the loop so that it doesn’t waste time on redundant calculations break; } }
i++
Continue to frame 11…
Frame 11 –
If the program has gone through all the objects, it stops
if (i == T) {
stop();
} else {
gotoAndPlay(10);
}
Otherwise it goes back and loops frame 10

