flash as3 The supplied DisplayObject must be a child of the caller when dragging a clip
all I'm trying to do is set up dragging for my movie clips, but I keep getting this error:
The supplied DisplayObject must be a child of the caller.
This is my code:
projectThumb.addEventListener( MouseEvent.MOUSE_DOWN, onThumbPress );
projectThumb.addEventListener( MouseEvent.MOUSE_UP, onThumbRelease );
function onThumbPress( e:MouseEvent ):void
{
setChildIndex(开发者_如何学Go e.target as MovieClip, numChildren - 1 );
e.target.startDrag();
}
function onThumbRelease( e:MouseEvent ):void
{
e.target.stopDrag();
}
The only function you're calling that would throw that error is setChildIndex
. It's likely that e.target as MovieClip
is returning null
.
Make sure that you guarantee that e.target
is always a valid MovieClip
(remember that as
will return null
if the types don't match), or add a check for null
in there.
What Kai said...
just would like to add to it what is likely causing it.
If you click on the thumb (the actual thumb) it is a Bitmap object. so the target is not a MovieClip. Try running it in debug mode to make sure that this is the case. You can also add a trace flash.utils.getQualifiedClassName(e.target)
to see what ti is. If you are converting a non related class (non MovieClip or MovieClip derived Class) then you'll get a null.
you could do an while statement to find the parent that is a projectThumb or just assume that it's the parent. Another way of doing it is adding transparent button that would cover the size of the projectThumb on top of everything so you know what's calling the click event listener
try...
projectThumb.addEventListener( MouseEvent.MOUSE_DOWN, onThumbPress );
projectThumb.addEventListener( MouseEvent.MOUSE_UP, onThumbRelease );
function onThumbPress( e:MouseEvent ):void
{
var target:MovieClip = findPT(e.target as DisplayObject) as MovieClip;
if(target){
setChildIndex(target, numChildren - 1 );
target.startDrag();
}
}
function onThumbRelease( e:MouseEvent ):void
{
var target:MovieClip = findPT(e.target as DisplayObject) as MovieClip;
if(target){
target.stopDrag();
}
}
function findPT($mc:DisplayObject):DisplayObject{
while (1){
if ($mc is ProjectThumb){ // <- this is to check for the right class
return $mc;
}else if($mc.parent == null){
return null
}else{
$mc = $mc.parent as DisplayObject;
}
}
return null
}
精彩评论