I will try my best at explaining my problem, maybe my approach is not even the way to do this, but here it is:
I've been tasked with rebuilding a training aid that will allow railroad instructors to use a fake railroad and build scenarios based on the discussion. The instructor starts out with a rail yard that only has tracks, NO CARS or LOCOMOTIVES. Here's a screenshot:
The instructor will have several objects below that map that he/she can drag and drop onto the rail yard to illustrate scenarios, here's a screenshot:
The objects are in my library and a single object is placed on the stage when the swf first runs. I have made an object array that has the linked name, x, and y coordinates of where I want the object to be placed when it's attached. Here is the object and loop to put item on the stage:
There are a total of about 50 items, but this example will only show two.
Object Array
var _objectArray = [{obj:{name:"detector",x:20,y:680}},{obj:{name:"locomotive",x:270,y:610}}];
Attach Movie Loop
/* pulls objects from library and places on the stage */
for(var _i in _objectArray)
{
var _name = String(_objectArray[_i].obj.name);
var _xx = _objectArray[_i].obj.x;
var _yy = _objectArray[_i].obj.y;
this.attachMovie(_name,_name,this.getNextHighestDepth(),{_x:_xx,_y:_yy});
this[_name].onRelease = function()
{
Clone(this);
}
}
When the instructor starts building the scenario he/she will click on the object and it will place a duplicate of that object on the stage outside the object area. Then he/she can drag and drop that duplicate anywhere on the stage. In the for loop you can see I call the Clone()
function, here that is:
Clone Function
var _number:Number = 0;
var _cloneArray:Array = [];
function Clone(_mc):Void
{
var _clone = String("clone" + _number);//used to make unique clone names
_cloneArray.push(_clone);//build array so I can remove objects from stage
/* duplicate objects */
_mc.duplicateMovieClip(_clone,100+_number,{y:580,_x:Stage.width/2});
/* set press event */
this[_clone].onPress = function()
{
this.swapDepths(10000);//bring to the top
this.startDrag(false,0,0,1024,580);//drag to constraints
}
/* set release event */
this[_clone].onRelease = function()
{
this.stopDrag();
if(_mc._name == "loco" || _mc._name == "car")
{
BuildTrain(this);
}
}
}
All this works great, but where I run into problems is when the instructor wants to "build" a train. So what I mean by that is, the instructor will place a locomotive on the stage, then place a tank car on the stage, the instructor will drag the tank car and drop it on top of the locomotive and the tank car will snap behind it the locomotive inline. When that happens the tank car will now follow the locomotive if the instructor wants to drag it somewhere. So it's like there are now one unit. This instructor could want 20 cars behind the locomotive ... and he might want to build more than one train. Here is a screenshot of a locomotive and car "snapped" together:
Here's some code that allows me to snap the cars together, which works fine, but my problem is I can't drag the objects around as one unit after all the cars have been snapped:
BuildTrain
function BuildTrain():Void
{
for(var _i in _consistArray)
{
var _value = _consistArray[_i];
var _math = _mc.width + this[_value]._width;
var _space = 1; // this is the space between cars
if(eval(_mc._droptarget)._name === _value)
{
_mc._x = this[_value]._x + _math / 2 + _space;
_mc._y = this[_value]._y;
}
}
}
So Clone()
does snap one car to the locomotive and car to car if you want to build a long train. Where the problem lies is I cannot figure out how to make them one unit after they are snapped. I thought about maybe building an array using a parent child relationship, but I'n not sure.
I know this is a long post, but some background hopefully helped.
UPDATE
I'm playing around with the idea that Amy had below since it seems logical that a child that resides inside a parent would just follow the parent. I managed to get one child - which is not enough, I need to be able to attach much much more - but I think this is the right path. I want to attach a car to the last car attached? Here's the code I'm working with:
function BuildTrain(_mc):Void
{
for(var _i in _consistArray)
{
var _value = _consistArray[_i];
var _math = _mc._width + this[_value]._width;
var _space = 2; // gap between cars
if(eval(_mc._droptarget)._name == _value)
{
this[_value].attachMovie("tank_car","consist"+_i,this.getNextHighestDepth(),{_x:_math / 2 + _space});
}
}
}