Flash AS3 - StartDrag() within StartDrag() on Child MovieClip
hey everyone, my code is listed below.
as you can see, I have a container MC which I have added to the stage. I set its drag constraints using a Rectangle(). I then add the 'cat' child movieclip to the container, and I want this to be dragable too. However, as soon as I click on my cat when testing the MC. It shoots to point x=0 y=0 on the stage and doesn't move.
The container MC can be moved without any trouble.
If I remove the rectangle bounds from the containers startdrag() function. both MC's can be dragged without any issue.
any help would be awesome.
thanks
//panning ability
my_x = 800 - myImage.width;
my_y = 480 - myImage.height;
myWidth = 0 - my_x;
myHeight = 0 - my_y;
container.addEventListener(MouseEvent.MOUSE_DOWN, bgMouseDown);
container.addEventLi开发者_开发知识库stener(MouseEvent.MOUSE_UP, bgMouseUp);
function bgMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}
function bgMouseUp(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.stopDrag();
}
//adding ze cat
cat = new ACat();
container.addChild(cat);
cat.x = 100;
cat.y = 400;
cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown);
cat.addEventListener(MouseEvent.MOUSE_UP, catMouseUp);
function catMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.startDrag(false);
}
function catMouseUp(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.stopDrag();
}
Try evt.stopPropagation( )
on the first line of the catMouseDown function.
Also try cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown, true);
I think you have to test wich is the currentTarget because the event is fired for the container when you drag the Cat mc
try something like that :
function bgMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
if(evt.currentTarget !=container) return;
object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}
Remove your listeners from the container, and make a base class for your dragable objects.
package
{
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
public class Dragable extends Sprite
{
// vars
private var _rect:Rectangle;
/**
* Constructor
*/
public function Dragable()
{
addEventListener(MouseEvent.MOUSE_DOWN, _drag);
addEventListener(MouseEvent.MOUSE_UP, _drop);
}
/**
* MOUSE_DOWN
*/
private function _drag(e:MouseEvent):void
{
if(_rect != null) startDrag(false, _rect);
else startDrag();
}
/**
* MOUSE_UP
*/
private function _drop(e:MouseEvent):void
{
stopDrag();
}
/**
* Define a boundary Rectangle
* @param rect The Rectangle to define
*/
public function set boundaries(rect:Rectangle):void
{
_rect = rect;
}
}
}
There are too many advantages to this to list.
精彩评论