PDA

View Full Version : addChild issue / dragging objects



magnitudian
September 14th, 2008, 08:21 PM
This is my first post. I have tried searching for a solution but have had no success. I have an object in my library that I am randomly adding to the stage and giving it the ability to be dragged. I have created a second sprite that I add to the randomly added movieclip. When I attempt to drag the movieclip with the new sprite it gives me this error:


ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/setChildIndex()
at randomPictures_fla::MainTimeline/swap()
at randomPictures_fla::MainTimeline/drag()

Here is the code:

var Picture:MovieClip;
var offSetX:Number;
var offSetY:Number;
var holder:Sprite = new Sprite();

holder.graphics.beginFill(0x000000);
holder.graphics.drawRect(0, 0, 258, 215);


for(var j:uint = 0; j < 3; j++) {
Picture = new picture();
holder = new imageHolder();
Picture.x = Math.floor(5 + Math.random()* stage.stageWidth/2);
Picture.y = Math.floor(5 + Math.random()* stage.stageHeight/2);
Picture.rotation = Math.floor(-10 + Math.random()* 20);
Picture.scaleX = .5;
Picture.scaleY = .5;
addChild(Picture);
Picture.addChild(holder);




}

stage.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

function drag (event:MouseEvent):void {
//this line makes each one dragable
Picture = event.target as MovieClip;
swap(event.target);
offSetX = event.stageX - Picture.x;
offSetY = event.stageY - Picture.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragPicture);

}

function stopDragging (event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragPicture);
}

function dragPicture (event:MouseEvent):void {
Picture.x = event.stageX - offSetX;
Picture.y = event.stageY - offSetY;

//Instruct the flash player to update after this event
event.updateAfterEvent();
}

function swap(Picture:*):void
{
var highestDepth:uint = numChildren - 1;
setChildIndex(Picture, highestDepth);

}

ViktorHesselbom
September 15th, 2008, 09:58 AM
You're better off using individual mouseevents and use the event's currentTarget property instead.
So something like this (excluding unchanged code):


stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

for(var j:uint = 0; j < 3; j++)
{
...
Picture.addEventListener(MouseEvent.MOUSE_DOWN, drag);
...
}
function drag (event:MouseEvent):void
{
...
Picture = event.currentTarget as MovieClip;
swap(event.currentTarget);
...
}

magnitudian
September 15th, 2008, 09:15 PM
Thank you for the response. That makes sense, and I will try this.

magnitudian
September 15th, 2008, 10:19 PM
Unfortunately I still get the error. Also, by making Picture the listener isntead of the stage, it has made the dragging very sluggish and almost unresponsive at times. It only gives me the error when I click on the child inside the picture. If I am to only click on the Picture child it doesnt give me the error. I am very confused. :crying: