Actionscript 3 鼠标离开不能使movieclip弹回来
我正在尝试在flash中制作一个小的拖放应用程序,我已经能够实现以下目标- 1) 拖动电影剪辑 2) 确保两个电影剪辑在相互重叠时不会互换位置 3) 确保被拖动的MC保持在其他电影剪辑的顶部 4) 使电影剪辑在指定位置下落时保持静止 然而,有一件非常重要的事情我无法实现,当光标移出舞台时,电影剪辑会粘在光标上,我希望用户离开舞台时,被拖动的剪辑会回到其原始位置 我已经尝试使用鼠标离开选项,但它不工作 我正在添加拖放代码,如下所示,请在此处指导我- 拖码- 数组以保存目标实例、放置实例、, 以及放置实例的开始位置Actionscript 3 鼠标离开不能使movieclip弹回来,actionscript-3,flash,Actionscript 3,Flash,我正在尝试在flash中制作一个小的拖放应用程序,我已经能够实现以下目标- 1) 拖动电影剪辑 2) 确保两个电影剪辑在相互重叠时不会互换位置 3) 确保被拖动的MC保持在其他电影剪辑的顶部 4) 使电影剪辑在指定位置下落时保持静止 然而,有一件非常重要的事情我无法实现,当光标移出舞台时,电影剪辑会粘在光标上,我希望用户离开舞台时,被拖动的剪辑会回到其原始位置 我已经尝试使用鼠标离开选项,但它不工作 我正在添加拖放代码,如下所示,请在此处指导我- 拖码- 数组以保存目标实例、放置实例、, 以及放
var hitArray:Array = new Array(hitTarget1,hitTarget2,hitTarget3);
var dropArray:Array = new Array(drop1,drop2,drop3);
var positionsArray:Array = new Array();
这会将鼠标向下和向上侦听器添加到drop实例
并添加放置实例的起始x和y位置
进入阵列
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);
positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}
这是投递代码
这将在鼠标移动时停止拖动选定对象
释放。如果将对象放置在相应的目标上
然后将其设置为目标的x和y位置。否则
它返回到原始位置
function mUp(e:MouseEvent):void {
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
target.stopDrag();
if (target.hitTestObject(hitArray[dropIndex])) {
target.x = hitArray[dropIndex].x;
target.y = hitArray[dropIndex].y;
}else{
target.x = positionsArray[dropIndex].xPos;
target.y = positionsArray[dropIndex].yPos;
}
}
请告诉我如何使用鼠标离开这里,并使回扣,
在拖放部分使用它,如下所示
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave);
但总是会出现一些错误,比如stage不支持属性x等。我添加了一些代码,比如将x和y相等,但它不起作用。。请引导
Jin
鼠标离开
显示我们是否在舞台上,但不检测鼠标的位置。您必须在鼠标按下时收集鼠标数据,以将实例限制在限制范围内
const LL:uint = 0;
const LT:uint = 0;
const LR:uint = stage.stageWidth;
const LB:uint = stage.stageHeight;
function EnterFrame(e:Event):void {
e.target.x = mouseX;
e.target.y = mouseY;
if (mouseX < LL) {e.target.x = LL;} else if (mouseX > LR) {e.target.x = LR;}
if (mouseY < LT) {e.target.y = LT;} else if (mouseY > LB) {e.target.y = LB;}
}
在函数“mUp”中:
target.removeEventListener(Event.ENTER_FRAME, EnterFrame);
一般方法
这是可以做到的方法。鼠标悬停在限制之外时,目标为[目标舞台]。因此,您必须创建一个变量\uu last
,该变量将使您能够识别鼠标单击的实例(p1或其他实例)。不需要StartDrag():
const LL:uint = 0;
const LT:uint = 0;
const LR:uint = stage.stageWidth;
const LB:uint = stage.stageHeight;
function EnterFrame(e:Event):void {
e.target.x = mouseX;
e.target.y = mouseY;
if (mouseX < LL) {e.target.x = LL;} else if (mouseX > LR) {e.target.x = LR;}
if (mouseY < LT) {e.target.y = LT;} else if (mouseY > LB) {e.target.y = LB;}
}
var __last:*;
p1.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
function OnMouseDown(e:MouseEvent):void {
__last = MovieClip(e.target);
__last.addEventListener(Event.ENTER_FRAME, EnterFrame);
}
this.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
function OnMouseUp(e:MouseEvent):void {
if(__last) __last.removeEventListener(Event.ENTER_FRAME, EnterFrame);
}
const LL:uint=0;
常数LT:uint=0;
const LR:uint=stage.stageWidth;
const LB:uint=stage.stageHeight;
函数EnterFrame(e:事件):无效{
e、 target.x=鼠标;
e、 target.y=mouseY;
如果(mouseXLR){e.target.x=LR;}
if(mouseYLB){e.target.y=LB;}
}
var _uLast:*;
p1.addEventListener(MouseEvent.MOUSE_DOWN、OnMouseDown);
函数OnMouseDown(e:MouseEvent):void{
__最后=电影剪辑(如目标);
__last.addEventListener(Event.ENTER_FRAME,EnterFrame);
}
this.stage.addEventListener(MouseEvent.MOUSE\u UP,OnMouseUp);
mouseup上的函数(e:MouseEvent):void{
如果(_last)_last.removeEventListener(Event.ENTER_FRAME,EnterFrame);
}
将其捕捉回原始位置
const LL:uint = 0;
const LT:uint = 0;
const LR:uint = stage.stageWidth;
const LB:uint = stage.stageHeight;
var __last:*;
var dropIndex:int;
function EnterFrame(e:Event):void {
if (mouseX < LL || mouseX > LR || mouseY < LT || mouseY > LB) {
__last.x = positionsArray[dropIndex].xPos;
__last.y = positionsArray[dropIndex].yPos;
__last.stopDrag();
}
}
var hitArray:Array = new Array(hitTarget1, hitTarget2, hitTarget3);
var dropArray:Array = new Array(drop1, drop2, drop3);
var positionsArray:Array = [];
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);
positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}
function mdown(e:MouseEvent):void {
__last = e.currentTarget;
dropIndex = dropArray.indexOf(__last);
setChildIndex(MovieClip(__last), numChildren - 1);
__last.startDrag();
addEventListener(Event.ENTER_FRAME, EnterFrame);
}
function mUp(e:MouseEvent):void {
if (__last.hitTestObject(hitArray[dropIndex])) {
__last.x = hitArray[dropIndex].x;
__last.y = hitArray[dropIndex].y;
} else {
__last.x = positionsArray[dropIndex].xPos;
__last.y = positionsArray[dropIndex].yPos;
}
__last.stopDrag();
removeEventListener(Event.ENTER_FRAME, EnterFrame);
}
const LL:uint=0;
常数LT:uint=0;
const LR:uint=stage.stageWidth;
const LB:uint=stage.stageHeight;
var _uLast:*;
var-dropIndex:int;
函数EnterFrame(e:事件):无效{
如果(鼠标XLR | |鼠标YLB){
__last.x=positionsArray[dropIndex].xPos;
__last.y=positionsArray[dropIndex].yPos;
__last.stopDrag();
}
}
var HITARGET:Array=新阵列(HITARGET1、HITARGET2、HITARGET3);
var dropArray:Array=新数组(drop1、drop2、drop3);
变量位置数组:数组=[];
for(变量i:int=0;i
尝试使用stage.addEventListener(MouseEvent.MOUSE\u OUT)代替。Event.MOUSE_LEAVE不存在。可能是@SlyRaskal的副本-谢谢,我之前尝试过该代码,它确实可以检测移动何时离开舞台,但由于某些原因,它不会将MC捕捉回原始状态,也会将MC连接到鼠标上,我尝试使用原始x,y坐标,但它不起作用…@DodgerThud谢谢,我也会尝试鼠标,但似乎鼠标离开确实存在…-谢谢,我尝试了第一种方法它不起作用,不幸的是,它可以检测出鼠标,但不能将其弹回到原始位置…@multiverse:在项目开始移动时保存位置,并将其像“_originalPosition=mc@DanielMesSer-使用-startXZ=event.target.x;startYZ=event.target.y;stage.addEventListener”那样存储(Event.MOUSE_-LEAVE,mouseLeaveA);函数mouseLeaveA(e:Event):void{Event.target.stopDrag();Event.target.x=startXZ;Event.target.y=startYZ;stage.removeEventListener(Event.MOUSE_-LEAVE,mouseLeaveA);}但是它把所有的位置都弄乱了,就像它离开舞台时把它们弹回来一样,但是当所有的物体离开舞台时,它们只是重叠在一起……你能不能guide@helloflash-非常感谢,我会尽力回复你,再次感谢你在这里给我时间和耐心…@helloflash-非常感谢,这太完美了这个
const LL:uint = 0;
const LT:uint = 0;
const LR:uint = stage.stageWidth;
const LB:uint = stage.stageHeight;
function EnterFrame(e:Event):void {
e.target.x = mouseX;
e.target.y = mouseY;
if (mouseX < LL) {e.target.x = LL;} else if (mouseX > LR) {e.target.x = LR;}
if (mouseY < LT) {e.target.y = LT;} else if (mouseY > LB) {e.target.y = LB;}
}
var __last:*;
p1.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
function OnMouseDown(e:MouseEvent):void {
__last = MovieClip(e.target);
__last.addEventListener(Event.ENTER_FRAME, EnterFrame);
}
this.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
function OnMouseUp(e:MouseEvent):void {
if(__last) __last.removeEventListener(Event.ENTER_FRAME, EnterFrame);
}
const LL:uint = 0;
const LT:uint = 0;
const LR:uint = stage.stageWidth;
const LB:uint = stage.stageHeight;
var __last:*;
var dropIndex:int;
function EnterFrame(e:Event):void {
if (mouseX < LL || mouseX > LR || mouseY < LT || mouseY > LB) {
__last.x = positionsArray[dropIndex].xPos;
__last.y = positionsArray[dropIndex].yPos;
__last.stopDrag();
}
}
var hitArray:Array = new Array(hitTarget1, hitTarget2, hitTarget3);
var dropArray:Array = new Array(drop1, drop2, drop3);
var positionsArray:Array = [];
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);
positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}
function mdown(e:MouseEvent):void {
__last = e.currentTarget;
dropIndex = dropArray.indexOf(__last);
setChildIndex(MovieClip(__last), numChildren - 1);
__last.startDrag();
addEventListener(Event.ENTER_FRAME, EnterFrame);
}
function mUp(e:MouseEvent):void {
if (__last.hitTestObject(hitArray[dropIndex])) {
__last.x = hitArray[dropIndex].x;
__last.y = hitArray[dropIndex].y;
} else {
__last.x = positionsArray[dropIndex].xPos;
__last.y = positionsArray[dropIndex].yPos;
}
__last.stopDrag();
removeEventListener(Event.ENTER_FRAME, EnterFrame);
}