Hi i have some movieclips that move to locations that i pull out from an xml document. The xml doc is updated every 20 mins, so my movieclips should then remove every twenty mins.
Now i have ths all working fine, but have been having problems with collission detection and have been posting in the forum but no ones been able to help me.
I think i have finally sussed the collission detection problem by using negative mass to make them repel eachother.
But it only seem to work the first time. When i pull in my xml file again and the movieclips move to their new locations, they dont seem to want to repel eachother anymore??? Is it somthing wrong with my on enter frame function???
My project deadline is a couple of days and this is the last problem for me to solve. SOMEONE PLEASE HELP !!!
var originalYposition: Number = 500;
var currentYposition:Array = ["500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500", "500"];
var duration:Number = 4;
var depth = 0;
var easing:Number = 0.008;
top = 0;
left = 0;
bottom = Stage.height;
right = Stage.width;
function initialisecell() {
for (i=0; i<32; i++) {
var cell:MovieClip = attachMovie("cell"+i, "cell"+i, depth++);
cell._x = Math.round(Math.random()*1100+20);
cell._y = currentYposition*;
cell.vx = 0;
cell.vy = 0;
cell.mass = -1;
}
}
initialisecell();
reloadXML();
var nInterval:Number = setInterval(reloadXML, 5000);
function reloadXML(): Void {
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("stocks.xml");
_global.indicename = new Array();
_global.currentprice = new Array();
_global.valuechange = new Array();
_global.newLocation = new Array();
_global.cell = new Array();
function loadXML(loaded) {
if (loaded) {
for (i=0; i<32; i++) {
_global.indicename.push(this.firstChild.childNodes*.childNodes[0].firstChild.nodeValue);
_global.currentprice.push(this.firstChild.childNodes*.childNodes[1].firstChild.nodeValue);
_global.valuechange.push(this.firstChild.childNodes*.childNodes[2].firstChild.nodeValue);
_global.cell = "cell"+i;
//trace("Symbol = "+_global.indicename* + " Attached to - " +cell);
//trace("Current Price = "+_global.currentprice*);
//trace("Value Change = "+_global.valuechange*);
_global.mySubstring = _global.valuechange*.substr(0, 1);
if (_global.mySubstring == "+") {
_global.origprice = parseFloat(_global.currentprice*)-parseFloat(_global.valuechange*);
//trace("Original Price = "+origprice);
//Following finds percentage change
_global.percentage = parseFloat(_global.valuechange*)/parseFloat(_global.origprice)*100;
//trace("Percentage Change = "+percentage);
_global.positionChange = (_global.percentage*100)/2;
_global.positionChange = Math.ceil(_global.positionChange*100)/100;
//trace("New _Y Position = "+positionChange);
_global.newLocation* = originalYposition-_global.positionChange;
//trace("Move from 500 To ="+newLocation*);
//trace("- - - - - - - - - - - - - - - - - - - ");
}
if (_global.mySubstring == "-") {
_global.valuechange* = _global.valuechange**-1;
_global.origprice = parseFloat(_global.currentprice*)-parseFloat(_global.valuechange*);
//trace("Original Price = "+origprice);
//Following finds percentage change
_global.percentage = parseFloat(_global.valuechange*)/parseFloat(_global.origprice)*100;
//trace("Percentage Change = "+percentage);
_global.positionChange = (_global.percentage*100)/2;
_global.positionChange = Math.ceil(_global.positionChange*100)/100;
//trace("New _Y Position = "+positionChange);
_global.newLocation* = originalYposition+_global.positionChange;
//trace("Move from 500 To ="+newLocation*);
//trace("- - - - - - - - - - - - - - - - - - - ");
}
if (_global.mySubstring == "0") {
_global.origprice = parseFloat(_global.currentprice*)-parseFloat(_global.valuechange*);
//trace("Original Price = "+origprice);
//Following finds percentage change
_global.percentage = parseFloat(_global.valuechange*)/parseFloat(_global.origprice)*100;
//trace("Percentage Change = "+percentage);
_global.positionChange = (_global.percentage*100)/2;
_global.positionChange = Math.ceil(_global.positionChange*100)/100;
//trace("New _Y Position = "+positionChange);
_global.newLocation* = originalYposition+_global.positionChange;
//trace("Move from 500 To ="+newLocation*);
//trace("- - - - - - - - - - - - - - - - - - - ");
}
}
}
movecells();
}
}
function movecells() {
onEnterFrame = function () {
//function onEnterFrame():Void
{
for(var i:Number = 0;i<32;i++)
{
var cell:MovieClip = this["cell" + i];
cell._x += cell.vx;
//take the cells new location and subtract it from its current. Move this distance using easing
cell.vy = (newLocation* - cell._y) * easing;
cell._y += cell.vy;
//if cells come in to contact with stage side, either bounce off or stay their if depth is not accessible.
if (this["cell" + i]._x + this["cell" + i]._width / 2 > right)
{
this["cell" + i]._x = right - this["cell" + i]._width / 2;
cell.vx *= -0.7;
}
else if (this["cell"+i]._x - this["cell"+i]._width / 2 < left)
{
this["cell"+i]._x = left + this["cell"+i]._width / 2;
cell.vx *= -0.7;
}
if (this["cell"+i]._y + this["cell"+i]._height / 2 > bottom)
{
this["cell"+i]._y = bottom - this["cell"+i]._height / 2;
cell.vy *= -0.7;
}
else if (this["cell"+i]._y - this["cell"+i]._height / 2 < top)
{
this["cell"+i]._y = top + this["cell"+i]._height / 2;
cell.vy *= -0.7;
}
}
for(i=0;i<32-1;i++)
{
var partA:MovieClip = this["cell" + i];
for(var j:Number = i+1;j<32;j++)
{
var partB:MovieClip = this["cell" + j];
checkCollision(partA, partB);
gravitate(partA, partB);
}
}
}
function gravitate(partA:MovieClip, partB:MovieClip):Void
{
var dx:Number = partB._x - partA._x;
var dy:Number = partB._y - partA._y;
var distSQ:Number = dx*dx + dy*dy;
var dist:Number = Math.sqrt(distSQ);
var force:Number = partA.mass * partB.mass / distSQ;
var ax:Number = force * dx / dist;
var ay:Number = force * dy / dist;
partA.vx += ax / partA.mass;
partA.vy += ay / partA.mass;
partB.vx -= ax / partB.mass;
partB.vy -= ay / partB.mass;
}
function checkCollision(ball0:MovieClip, ball1:MovieClip):Void
{
var dx:Number = ball1._x - ball0._x;
var dy:Number = ball1._y - ball0._y;
var dist:Number = Math.sqrt(dx*dx + dy*dy);
if(dist < ball0._width / 2 + ball1._width / 2)
{
// calculate angle, sine and cosine
var angle:Number = Math.atan2(dy, dx);
var sine:Number = Math.sin(angle);
var cosine:Number = Math.cos(angle);
// rotate ball0's position
var pos0:Object = {x:0, y:0};
// rotate ball1's position
var pos1:Object = rotate(dx, dy, sine, cosine, true);
// rotate ball0's velocity
var vel0:Object = rotate(ball0.vx, ball0.vy, sine, cosine, true);
// rotate ball1's velocity
var vel1:Object = rotate(ball1.vx, ball1.vy, sine, cosine, true);
// collision reaction
var vxTotal:Number = vel0.x - vel1.x;
vel0.x = ((ball0.mass - ball1.mass) * vel0.x + 2 * ball1.mass * vel1.x) / (ball0.mass + ball1.mass);
vel1.x = vxTotal + vel0.x;
// update position
var absV:Number = Math.abs(vel0.x) + Math.abs(vel1.x);
var overlap:Number = (ball0._width / 2 + ball1._width / 2) - Math.abs(pos0.x - pos1.x);
pos0.x += vel0.x / absV * overlap;
pos1.x += vel1.x / absV * overlap;
// rotate positions back
var pos0F:Object = rotate(pos0.x, pos0.y, sine, cosine, false);
var pos1F:Object = rotate(pos1.x, pos1.y, sine, cosine, false);
// adjust positions to actual screen positions
ball1._x = ball0._x + pos1F.x;
ball1._y = ball0._y + pos1F.y;
ball0._x = ball0._x + pos0F.x;
ball0._y = ball0._y + pos0F.y;
// rotate velocities back
var vel0F:Object = rotate(vel0.x, vel0.y, sine, cosine, false);
var vel1F:Object = rotate(vel1.x, vel1.y, sine, cosine, false);
ball0.vx = vel0F.x;
ball0.vy = vel0F.y;
ball1.vx = vel1F.x;
ball1.vy = vel1F.y;
}
}
function rotate(x:Number, y:Number, sine:Number, cosine:Number, reverse:Boolean):Object
{
var result:Object = new Object();
if(reverse)
{
result.x = x * cosine + y * sine;
result.y = y * cosine - x * sine;
}
else
{
result.x = x * cosine - y * sine;
result.y = y * cosine + x * sine;
}
return result;
}
if (currentYposition* == newLocation*){
for (i=0; i<32; i++) {
currentYposition* = newLocation*;
}
}
}
}