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元件时候,能实现拖拽

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值