PDA

View Full Version : Draggable MC, Class & Casting



lonerhino
July 24th, 2007, 02:42 PM
Let’s say I wish to drag a panel but only from a strip at the top. I put a MovieClip named mcBar inside a MovieClip named mcPanel.
When I code frame one in the timeline it works.


mcPanel.mcBar.addEventListener(MouseEvent.MOUSE_DO WN, downHandle);
mcPanel.mcBar.addEventListener(MouseEvent.MOUSE_UP , upHandle);

function downHandle(evtObj:MouseEvent):void{

this.startDrag();

}

function upHandle(evtObj:MouseEvent):void{

this.stopDrag();

}



What’s confusing is that it works. Why is the whole thing dragging? I’m assuming it’s event propagation? mcBar’s mouse down is “bubbling” to mcPanel… Even though this is exactly what I want – Don’t completely understand it.


Now, further complication is when I move the code to a class.


package{

//imports
import flash.display.MovieClip;
import flash.events.MouseEvent;


public class DraggableClip extends MovieClip{

//constructor
public function DraggableClip(){

this.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
this.addEventListener(MouseEvent.MOUSE_UP, upHandler);
}

//methods
function downHandle(evtObj:MouseEvent):void{

parent.startDrag();
}

function upHandle(evtObj:MouseEvent):void{

parent.stopDrag();

}
}
}


Now, if I assign mcBar via Linkage to DraggableClip.as, only the bar drags. This is the behavior I would have expected when I initially coded the timeline. However, I really do want the entire panel to drag so how do I target it?

As the target for the drag methods:


this refers to the instance of the class(mcBar only) , parent throws a 1061: Call to a possibly undefined method startDrag through a reference with static type flash.display:DisplayObjectContainer. error.

I found a drag MC sample from Jen deHaan that had: var thisMC:MovieClip = event.currentTarget as MovieClip;

I barely understand that this is a Casting Operation...Is this what I'm missing?

In Summary:

What do I need to do to get mcBar inside mcPanel to drag both mcBar and mcPanel from code in a class?
Also if my issue is with casting, why?
In my deHaan sample, why legacy as operator vs type(expression)?
Thanks for any/all insight.

CarlLooper
July 24th, 2007, 03:01 PM
Two ways of casting - one easier to read, the other easier to write

mc = disobj as MovieClip;
mc.gotoAndPlay(10);
mc = mc.parent as MovieClip
mc.gotoAndPlay(10);

or

MovieClip(disobj).gotoAndPlay(10);
MovieClip(disobj.parent).gotoAndPlay(10);

The cast is necessary as parent returns a reference to a DisplayObjectContainer, rather than a MovieClip.

Carl

lonerhino
July 24th, 2007, 04:06 PM
Thanks for the reply CarlLooper,

I tried var mcParent:MovieClip = this.parent as MovieClip; and it works. I'm pleased and I pseudo-understand it.

What would the code be to Cast once or is it a reasonable solution as is? I have the line above in both the downHandler and upHandler methods.

If I move it to the constructor, then it's a scope issue. Methods do not understand mcParent reference.

Thanks

CarlLooper
July 24th, 2007, 04:53 PM
If I move it to the constructor, then it's a scope issue. Methods do not understand mcParent reference.


It's not a scope issue as such. The parent property of an object refers (of course) to it's parent but the object doesn't actually have a parent until it's been added to the display list. And it can't be added to the display list until it has been constructed - so any references to a parent - in a constructor - will always be invalid. A "catch 22" as they say.

Carl

lonerhino
July 24th, 2007, 05:06 PM
Thanks Carl,

I totally understand - you rock like "Dokken"!

Appreciate all the help.