Flash拖拽元件的元件+元件的元件隨鼠標移動:目的讓元件的元件隨着鼠標移動

目錄

一、目的:

1、項目中需要元件裏面的元件隨着鼠標移動,下面是倆種方法,一個是拖拽法,一個數幀監聽中,將鼠標座標賦值給此元件

一、使用幀監聽賦值座標法:元件的元件隨鼠標移動

1、代碼

1、舞臺佈置test_child_mc再test_parent_mc裏面

1、效果:test_child_mc隨着鼠標移動

一、拖拽元件的元件:startDrag、stopDrag

1、代碼

1、舞臺佈置test_child_mc再test_parent_mc裏面

1、效果:拖拽 test_child_mc元件時候,能實現拖拽


一、目的:

1、項目中需要元件裏面的元件隨着鼠標移動,下面是倆種方法,一個是拖拽法,一個數幀監聽中,將鼠標座標賦值給此元件

一、使用幀監聽賦值座標法:元件的元件隨鼠標移動

1、代碼

import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;

stop();

Start();

//功能:初始化
function Start()
{
	if (! stage.hasEventListener(Event.ENTER_FRAME))
	{
		stage.addEventListener(Event.ENTER_FRAME,ENTER_FRAME_stage);
	}
}

//功能:離開此幀,刪除的所有東西;
function Destroy()
{
	if (stage.hasEventListener(Event.ENTER_FRAME))
	{
		stage.removeEventListener(Event.ENTER_FRAME,ENTER_FRAME_stage);
	}
}

//功能:幀監聽
function ENTER_FRAME_stage(e:Event)
{	
	//因爲舞臺上面找不到test_child_mc,只能通過test_parent_mc.test_child_mc找到test_child_mc元件,所以test_child_mc的座標都是通過test_parent_mc來確定的
	trace("test_parent_mc.test_child_mc.x:"+test_parent_mc.test_child_mc.x+",test_parent_mc.test_child_mc.y:"+test_parent_mc.test_child_mc.y);
	var point_mouse:Point = new Point(mouseX,mouseY);
	//得到鼠標在test_parent_mc上面的座標
	point_mouse = test_parent_mc.globalToLocal(point_mouse);
	trace("point_mouse:"+point_mouse);
	//此時鼠標在test_parent_mc座標賦值給test_child_mc在test_parent_mc上面的座標,此時倆個都是在test_parent_mc上面的座標了,可以互相賦值了
	test_parent_mc.test_child_mc.x = point_mouse.x;
	test_parent_mc.test_child_mc.y = point_mouse.y;
}

1、舞臺佈置test_child_mc再test_parent_mc裏面

1、效果:test_child_mc隨着鼠標移動

一、拖拽元件的元件:startDrag、stopDrag

1、代碼

/***********************************/
//作者:xzy
//日期:20200334
//功能:拖拽物體
//版本:1.0
/***********************************/
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;

stop();

Start();

//功能:初始化
function Start()
{
	if (! test_parent_mc.test_child_mc.hasEventListener(MouseEvent.MOUSE_DOWN))
	{
		test_parent_mc.test_child_mc.addEventListener(MouseEvent.MOUSE_DOWN,MOUSE_DOWN);
	}

	if (! test_parent_mc.test_child_mc.hasEventListener(MouseEvent.MOUSE_UP))
	{
		test_parent_mc.test_child_mc.addEventListener(MouseEvent.MOUSE_UP,MOUSE_UP);
	}
}

//功能:離開此幀,刪除的所有東西;
function Destroy()
{
}

function MOUSE_DOWN(e:MouseEvent):void
{
	test_parent_mc.test_child_mc.startDrag();
}

function MOUSE_UP(e:MouseEvent):void
{
	test_parent_mc.test_child_mc.stopDrag();
}

1、舞臺佈置test_child_mc再test_parent_mc裏面

1、效果:拖拽 test_child_mc元件時候,能實現拖拽

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章