GlobalToLocal – příklad

Drag and Dropp + addChild

import flash.display.MovieClip;
import flash.geom.Point;

karta1_mc.addEventListener(MouseEvent.MOUSE_DOWN, chyt);
karta2_mc.addEventListener(MouseEvent.MOUSE_DOWN, chyt);
karta3_mc.addEventListener(MouseEvent.MOUSE_DOWN, chyt);
karta4_mc.addEventListener(MouseEvent.MOUSE_DOWN, chyt);
desk_mc.addEventListener(MouseEvent.MOUSE_DOWN, chyt);

function chyt(e:MouseEvent):void
{
	//trace(e.currentTarget.hitTestObject(desk_mc));
	//trace(desk_mc.hitTestObject(e.currentTarget as MovieClip));

	if (e.target.parent==desk_mc)
	{	
		desk_mc.addChild(e.target as MovieClip);

		var globalniSouradnice:Point = new Point();
		globalniSouradnice = desk_mc.localToGlobal(new Point(e.target.x,e.target.y));

		e.target.x = globalniSouradnice.x;
		e.target.y = globalniSouradnice.y;
	}
	e.target.startDrag();
	addChild(e.target as MovieClip);
}

karta1_mc.addEventListener(MouseEvent.MOUSE_UP, Pust);
karta2_mc.addEventListener(MouseEvent.MOUSE_UP, Pust);
karta3_mc.addEventListener(MouseEvent.MOUSE_UP, Pust);
karta4_mc.addEventListener(MouseEvent.MOUSE_UP, Pust);
desk_mc.addEventListener(MouseEvent.MOUSE_UP, Pust);

function Pust(e:MouseEvent):void
{
	e.target.stopDrag();
	if (e.target.hitTestObject(desk_mc) && e.target != desk_mc)
	{
		desk_mc.addChild(e.target as MovieClip);
		
		var lokalniSouradnice:Point = new Point();
		lokalniSouradnice = desk_mc.globalToLocal(new Point(e.target.x,e.target.y));
		
		e.target.x = lokalniSouradnice.x;
		e.target.y = lokalniSouradnice.y;
	}
}

 


Posted

in

by

Tags: