📄 nodegarden.as
字号:
package {
//import flash.display.Sprite;
import flash.events.Event;
import mx.core.UIComponent;
[SWF(backgroundColor=0x000001)]
public class NodeGarden extends UIComponent
{
private var particles:Array;
private var numParticles:uint = 100;//the number of nodes
private var minDist:Number =200;
private var springAmount:Number = .001;
//for advance() to create boundary
public var vx:Number = 0;
public var vy:Number = 0;
public var ay:Number = .5; //acceleration in y
public var bottom:Number;
public var bounceFactor:Number = 0.7;
//end
private var xBound:Number; // added by leo
private var yBound:Number;
public function NodeGarden(width:Number,height:Number) // can only access stage from mxml, have to pass by value
{
xBound = width; // added by leo
yBound = height; // etc
init();
}
private function init():void
{
//stage.scaleMode = StageScaleMode.NO_SCALE; commented out by leo
//stage.align = StageAlign.TOP_LEFT;
particles = new Array();
for(var i:uint = 0; i < numParticles; i++)
{
var particle:Ball = new Ball(1, 0xffffff);
particle.x = Math.random() * xBound/*stage.stageWidth*/;
particle.y = Math.random() * yBound/*stage.stageHeight*/;
particle.vx = Math.random() * 2 - 1;
particle.vy = Math.random() * 2 - 1;
addChild(particle);
particles.push(particle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
for(var i:uint = 0; i < numParticles; i++)
{
var particle:Ball = particles[i];
particle.x += particle.vx;
particle.y += particle.vy;
var partB:Ball = particles[i];
var partA:Ball = particles[i+1];
///for(i=0; i < numParticles - 1; i++)
///{if(partA.x==partB.x)
//{
//if(partA.y==partB.y)
//{
// partA.vx = 0 - partA.vx;
// partA.vy = 0 - partA.vy;
// partB.vx = 0 - partB.vx;
// partB.vy = 0 - partB.vy;
//}
//}
//}
//Ends here
//Bounce at the edge module
//Right
if(particle.x > xBound)
{
particle.vx = 0- particle.vx;
}
//Left
else if(particle.x < 0)
{
particle.vx = 0- particle.vx;
}
//Down
if(particle.y > yBound) // changed
{
particle.vy = 0- particle.vy;
}
//up
else if(particle.y < 0)
{
particle.vy = 0- particle.vy;
}
}
//Bounce at the edge module ends here
//Spring and bounce to eachother module -- failed
//for(i=0; i < numParticles - 1; i++)
//{
// var partA:Ball = particles[i];
//gives every ball a define. to control every ball
//for(var j:uint = i + 1; j < numParticles; j++)
// {
//var partB:Ball = particles[j];
//spring(partA, partB);
//}
//bounce(partA,partB);
//Spring and bounce to eachother module ends here
}
}
//advance()
//this another failed module, but I use the module above to solve this problem
//this codes ICP(internet copy and paste from mike`s handout vol.i)
//at first I want to give this stage a bottom, when the event bottom happened, means they should brounce
//but they ignore this module, so I change my mind.
//public function advance( ):void {
//vy += ay; //Calculate velocity
//y += vy; //Calculate position
//if(y + height > bottom) {
// y = bottom - height;
// vy *= -1*bounceFactor;
//}
//}
//end
//private function spring(partA:Ball, partB:Ball):void
//{
// var dx:Number = partB.x - partA.x;
// var dy:Number = partB.y - partA.y;
// var dist:Number = Math.sqrt(dx * dx + dy * dy);
// if(dist < minDist)
// {
// var ax:Number = dx * springAmount;
// var ay:Number = dy * springAmount;
// partA.vx += ax;
// partA.vy += ay;
// partB.vx -= ax;
// }
//}
//A module to let them spring, I drop this module, but I respect the others work.
//so I still put them at their first place.
//bounce to eachother module failed try NO.2
//private function bounce(partA:Ball, partB:Ball):void
//{
// if(partA.x==partB.x)
// {
// if(partA.y==partB.y)
// {
// partA.vx = 0 - partA.vx;
// partB.vx = 0 - partB.vx;
// partA.vy = 0 - partA.vy;
// partB.vy = 0 - partB.vy;
//}
// }
//}
//I dont know why they does not work??? mike, why?
//bounce to eachother module failed try NO.2 ends here
// DANGER! DONT USE THIS MODULE! BIO HAZZARD!
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -