Hey all
I've been banging my head around this since yesterday and I can't really understand what's the problem...
1. I have created on stage three movieclip from the library called Eve, Sta, and Act... If Eve or Act touch Sta they each snap ina specific position relatively to Sta...
2. Let's say that Eve touches Sta and it snaps in place... They will now move as one. This is made possible because I placed them inside an empty MovieCLip on stage called Container... Up to here the code works fine; they snap and move around like they should
3. I want now to snap Act to Sta too, meaning (I guess) that if Act touches the child of Container, called Sta or the child in position 0 (Sta is always in position 0 inside Container), it should snap into position relatively to Container's child Sta....
the snapping code works like this
function snap(e:MouseEvent):void
{
var container:MovieClip = new MovieClip();
container.name = "ao";
if (e.target.name == "eve" && DisplayObjectContainer(e.target).hitTestObject(this.getChildByName("s ta")))
{
DisplayObjectContainer(e.target).x = DisplayObjectContainer(this.getChildByName("sta")).x - 78;
DisplayObjectContainer(e.target).y = DisplayObjectContainer(this.getChildByName("sta")).y;
container.addChild(DisplayObjectContainer(this.getChildByName("sta")) );
container.addChild(DisplayObjectContainer(this.getChildByName("eve")) );
}
if (e.target.name == "act" && DisplayObjectContainer(e.target).hitTestObject(this.getChildByName("s ta")))
{
DisplayObjectContainer(e.target).x = DisplayObjectContainer(this.getChildByName("sta")).x;
DisplayObjectContainer(e.target).y = DisplayObjectContainer(this.getChildByName("sta")).y + 75;
container.addChild(DisplayObjectContainer(this.getChildByName("sta")) );
container.addChild(DisplayObjectContainer(this.getChildByName("act")) );
}
else if (e.target.hitTestObject(container.getChildByName("sta")))
{
trace("ok");
}
container.addEventListener(MouseEvent.MOUSE_DOWN, drag);
container.addEventListener(MouseEvent.MOUSE_UP, noDrag);
container.buttonMode = true;
//feedback
trace(container.x); // returns 0
trace(container.y); // returns 0
trace(container.getChildAt(0)); //returns the name of the obj from the library
trace(container.getChildByName("sta")); //returns the name of the obj from the library
addChild(container);
}
To finish, the problem I'm having is that if I snap something to Sta, any other attempt to attach another mc will fail, so the snapping works fine for the forst time, but not for any other attempt...
by the way I get this message of error once I tried to snap a second object to the container's child
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at GUI_v3_fla::MainTimeline/snap()
Just on a cursory review of the code, the problem (with the error) likely lies in testing...
this.getChildByName("sta")
As soon as you place the sta object inside the container it is no longer a direct child of 'this', but is instead a child of 'container'
There are likely a few ways to deal with this. One would be to always have sta inside the container right from the start, and then hit test against container.getChildByName("sta"). Another way could be to not use the getChildByName approach and instead store references to the actual objects, and then just hit test the objects.
North America
Europe, Middle East and Africa
Asia Pacific