gpt4 book ai didi

flash - Actionscript 3.0 一次拖动一个 MovieClip

转载 作者:行者123 更新时间:2023-12-01 11:06:23 25 4
gpt4 key购买 nike

我一直在创建一个 Flash 文件,其中可以在舞台上拖动和移动多个 MovieClip。我已经设置了一些功能来控制它何时被拖动,但是当我单击一个 MovieClip 时,另一个会立即将自己附加到另一个。我该如何解决这个问题?

代码如下:

import flash.geom.Point;

//Add variables and items, such as milk and eggs to stage:
var milkClickOffset:Point = null;
var eggClickOffset:Point = null;

var milk:Milk = new Milk();
milk.x = 250;
milk.y = 250;
addChild(milk);

var egg:Egg = new Egg();
egg.x = 330;
egg.y = 330;
addChild(egg);

//Milk Event listeners:;
addEventListener(MouseEvent.MOUSE_DOWN, startMilkDrag);
addEventListener(MouseEvent.MOUSE_UP, stopMilkDrag);
addEventListener(Event.ENTER_FRAME, dragMilk);

//Egg Event listeners:
addEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
addEventListener(MouseEvent.MOUSE_UP, stopEggDrag);
addEventListener(Event.ENTER_FRAME, dragEgg);

//Milk Functions:;
function startMilkDrag(event:MouseEvent):void
{
milkClickOffset = new Point(event.localX,event.localY);
}
function stopMilkDrag(event:MouseEvent):void
{
milkClickOffset = null;
}
function dragMilk(event:Event):void
{
if (milkClickOffset != null)
{// must be dragging
milk.x = mouseX - milkClickOffset.x;
milk.y = mouseY - milkClickOffset.y;
}
}

//Egg Functions:
function startEggDrag(event:MouseEvent):void
{
eggClickOffset = new Point(event.localX,event.localY);
}
function stopEggDrag(event:MouseEvent):void
{
eggClickOffset = null;
}
function dragEgg(event:Event):void
{
if (eggClickOffset != null)
{// must be dragging
egg.x = mouseX - eggClickOffset.x;
egg.y = mouseY - eggClickOffset.y;
}
}

最佳答案

因为您要将 eventListener 添加到 stage 而不是 movieclip

//Milk Event listeners:;
milk.mouseChildren = false;
milk.addEventListener(MouseEvent.MOUSE_DOWN, startMilkDrag);
milk.addEventListener(MouseEvent.MOUSE_UP, stopMilkDrag);
milk.addEventListener(Event.ENTER_FRAME, dragMilk);

//Egg Event listeners:
egg.mouseChildren = false;
egg.addEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
egg.addEventListener(MouseEvent.MOUSE_UP, stopEggDrag);
egg.addEventListener(Event.ENTER_FRAME, dragEgg);

关于flash - Actionscript 3.0 一次拖动一个 MovieClip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9687658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com