Flash AS3 - 拖放多个对象到一个目标? [英] Flash AS3 - Drag and Drop multiple objects to one target?
问题描述
<标题或多或少都是不言自明的,我一直在通过许多不同的教程,而且我对AS3的评价并不太好,因此非常诚实。 (上图显示了我的目标)
无论如何,我在大多数在线教程中看到的拖放教程都基于一个对象到一个目标或多个对象到多个目标,所以我想知道是否有人善意帮助我,并解释如何让多个对象连接到一个目标。
并且,如果可能使其可切换,例如,如果在拖动对象2时对象1已经在目标上,则对象1返回它的原始位置和对象二取代它的地方。
一个更简单的方法来解释这是说,我试图创建一个游戏,有三个雕像和用户可以选择三个之一放置在设置的目标区域。
我道歉,如果我说的没有多大意义,将清理任何东西,如果混乱。这是我现在使用的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);
函数pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
event.target.parent.addChild(event.target);
$ b 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;
dropTarget
,你可以使用 hitTestObject
来查看被删除的对象是否触摸 theTarget
。否则,任何已经被放到 theTarget
上的其他项目可能被报告为 dropTarget
。 此外,由于
MovieClip
是动态的,因此可以存储 startX
和 startY
以下修改后的代码将使用一个 target_mc
作为一个放下的目标。当一个项目被放在它上面时,任何其他的项目都会被移回到原来的位置:
$ $ p $ $ code创建一个数组为@David建议跟踪您的可拖动项目
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;
函数pickUp(event:MouseEvent):void
{
//不再需要跟踪startX& startY这里,因为这已经完成
event.target.startDrag(true);
event.target.parent.addChild(event.target);
$ b function dropIt(event:MouseEvent):void
{
event.target.stopDrag();
//使用hitTestObject检查事件目标是否触及target_mc
if(event.target.hitTestObject(target_mc)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
//将所有圈子比当前目标移回其原始位置
(var circleMC:MovieClip圈)
{
if(event.target!= circleMC )
{
circleMC.x = circleMC.startX;
circleMC.y = circleMC.startY;
}
}
}
else
{
//只需要将事件目标移回target_mc以外
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
Title is more or less self-explanatory, I’ve been working through many different tutorials and I’m not exactly too good with AS3 to be perfectly honest. (Image above shows what I'm aiming for)
Anyway, what I notice in most of the online tutorials I see, the drag and drop tutorials are either based on one object to one target or multiple objects to multiple targets, so I was wondering if someone is kind enough to help me and explaining how I can get multiple objects to connect to one target.
And, if possible making it switchable, as in for example, if object 1 is already in place on the target when I drag object 2 over, then object 1 returns to its original position and object two takes its place.
A easier way to explain this is to say that I'm trying to create a game where there are three statues and the user can pick one of the three to place in a set target zone.
I apologies if what I say doesn't make much sense, will clear up anything if that causes confusion. Here's the AS3 code I'm using at the moment.
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;
Instead of checking the dropTarget
, you can use hitTestObject
to see if the dropped object is "touching" theTarget
. Otherwise, any other item that has already been dropped onto theTarget
may be reported as the dropTarget
.
Also, since MovieClip
is dynamic, you can store the startX
and startY
values in each instance.
The following modified code will use a single target_mc
as a drop target. When one item is dropped on it, any other items will be moved back to their original spot:
// 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;
}
}
这篇关于Flash AS3 - 拖放多个对象到一个目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!