标题或多或少是不言自明的,我一直在研究许多不同的教程,而且我对AS3来说并不是很完美。 (上图显示了我的目标)
无论如何,在我看到的大多数在线教程中我注意到,拖放教程要么基于一个对象到一个目标,要么多个对象到多个目标,所以我想知道是否有人能够帮助我并解释如何让多个对象连接到一个目标。
并且,如果可能使其可切换,例如,当我将对象2拖过时,如果对象1已经在目标上就位,则对象1返回其原始位置,对象2取代它。
更简单的解释方法是说我正在尝试创建一个有三个雕像的游戏,用户可以选择三个中的一个放置在一个设定的目标区域。
如果我说的话没有多大意义,我会道歉,如果这会引起混淆,我会澄清任何事情。这是我目前正在使用的AS3代码。
var startX:int;
var startY:int;
circle1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var theTargetName:String = "target" + event.target.name;
var theTarget:DisplayObject = getChildByName(theTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == theTarget){
event.target.buttonMode = false;
event.target.x = theTarget.x;
event.target.y = theTarget.y;
}
else{
event.target.x = startX;
event.target.y = startY;
circle1_mc.buttonMode = true;
circle2_mc.buttonMode = true;
circle3_mc.buttonMode = true;
答案 0 :(得分:5)
您可以使用dropTarget
来查看被删除的对象是否是"触摸"而不是检查hitTestObject
。 theTarget
。否则,已放到theTarget
的任何其他项目可能会被报告为dropTarget
。
此外,由于MovieClip
是动态的,因此您可以在每个实例中存储startX
和startY
值。
以下修改后的代码将使用单个target_mc
作为放置目标。当一个项目被丢弃时,任何其他项目将被移回原来的位置:
// create an array as @David suggested to keep track of your draggable items
var circles:Array = [circle1_mc, circle2_mc, circle3_mc];
for each(var circleMC:MovieClip in circles)
{
circleMC.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circleMC.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circleMC.startX = circleMC.x;
circleMC.startY = circleMC.y;
}
function pickUp(event:MouseEvent):void
{
// no longer need to keep track of startX & startY here because that's already been done up above
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void
{
event.target.stopDrag();
// check to see if the event target is touching target_mc using hitTestObject
if(event.target.hitTestObject(target_mc)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
// move all circles OTHER than the current target back to their original positions
for each(var circleMC:MovieClip in circles)
{
if(event.target != circleMC)
{
circleMC.x = circleMC.startX;
circleMC.y = circleMC.startY;
}
}
}
else
{
// only need to move the event target back if it was dropped outside of target_mc
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}