Frame 1
PopcornMain.getInstance().init(mc_canvas);
Symbol 29 MovieClip [ball] Frame 1
#initclip 6
Object.registerClass("ball", Ball);
#endinitclip
Symbol 32 MovieClip Frame 1
function onSelectURLItem(obj, menuItem) {
getURL (menuItem.url, "_blank");
}
var items = [];
items[0] = {label:"popcorn painting .com"};
items[1] = {label:"by Rafa\u00EBl Rozendaal, 2008", url:"http://www.newrafael.com"};
items[2] = {label:"programming by Reinier Feijen", url:"http://www.boxofchocolates.nl"};
var defaultItems = {zoom:true, quality:true, loop:false, play:false, print:true, rewind:false, forwardAndBack:false, save:true};
var enableTextItems = false;
var myContextMenu = new ContextMenu();
var i = 0;
while (i < items.length) {
var item = new ContextMenuItem(items[i].label, function () {
});
if (items[i].separatorBefore != null) {
item.separatorBefore = items[i].separatorBefore;
}
if (items[i].url != null) {
item.url = items[i].url;
item.onSelect = onSelectURLItem;
} else {
item.enabled = enableTextItems;
}
myContextMenu.customItems.push(item);
i++;
}
for (var s in defaultItems) {
myContextMenu.builtInItems[s] = defaultItems[s];
}
this._parent.menu = myContextMenu;
Symbol 33 MovieClip [__Packages.PopcornMain] Frame 0
class PopcornMain
{
var radius, stage_xmax, basemc, sl, balls, grid, pops, hits, coll_history, supporting, supported, frame_count, next_pop, update_interval, next_update, interval, sound_bg, floor, bounds, sounds, reserves, next_x;
function PopcornMain () {
radius = PopcornConfig.radius;
stage_xmax = PopcornConfig.stage_max_width;
}
static function getInstance() {
if (instance == null) {
instance = new PopcornMain();
}
return(instance);
}
function init(mc) {
basemc = _root;
Stage.align = "LB";
Stage.scaleMode = "noScale";
sl = new StageListener(800, 600);
Stage.addListener(this);
onResize(true);
balls = [];
grid = [];
pops = [];
hits = [];
coll_history = [];
supporting = [];
supported = [];
frame_count = 0;
next_pop = 0;
createInitialPopcorn();
readSounds();
update_interval = PopcornConfig.update_interval;
next_update = 0;
var cont = this;
basemc.onMouseDown = function () {
cont.pause(!cont.bool_paused);
};
Key.addListener(this);
pause(PopcornConfig.start_paused);
}
function onKeyDown() {
if (Key.isDown(32)) {
if (String.fromCharCode(Key.getAscii()) == "d") {
bool_debug = !bool_debug;
} else if (String.fromCharCode(Key.getAscii()) == "p") {
pause(!bool_paused);
} else if (String.fromCharCode(Key.getAscii()) == "s") {
bool_sounds = !bool_sounds;
}
}
}
function pause(b) {
bool_paused = !(b === false);
clearInterval(interval);
if (!bool_paused) {
interval = setInterval(this, "step", 1);
startBgSound();
} else {
stopBgSound();
}
}
function startBgSound() {
if (sound_bg == null) {
sound_bg = new Sound(_root);
sound_bg.attachSound("bgsound");
sound_bg.onSoundComplete = function () {
this.start();
};
}
sound_bg.start();
}
function stopBgSound() {
sound_bg.stop();
}
function step() {
if (getTimer() >= next_update) {
next_update = getTimer() + update_interval;
update();
}
}
function update() {
checkCollisions();
var _local2 = hits.length - 1;
while (_local2 >= 0) {
var _local3 = balls[hits[_local2]];
_local3.update();
if (((_local3._y + _local3.radius) >= (floor - 10)) && (_local3.angle < Math.PI)) {
_local3._y = floor - radius;
_local3.__set__fase(Ball.FASE_STILL);
registerBallToGrid(hits[_local2]);
hits.splice(_local2, 1);
} else if (((_local3._x - radius) >= bounds.xMax) || ((_local3._x + radius) <= bounds.xMin)) {
_local3.destroy();
delete balls[hits[_local2]];
hits.splice(_local2, 1);
refill_count = refill_count + refill_amount;
}
_local2--;
}
_local2 = pops.length - 1;
while (_local2 >= 0) {
var _local3 = balls[pops[_local2]];
_local3.update();
if ((_local3._y + radius) < bounds.yMin) {
_local3.destroy();
delete balls[pops[_local2]];
pops.splice(_local2, 1);
}
_local2--;
}
if (frame_count >= next_pop) {
next_pop = (frame_count + PopcornConfig.pause_between_pops_min) + (Math.random() * (PopcornConfig.pause_between_pops_max - PopcornConfig.pause_between_pops_min));
pop();
}
frame_count++;
}
function onResize(dont_update) {
bounds = {xMin:sl.O[0], xMax:sl.O[0] + Stage.width, yMin:sl.O[1], yMax:sl.O[1] + Stage.height};
floor = bounds.yMax - PopcornConfig.floor;
}
function readSounds() {
sounds = [];
var _local3 = 1;
while (true) {
var _local2 = new Sound(basemc);
_local2.attachSound("pop" + _local3);
if (_local2.duration == undefined) {
break;
}
sounds.push("pop" + _local3);
_local3++;
}
}
function playPopSound() {
if (bool_sounds) {
var _local2 = new Sound(basemc);
var _local3 = sounds[random(sounds.length)];
_local2.attachSound(_local3);
_local2.start();
}
}
function createInitialPopcorn() {
reserves = [];
var _local6 = 1;
var _local2 = stage_xmax;
var _local4 = 0;
while (_local4 < _local6) {
var _local7 = ((PopcornConfig.fixed_layers_offset + bounds.yMax) - ((_local4 * 1.2) * PopcornConfig.radius)) - 1;
var _local5 = (-radius) / 2;
while (_local5 < _local2) {
var _local3 = addBall(_local5, (_local7 + (Math.random() * 10)) - 5);
reserves.push(_local3);
_local5 = _local5 + (radius + ((0.2 * Math.random()) * radius));
}
_local4++;
}
var _local7 = (floor - PopcornConfig.radius) - 1;
var _local5 = (-radius) / 2;
while (_local5 < _local2) {
var _local3 = addBall(_local5, (_local7 + (Math.random() * 10)) - 5);
if (bool_debug) {
new Color(_local3).setRGB(16744576);
}
balls.push(_local3);
registerBallToGrid(balls.length - 1);
supporting[balls.length - 1] = [];
supported[balls.length - 1] = [];
_local5 = _local5 + (radius + ((0.2 * Math.random()) * radius));
}
var _local8 = Math.round(_local2 / 100);
_local7 = (floor - (2 * PopcornConfig.radius)) - 1;
_local4 = 0;
while (_local4 < 8) {
_local5 = Math.random() * _local2;
var _local3 = addBall(_local5, (_local7 + (Math.random() * 10)) - 5);
if (bool_debug) {
new Color(_local3).setRGB(16744576);
}
balls.push(_local3);
registerBallToGrid(balls.length - 1);
supporting[balls.length - 1] = [];
supported[balls.length - 1] = [];
_local4++;
}
}
function addBallFromReserve() {
var _local3 = 0;
while (_local3 < refill_count) {
do {
var mc = reserves.splice(random(reserves.length), 1)[0];
} while ((mc._x > bounds.xMax) || (mc._x < bounds.xMin));
if (bool_debug) {
new Color(mc).setRGB(255);
}
balls.push(mc);
registerBallToGrid(balls.length - 1);
supporting[balls.length - 1] = [];
supported[balls.length - 1] = [];
var _local2 = addBall(mc._x, mc._y);
if (bool_debug) {
new Color(_local2).setRGB(65280);
}
reserves.push(_local2);
mc.swapDepths(_local2);
_local3++;
}
refill_count = refill_count - Math.floor(refill_count);
}
function addBall(x, y) {
var _local3 = basemc.getNextHighestDepth();
var _local2 = basemc.attachMovie("ball", "ball" + _local3, _local3, {_x:x, _y:y});
_local2._xscale = PopcornConfig.scale;
_local2._yscale = _local2._xscale;
_local2.cacheAsBitmap = true;
return(_local2);
}
function pop() {
var _local4 = PopcornConfig.pop_angle_min + ((PopcornConfig.pop_angle_max - PopcornConfig.pop_angle_min) * Math.random());
var _local3 = PopcornConfig.pop_speed_min + ((PopcornConfig.pop_speed_max - PopcornConfig.pop_speed_min) * Math.random());
if (next_x != undefined) {
var _local6 = next_x;
} else {
var _local6 = Math.random() * bounds.xMax;
}
delete next_x;
var _local5 = (bounds.yMax - radius) + (_local3 * Math.sin(_local4));
var _local2 = addBall(_local6, _local5);
_local2.speed = _local3;
_local2.angle = _local4;
_local2.fase = Ball.FASE_POP;
balls.push(_local2);
pops.push(balls.length - 1);
playPopSound();
addBallFromReserve();
}
function checkCollisions() {
var _local16 = radius * 1.6;
var _local10 = 1;
var _local15 = hits.length - 1;
while (_local15 >= 0) {
var _local11 = balls[hits[_local15]];
var _local13 = _local11.__get__pos();
var _local14 = Math.floor(_local11.__get__pos().x / radius) + 10;
var _local12 = Math.floor(_local11.__get__pos().y / radius) + 10;
if (_local11.__get__pos().y < (bounds.yMax - 0)) {
} else {
var _local8 = false;
var _local9 = _local14 - _local10;
while (_local9 <= (_local14 + _local10)) {
if (_local8) {
break;
}
var _local6 = _local12 - _local10;
while (_local6 <= (_local12 + _local10)) {
if (_local8) {
break;
}
var _local3 = grid[_local9][_local6];
if (_local3 != undefined) {
var _local2 = 0;
while (_local2 < _local3.length) {
var _local5 = balls[_local3[_local2]];
var _local7 = _local5.__get__pos();
var _local4 = _local16 - flash.geom.Point.distance(_local13, _local7);
if (_local4 > 0) {
if (bool_trace) {
trace((frame_count + " coll: ") + [hits[_local15], _local3[_local2]]);
}
if (hits[_local15] == _local3[_local2]) {
trace("!");
}
registerBallsToSupportArrays(_local3[_local2], hits[_local15]);
_local11.__set__fase(Ball.FASE_STILL);
registerBallToGrid(hits[_local15]);
hits.splice(_local15, 1);
_local8 = true;
break;
}
_local2++;
}
}
_local6++;
}
_local9++;
}
}
_local15--;
}
_local16 = (1.1 * radius) * 2;
_local10 = 2;
_local15 = 0;
while (_local15 < pops.length) {
var _local11 = balls[pops[_local15]];
var _local13 = _local11.__get__pos();
var _local14 = Math.floor(_local11.__get__pos().x / radius) + 10;
var _local12 = Math.floor(_local11.__get__pos().y / radius) + 10;
if (_local11.__get__pos().y < (bounds.yMax - 200)) {
} else {
var _local9 = _local14 - _local10;
while (_local9 <= (_local14 + _local10)) {
var _local6 = _local12 - _local10;
while (_local6 <= (_local12 + _local10)) {
var _local3 = grid[_local9][_local6];
if (_local3 != undefined) {
var _local2 = 0;
while (_local2 < _local3.length) {
var _local5 = balls[_local3[_local2]];
var _local7 = _local5.__get__pos();
var _local4 = _local16 - flash.geom.Point.distance(_local13, _local7);
if (_local4 > 0) {
doCollisionPop(_local11, _local5, _local4);
dropBall(_local3[_local2]);
}
_local2++;
}
}
_local6++;
}
_local9++;
}
}
_local15++;
}
}
function dropBall(i) {
balls[i].fase = Ball.FASE_HIT;
hits.push(i);
unregisterBallFromGrid(i);
unregisterSupporteeFromArrays(i);
if (bool_trace) {
trace((frame_count + " drop: ") + [i, supporting[i], "-", supported[i]]);
}
var _local2 = supporting[i].length - 1;
while (_local2 >= 0) {
if (supporting[i][_local2].fase == Ball.FASE_STILL) {
dropBall(supporting[i][_local2]);
}
_local2--;
}
unregisterSupporterFromArrays(i);
}
function doCollisionHit(o0, o1, overlap) {
var _local4 = 1;
var _local3 = 99999 /* 0x01869F */;
var _local21 = o0.__get__pos();
var _local20 = o1.__get__pos();
var _local11 = flash.geom.Point.polar(o0.speed, o0.angle);
var _local10 = flash.geom.Point.polar(0, 0);
var _local15 = new Vector(_local11.x, _local11.y);
var _local13 = new Vector(_local10.x, _local10.y);
var _local2 = new Vector(_local20.x - _local21.x, radius);
_local2.normalize(1);
var _local5 = new Vector(-_local2.y, _local2.x);
var _local16 = Vector.dotproduct(_local2, _local15);
var _local19 = Vector.dotproduct(_local5, _local15);
var _local17 = Vector.dotproduct(_local2, _local13);
var _local18 = Vector.dotproduct(_local5, _local13);
var _local23 = ((_local16 * (_local4 - _local3)) + ((2 * _local3) * _local17)) / (_local4 + _local3);
var _local22 = ((_local17 * (_local3 - _local4)) + ((2 * _local4) * _local16)) / (_local4 + _local3);
var _local9 = _local2.scale(_local23);
var _local12 = _local5.scale(_local19);
var _local8 = _local2.scale(_local22);
var _local14 = _local5.scale(_local18);
var _local6 = new Vector(_local9.x + _local12.x, _local9.y + _local12.y);
var _local24 = new Vector(_local8.x + _local14.x, _local8.y + _local14.y);
o0.angle = Math.atan2(_local6.y, _local6.x);
o0.speed = _local6.length * 0.01;
}
function doCollisionPop(o0, o1, overlap) {
var _local5 = 1;
var _local4 = 1;
var _local22 = o0.__get__pos();
var _local21 = o1.__get__pos();
var _local11 = flash.geom.Point.polar(o0.speed, o0.angle);
var _local10 = flash.geom.Point.polar(o1.speed, o1.angle);
var _local15 = new Vector(_local11.x, _local11.y);
var _local13 = new Vector(_local10.x, _local10.y);
var _local2 = new Vector(_local21.x - _local22.x, radius);
_local2.normalize(1);
var _local6 = new Vector(-_local2.y, _local2.x);
var _local16 = Vector.dotproduct(_local2, _local15);
var _local20 = Vector.dotproduct(_local6, _local15);
var _local17 = Vector.dotproduct(_local2, _local13);
var _local19 = Vector.dotproduct(_local6, _local13);
var _local24 = ((_local16 * (_local5 - _local4)) + ((2 * _local4) * _local17)) / (_local5 + _local4);
var _local23 = ((_local17 * (_local4 - _local5)) + ((2 * _local5) * _local16)) / (_local5 + _local4);
var _local9 = _local2.scale(_local24);
var _local12 = _local6.scale(_local20);
var _local8 = _local2.scale(_local23);
var _local14 = _local6.scale(_local19);
var _local25 = new Vector(_local9.x + _local12.x, _local9.y + _local12.y);
var _local7 = new Vector(_local8.x + _local14.x, _local8.y + _local14.y);
o1.angle = Math.atan2(_local7.y, _local7.x);
o1.speed = Math.min(35, _local7.length);
o1.bool_grav = true;
o1.bool_fric = true;
}
function registerBallToGrid(i) {
var _local2 = balls[i];
_local2.cell_x = Math.floor(_local2.__get__pos().x / radius) + 10;
_local2.cell_y = Math.floor(_local2.__get__pos().y / radius) + 10;
if (_local2.__get__fase() == Ball.FASE_STILL) {
if (grid[_local2.cell_x] == undefined) {
grid[_local2.cell_x] = [];
}
if (grid[_local2.cell_x][_local2.cell_y] == undefined) {
grid[_local2.cell_x][_local2.cell_y] = [i];
} else {
grid[_local2.cell_x][_local2.cell_y].push(i);
}
if (bool_trace) {
trace((frame_count + " reg: ") + [_local2.cell_x, _local2.cell_y, grid[_local2.cell_x][_local2.cell_y]]);
}
}
}
function unregisterBallFromGrid(i) {
var _local5 = balls[i];
var _local3 = grid[_local5.cell_x][_local5.cell_y];
if (bool_trace) {
trace((frame_count + " unreg: ") + [i, _local3]);
}
var _local2 = _local3.length - 1;
while (_local2 >= 0) {
if (_local3[_local2] == i) {
_local3.splice(_local2, 1);
}
_local2--;
}
}
function registerBallsToSupportArrays(supporter, supportee) {
if (supporting[supporter] == undefined) {
supporting[supporter] = [supportee];
} else {
var _local2 = supporting[supporter].length - 1;
while (_local2 >= 0) {
if (supporting[supporter][_local2] == supportee) {
supporting[supporter].splice(_local2, 1);
}
_local2--;
}
supporting[supporter].push(supportee);
}
if (supported[supportee] == undefined) {
supported[supportee] = [supporter];
} else {
var _local2 = supported[supportee].length - 1;
while (_local2 >= 0) {
if (supported[supportee][_local2] == supporter) {
supported[supportee].splice(_local2, 1);
}
_local2--;
}
supported[supportee].push(supporter);
}
}
function unregisterSupporteeFromArrays(supportee) {
var _local4 = supported[supportee].length - 1;
while (_local4 >= 0) {
var _local3 = supporting[supported[supportee][_local4]];
var _local2 = _local3.length - 1;
while (_local2 >= 0) {
if (_local3[_local2] == supportee) {
_local3.splice(_local2, 1);
}
_local2--;
}
_local4--;
}
supported[supportee] = [];
}
function unregisterSupporterFromArrays(supporter) {
var _local4 = supporting[supporter].length - 1;
while (_local4 >= 0) {
var _local3 = supported[supporting[supporter][_local4]];
var _local2 = _local3.length - 1;
while (_local2 >= 0) {
if (_local3[_local2] == supporter) {
_local3.splice(_local2, 1);
}
_local2--;
}
_local4--;
}
supporting[supporter] = [];
}
static var instance = null;
var bool_sounds = true;
var bool_debug = false;
var bool_trace = false;
var bool_paused = false;
var refill_count = 0;
var refill_amount = 1;
}
Symbol 34 MovieClip [__Packages.StageListener] Frame 0
class StageListener
{
var _origsize, O;
function StageListener (w, h) {
Stage.addListener(this);
_origsize = [w, h];
O = calcLT();
}
function onResize() {
O = calcLT();
for (var _local2 in _members) {
if (_members[_local2][0] ne undefined) {
alignItem(_members[_local2][0], _members[_local2][1], _members[_local2][2]);
} else {
delete _members[_local2];
}
}
}
function calcLT() {
var _local2 = [];
if (Stage.align.indexOf("L") > -1) {
_local2[0] = 0;
} else if (Stage.align.indexOf("R") > -1) {
_local2[0] = _origsize[0] - Stage.width;
} else {
_local2[0] = (_origsize[0] - Stage.width) / 2;
}
if (Stage.align.indexOf("T") > -1) {
_local2[1] = 0;
} else if (Stage.align.indexOf("B") > -1) {
_local2[1] = _origsize[1] - Stage.height;
} else {
_local2[1] = (_origsize[1] - Stage.height) / 2;
}
return(_local2);
}
function alignItem(mc, tp, opos) {
if (tp.indexOf("L") > -1) {
var _local5 = O[0] + opos[0];
} else if (tp.indexOf("R") > -1) {
var _local5 = (O[0] + Stage.width) - (_origsize[0] - opos[0]);
} else {
var _local5 = (O[0] + (0.5 * Stage.width)) - ((0.5 * _origsize[0]) - opos[0]);
}
if (tp.indexOf("T") > -1) {
var _local4 = O[1] + opos[1];
} else if (tp.indexOf("B") > -1) {
var _local4 = (O[1] + Stage.height) - (_origsize[1] - opos[1]);
} else {
var _local4 = (O[1] + (0.5 * Stage.height)) - ((0.5 * _origsize[1]) - opos[1]);
}
mc._x = _local5;
mc._y = _local4;
}
function addListener(mc, alignment) {
var _local4 = getKey(mc);
var _local3 = [mc._x, mc._y];
_members[_local4] = [mc, alignment, _local3];
alignItem(mc, alignment, _local3);
}
function removeListener(mc) {
var _local2 = getKey(mc);
delete _members[_local2];
}
function getKey(mc) {
return(mc._name.split("/").join("_"));
}
var _members = {};
}
Symbol 35 MovieClip [__Packages.PopcornConfig] Frame 0
class PopcornConfig
{
function PopcornConfig () {
}
static var floor = -10;
static var fixed_layers = 1;
static var fixed_layers_offset = -10;
static var start_paused = false;
static var update_interval = 25;
static var scale = 135;
static var stage_max_width = 2500;
static var radius = 60;
static var grav_acc = 3;
static var friction = 0.99;
static var elasticity = 0;
static var pop_angle_min = 3.92699081698724;
static var pop_angle_max = 5.49778714378214;
static var pop_speed_min = 40;
static var pop_speed_max = 60;
static var pause_between_pops_min = 3;
static var pause_between_pops_max = 6;
}
Symbol 36 MovieClip [__Packages.Ball] Frame 0
class Ball extends MovieClip
{
var radius, grav_acc, friction, rotation_speed, gotoAndStop, _totalframes, cacheAsBitmap, removeMovieClip, bool_grav, speed, angle, bool_fric, _x, _y, _rotation, bool_coll, __fase;
function Ball () {
super();
fase = (FASE_STILL);
radius = PopcornConfig.radius;
grav_acc = PopcornConfig.grav_acc;
friction = PopcornConfig.friction;
rotation_speed = 5;
stopMoving();
gotoAndStop(Math.round(Math.random() * _totalframes) + 1);
cacheAsBitmap = true;
}
function destroy() {
removeMovieClip();
}
function update() {
if (bool_grav) {
var _local2 = flash.geom.Point.polar(speed, angle);
_local2.y = _local2.y + grav_acc;
speed = _local2.length;
angle = Math.atan2(_local2.y, _local2.x);
}
if (bool_fric) {
speed = speed * friction;
}
var _local4 = _x + (Math.cos(angle) * speed);
var _local3 = _y + (Math.sin(angle) * speed);
_x = _local4;
_y = _local3;
if (speed > 10) {
_rotation = _rotation + (rotation_speed * (((angle > 1.57) and (angle < 4.61)) ? -1 : 1));
}
}
function stopMoving() {
speed = 0;
angle = 0;
bool_grav = false;
bool_fric = false;
bool_coll = false;
}
function get pos() {
return(new flash.geom.Point(_x, _y));
}
function set pos(p) {
_x = p.x;
_y = p.y;
//return(pos);
}
function get fase() {
return(__fase);
}
function set fase(n) {
__fase = n;
if (n == FASE_STILL) {
stopMoving();
} else if (n == FASE_HIT) {
bool_grav = true;
}
//return(fase);
}
static var FASE_STILL = 0;
static var FASE_HIT = 1;
static var FASE_POP = 9;
}
Symbol 37 MovieClip [__Packages.Vector] Frame 0
class Vector extends flash.geom.Point
{
var x, y;
function Vector (ax, ay) {
super();
x = ax;
y = ay;
}
function clone() {
return(new Vector(x, y));
}
function scale(f) {
return(new Vector(x * f, y * f));
}
function tangent() {
return(new Vector(-y, x));
}
static function dotproduct(v0, v1) {
return((v0.x * v1.x) + (v0.y * v1.y));
}
}