Welcome Guest [Log In] [Register]
Welcome to Brackenwood. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Attraction; Revised Edition
Topic Started: Mar 22 2006, 12:01 PM (197 Views)
Vector
Member Avatar
Resident Actionscript Guru ♂
Admin
Mucking around with an older project (rewritten from scratch) gave me this...

http://photobucket.com/albums/v661/Vector8...=Attraction.swf

The bonds of attraction between each node and all of the others pulls it in different directions, based on how close they are.

The red line coming from each node represents the speed and direction that the current node is travelling in.

The only trig used in this is pythag, no sin or cos folks!

If you want to make your own one of this, feel free to nab the source code.
What you'll need to do is create a movieclip called "mcTick". Then, go into your library (F11) and right-click on your movieclip. Select "linkage". Tick the first box and click ok. Delete the node that you have off the stage so the only copy exists in the library. Then, paste this code into the actionscript panel (F9) ON THE FRAME:

Code:
 
var aNodes:Array = new Array();
var vRad:Number = 200;
var vMax:Number = Math.sqrt(3) / 2 * vRad;
var vMaxVel:Number = 15;
var vCount:Number = 10;
var vMasterDecay:Number = 0.97;

for (var x = 0; x < vCount; x++) {
var vTemp:MovieClip = _root.attachMovie("mcTick", "iTick" + (x + 1), x + 1);
vTemp._x = Math.random() * Stage.height;
vTemp._y = Math.random() * Stage.width;
vTemp.xvel = Math.random() * 10 - 5;
vTemp.yvel = Math.random() * 10 - 5;
aNodes.push(vTemp);
delete vTemp;
}

function dx(mc1:MovieClip, mc2:MovieClip) : Number {
return mc2._x - mc1._x;
}
function dy(mc1:MovieClip, mc2:MovieClip) : Number {
return mc2._y - mc1._y;
}

function pythag(mc1:MovieClip, mc2:MovieClip) : Number {
var dX:Number = dx(mc1, mc2);
var dY:Number = dy(mc1, mc2);
return Math.sqrt(dX*dX + dY*dY);
delete dX;
delete dY;
}

function line(mc1:MovieClip, mc2:MovieClip) : Void {
_root.moveTo(mc1._x, mc1._y);
_root.lineTo(mc2._x, mc2._y);
}

_root.onEnterFrame = function() {

_root.clear();

/*aNodes[0]._x = _xmouse;
aNodes[0]._y = _ymouse;*/

for (var x = 0; x < aNodes.length; x++) {
 for (var y = x + 1; y < aNodes.length; y++) {
  var vD:Number = pythag(aNodes[x], aNodes[y]);
  if (vD <= vRad) {
   _root.lineStyle(1, 0x000000, ((vRad - vD) / vRad * 100));
   line(aNodes[x], aNodes[y]);
   var vdx:Number = dx(aNodes[x], aNodes[y]);
   var vdy:Number = dy(aNodes[x], aNodes[y]);
   aNodes[x].xvel += (vdx / (vMax * 1)) * Math.pow(vMasterDecay, vD);
   aNodes[x].yvel += (vdy / (vMax * 1)) * Math.pow(vMasterDecay, vD);
   aNodes[y].xvel -= (vdx / (vMax * 1)) * Math.pow(vMasterDecay, vD);
   aNodes[y].yvel -= (vdy / (vMax * 1)) * Math.pow(vMasterDecay, vD);
   delete vdx;
   delete vdy;
  }
  delete vD;
 }
}

_root.lineStyle(2, 0xFF0000, 100);

for (var x = 0; x < aNodes.length; x++) {
 
 /*aNodes[x].xvel *= 0.99;
 aNodes[x].yvel *= 0.99;*/
 
 aNodes[x].xvel = aNodes[x].xvel > vMaxVel ? vMaxVel : aNodes[x].xvel;
 aNodes[x].xvel = aNodes[x].xvel < -vMaxVel ? -vMaxVel : aNodes[x].xvel;
 aNodes[x].yvel = aNodes[x].yvel > vMaxVel ? vMaxVel : aNodes[x].yvel;
 aNodes[x].yvel = aNodes[x].yvel < -vMaxVel ? -vMaxVel : aNodes[x].yvel;

 aNodes[x]._x += aNodes[x].xvel;
 aNodes[x]._y += aNodes[x].yvel;

 _root.moveTo(aNodes[x]._x, aNodes[x]._y);
 _root.lineTo(aNodes[x]._x + aNodes[x].xvel * 5,
    aNodes[x]._y + aNodes[x].yvel * 5);
 
 if (aNodes[x]._x > Stage.width) {
  aNodes[x]._x %= Stage.width;
 } else if (aNodes[x]._x < 0) {
  aNodes[x]._x += Stage.width;
 }
 if (aNodes[x]._y > Stage.height) {
  aNodes[x]._y %= Stage.height;
 } else if (aNodes[x]._y < 0) {
  aNodes[x]._y += Stage.height;
 }  
}

}


There's a few variables up the top that you can play with.

Go NUTS! :bing:
Offline Profile Quote Post Goto Top
 
Mr. Jiggmin
Member Avatar
made of tulips
Neato Vec!

I have a question. (of course) ;) You're manually deleting some of the variables when you're through with them... I thought that when you declared a variable with "var" it automatically got deleted when the function finished? Is there any benefit to doing it the "delete suchAndSuch" way?

*goes to watch the little dots move around some more*

-edit

Ahh Jeez, you know you've been using flash too much when you try to hit ctrl+enter rather than clicking on the link. :yfok:
Offline Profile Quote Post Goto Top
 
Vector
Member Avatar
Resident Actionscript Guru ♂
Admin
Mr. Jiggmin
Mar 22 2006, 03:03 PM
I have a question. (of course) ;)  You're manually deleting some of the variables when you're through with them... I thought that when you declared a variable with "var" it automatically got deleted when the function finished? Is there any benefit to doing it the "delete suchAndSuch" way?

Normally I don't even bother, but with some recent experience with flash I found out that flash doesn't seem to manually delete local variables when they finish (go figure). I'm not sure if this is for everything or just functions or just events or what, but it happens. The app I was working on was meant for long-term running and it crashed overnight, so I deleted the variables after use and it worked fine. So there you go :bing:

<edit>

Attached the fla of a version that you can play with live, and a link to it right here!
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Actionscript · Next Topic »
Add Reply