Question

I'm trying to daisychain duplicated movieclips and I can't figure out how to have each one butt-up against each other. I'm building a training aid for railroad instructors and part of this aid is the instructors can build trains dynamically.

There's a total of five objects, locomotive, hopper, flat_car, tank_car, and box_car. When the instructor clicks on one of the cars a duplicate is created on the stage where the instructor can drag and drop that duplicate any where on the stage. If he/she clicks on another car the same thing happens, but if one of the duplicates is dropped on top of another car the train building will begin. The new car will snap to the right of the target and if another is dropped it will snap to the right and so on, but where I run into trouble is when I add the third car it only snaps half way.

The registration point is messing things up for me, it's center, so I need to figure out half width of the target and half width of the car being dropped which will give me the point of which the car will snap to. So for example:

target car is 50 and the car being dropped is 50 then the point to snap to should be 25 + 25. If a third car is dropped the two cars that are now chained together are 100 and the car being dropped is 50, the new drop point should be 75.

The cars are each a different size, which is making this difficult, I wish they were all the same size :( Below is the code I'm using, maybe it will help illustrate what I'm talking about:

var _carArray:Array = ["locomotive","tank_car","box_car","hopper","flat_car"];

/*** 
* the function below is called when a duplicated
* object is dropped on a another duplicated object
* I could add that code, but I figured it wasn't 
* needed since my math is what I'm struggling with
***/

function BuildTrain(_mc,_instanceName):Void
{
    var _tar        = eval(_mc._droptarget);
    var _carHalf    = (_mc._width) / 2;
    var _targetHalf = (_tar._width) / 2;
    var _math       = _targetHalf + _carHalf;
    for(var _i in _carArray)
    {
        if(instanceName == _carArray[_i])
        {
            _tar.attachMovie(_instanceName,_instanceName,200+_count,{_x:_math});
        }
        _p.removeMovieClip();
    }
}

NOTE: I tagged this as math because it does require math to figure this out.

Was it helpful?

Solution

Create another array to hold your current train.

var myTrain = [];

Every time you want to snap a new car into the mix, add it to the myTrain array. Then iterate through the myTrain array and re-position everything in order.

function BuildTrain(_mc,_instanceName):Void
{
    var _tar        = eval(_mc._droptarget);
    var _carHalf    = (_mc._width) / 2;
    var _targetHalf = (_tar._width) / 2;
    var _math       = _targetHalf + _carHalf;
    for(var _i in _carArray)
    {
        if(instanceName == _carArray[_i])
        {
            myTrain.push(_tar.attachMovie(_instanceName,_instanceName,200+_count,{_x:_math})); //put the newly created car in the mytrain array;
            redrawTrain();
        }
        _p.removeMovieClip();
    }
}

function redrawTrain(){
    var curX = 0;
    for(var i = 0;i < myTrain.length; i++){
        myTrain[i]._x = curX + (myTrain[i]._width * .5); //offset by half the car width since it's anchored in the center
        curX += myTrain[i]._width;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top