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);
}
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);
}