hello all!
I’m trying to make this system that creates circles with sound, and once on stage based its size, it decides either to orbit, or be orbited around, I’m having real trouble with making and setting the orbits on the movieclips.
The function findFriend() evaluates the xy distance between two movieclips in the array, for some reason it is not calculating the distance between these clips, it keeps tracing a zero!
Here’s the code if you can check it out:
// indexes
var i:Number = 0;
// endpoint of radius
// centerpoint for radius
var x1:Number ;
var y1:Number;
// centerpoint for radius
// Array for Circle movieClips
var c:Array = [];
// collision variables
var spring:Number = .05;
var gravity:Number = .3;
// vars for Stage Boundaries
var left:Number = 0;
var top:Number = 0;
var right:Number = Stage.width;
var bottom:Number = Stage.height;
// Stage Listener
Stage.addListener(this);
onResize = function () {
right = Stage.width;
bottom = Stage.height;
};
// *------------------- GET MIC INPUT: MICROPHONE OBJECT ------------------*//
mic = Microphone.get();
attachAudio(mic);
mic.setUseEchoSuppression(false);
// var micVol:Number = mic.activityLevel;
// *------------------- ENTER FRAME, CREATE CIRCLES, SET ROTATION ---------------*//
function onEnterFrame(Void):Void
{
if (mic.activityLevel>5.68)
{
createCircle();
findFriend();
}
}
//**--------- FUNCTION TO CREATE CIRCLES --------------**//
createCircle = function (){
var mc=this.createEmptyMovieClip("c"+i, i);
// each time a circle is created push into an array
c.push("c"+i);
var x1:Number = Math.random()*Stage.width;
var y1:Number = Math.random()*Stage.height;
// set radius according to activity level
r = mic.activityLevel;
// DRAW CIRCLE USING DRAWING API
var A:Number = Math.tan(22.5*Math.PI/180);
var endx:Number;
var endy:Number;
var cx:Number;
var cy:Number;
this[c*].lineStyle(1, 0x000000, 100);
this[c*].beginFill(0x000000, 100);
this[c*].moveTo(x1+r, y1);
for (var angle = 45; angle<=360; angle += 45) {
endx = r*Math.cos(angle*Math.PI/180);
endy = r*Math.sin(angle*Math.PI/180);
cx = endx+r*A*Math.cos((angle-90)*Math.PI/180);
cy = endy+r*A*Math.sin((angle-90)*Math.PI/180);
this[c*].curveTo(cx+x1, cy+y1, endx+x1, endy+y1);
}
this[c*].endFill();
i++;
//create the onEnterFrame function for the movieclip
this[c*].onEnterFrame = function(){
//trace(this.centerMC);
if (this[c*].centerMC){
//do orbiting animation...
var centerx = this[c*].centerMC._x;
var centery = this[c*].centerMC._y;
//increment your angle
trace(this.angle + " mc " + this._name);
trace("radius..." + this.orbitingRadius);
//set the x and y of the movieclip based on sine and cosine
var newx = this[c*].orbitingRadius * Math.sin( this[c*].angle*Math.PI/180.0 ) + centerx;
var newy = this[c*].orbitingRadius * Math.cos( this[c*].angle*Math.PI/180.0 ) + centery;
trace("newx,y " + newx + " " +newy);
this[c*].angle += 10;
this[c*]._x = newx;
this[c*]._y = newy;
//trace (this._name + " orbiting around ... " + this.centerMC._name);
} else {
//trace(this._name + " i'm not orbiting...");
}
}
// trace(i);
};
//**--------- FUNCTION TO FIND NEAREST MOVIECLIP --------------**//
findFriend = function(){
var mcA:MovieClip = this[c[c.length - 1]];
// var to compare to
var closestDistanceSoFar:Number = 999;
if(c.length == 1){
return;
}
else
{
for (var j=0; j < c.length-1; j++)
{
//calculate distance to neighbor
var dx:Number = mcA._x - this[c[j]]._x;
var dy:Number = mcA._y - this[c[j]]._y;
var distance:Number = Math.sqrt(dx*dx + dy*dy);
trace("this[c["+j+"]]"+ this[c[j]]._name);
trace("distance = "+ distance);
//compare to see if it's the closer than what I've found already
if( distance < closestDistanceSoFar){
//if it is, set mcB to the new neighbor
var mcB:MovieClip = this[c[j]]; // this is our closest neighbor right now
closestDistanceSoFar = distance; //now THIS is the closest distance...
trace("closestDistanceSoFar ="+closestDistanceSoFar);
//it will be used the next time we compare
}
}
var sizeofA:Number = mcA._height;
var sizeofB:Number = mcB._height;
// trace( "sizeA ="+ sizeA + " sizeB = " +sizeB);
if (sizeofA < sizeofB){
// trace("sizeB is greater");
//CREATE A property on mcA's timeline to save the MC that it orbits around
mcA.centerMC = mcB;
mcA.angle = 0;
mcA.orbitingRadius = closestDistanceSoFar;
} else {
// trace("sizeA is greater");
mcB.centerMC = mcA;
mcB.angle = 0;
mcB.orbitingRadius = closestDistanceSoFar;
}
}
}
I will be oh so thankful with someone helping me with this!