Okay, so I’m working on a solar system simulation in which objects rotate around each other. I have all the objects in an array, because I figure it’s easier to control them all that way. So everything is going fine until I get to the collision scripting. I tried to make it so that when an object hits another, the larger one gains the mass of the smaller one and the smaller one gets deleted. But now, when an object gets deleted, all the other objects stop moving… and never move afterwards. I can’t, for the life of me, figure out what is causing this. Am I doing something wrong?
Here is the fla code:
var Bodies:Array = new Array();
var depth:Number = 0;
init();
function init():Void {
Bodies.push(attachMovie(“body_mc”, “sun”, depth++));
sun.init(Stage.width/2, Stage.height/2, 0, 0, 30000, true);
}
function onMouseDown():Void {
if (Key.isDown(Key.SHIFT)) {
var planet:MovieClip = attachMovie(“body_mc”, “p”+Bodies.length, depth++);
planet.init(_xmouse, _ymouse, 0, 0, 10, false);
Bodies.push(planet);
planet.dragging = true;
planet.startDrag();
//trace(planet._name);
}
}
function onMouseUp():Void {
var i:Number = Bodies.length;
Bodies*.dragging = false;
Bodies*.stopDrag;
}
function onEnterFrame():Void {
for (var i:Number = 0; i<Bodies.length; i++) {
//this doesnt work… Im not sure why:
if (Bodies* == null) {
Bodies.splice(i, 1);
trace(“Bodies [”+i+“] was removed”);
}
var body:MovieClip = Bodies*;
for (var j:Number = i+1; j<Bodies.length; j++) {
var partB:MovieClip = Bodies[j];
body.gravitate(partB);
body.checkCollision(partB);
}
}
}
and this is the collision code in the class file for the objects:
function checkCollision(other:MovieClip):Void {
var dx:Number = other._x-_x;
var dy:Number = other._y-_y;
var dist:Number = Math.sqrt(dxdx+dydy);
var minDist = _width/2+other._width/2;
if (dist<minDist) {
if (mass>other.mass) {
//trace(mass);
mass += other.mass;
other.removeMovieClip(this);
} else {
other.mass += mass;
this.removeMovieClip(this);
}
}
}
And here it is in it’s glitch-y glory:
http://www.slydevilstudios.com/flash/solarsystem_test.swf
Thanks for any and all help. I’m pretty new at this, so I make dumb mistakes
-Sly