Solved

Drag and drop - avoid snapping to other Mc's

Posted on 2003-11-12
28
1,103 Views
Last Modified: 2010-04-03
Hello,

I have 5 draggable MC's. when i drag MC "1" and getting close to the other MCs like MC "2" or "3".... then the cursor(hand) picks up the other MC "2" or "3" and the MC "1" stays behind that MC "2" or "3".


I am clear about dragging Mcs to the targets and backing to original position, when released outside the targets.

I just want to avoid the snapping with other objects.

Pls let me know...

Ansari
0
Comment
Question by:Ansari
  • 12
  • 10
  • 6
28 Comments
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
let's see if i understand the problem, when you drag mc 1 over the other mcs, the other mcs start dragging as well?

paste your code on the the mc here.
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
post your code...

it seems like you need to have a check in the drag code that looks to see if another movieclip is currently being dragged...

it could be as simple as just setting a variable and having all of the movieclips check that variable to see if it is set to a number...

I think your code could be used in some very interesting ways as it is right now....  maybe in a magnet application or game  :)

post the code...


rp
0
 

Author Comment

by:Ansari
Comment Utility
My code is here:

I have buttons inside the MC.

on (press) {
    dr_x = this._x;
    dr_y = this._y;
    startDrag("");
}
on (release) {
    stopDrag();
    if (this._droptarget == "/box2") {
        this.gotoAndStop(2);
           } else {
        this._x = dr_x;
        this._y = dr_y;
    }
}


Thanks for your both fast reply.

Ans
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
don't you need to reference a movieclip instance name when you use startDrag()?

something like this maybe:

on (press) {
    dr_x = this._x;
    dr_y = this._y;
    startDrag(this);
}
on (release) {
    stopDrag();
    if (this._droptarget == "/box2") {
        this.gotoAndStop(2);
           } else {
        this._x = dr_x;
        this._y = dr_y;
    }
}


this is what is on all of your buttons?   can you post the url to the example .SWF and maybe the .FLA


rp
0
 

Author Comment

by:Ansari
Comment Utility
I named the instance as this, but  not working.

the URL is:
www25.brinkster.com/ansarish/dragndrop.swf

and one hint i got is using "swapdepth"...but need to make it work properly.
eg: _root.car.swapDepths(_root.cat); and other MCs...

Ans
0
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
here's code i'm using for multiple drag and drop items, showing items fill and when they've been dragged they return to original position empty. I also had a depth problem

PS. if you're using FlashMX - use hitTest rather than dropTarget

on (release) {
      stopDrag();
      if (this.hitTest(cookroot.pot)) {
          cookroot.addPot("flour");
         _parent.ob3emp.swapDepths(this); //currently dragged item appears on top of ob3emp
      }
}

i would use a loop to check which other movieclip the currently dragged one is over, and swop it with that one.
      
0
 

Author Comment

by:Ansari
Comment Utility
Thank you.

But swapdepth is not working for me.

actually what is "_parent.ob3emp"

i have many MCs like car, cat, chair....hope you have seen my swf.


0
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
yes i saw your swf.
here's a quick and easy trick

ps. _parent.ob3emp is referring to a movieclip I have on the main timeline, you just need to replace that with the instance names of your movieclips.

here's the quick and easy. You can only check a hitTest or dropTarget on release, but you want the dragged item to be on top while dragging, so this is my foolproof way...

just change your on press handler for each of the movieclips as follows:

on (press) {
    dr_x = this._x;
    dr_y = this._y;
    startDrag(this);
_root["cat"].swapDepths(this);
_root["chair"].swapDepths(this);
}

obviously, when this is on the cat movieclip, "cat" will be replaced with by car etc ....

not the most effective way, but its easiest and it does the job.


0
 

Author Comment

by:Ansari
Comment Utility
when second time pressing the button, it does not work ie., the swapdepth is not working. i have encountered this same problem before also.

0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
so I have seen the SWF and I don't see the problems you are facing...

are you now concerned with swapDepths?


what is the status of this question?

also, post the code you are currently using...


rp
0
 

Author Comment

by:Ansari
Comment Utility
Actually 2scenes: first scene is the intro and second scene has drag-drop exercise.

The draggable MC has 2frames. First frame has button and second frame is empty.


The code is:

on (press) {
    dr_x = 87;
    dr_y = 93;
    startDrag("");
    _root["cat"].swapDepths(this);
    _root["chair"].swapDepths(this);
    _root["crow"].swapDepths(this);
    _root["vase"].swapDepths(this);
    _root["phone"].swapDepths(this);
    _root["grass"].swapDepths(this);
    _root["rose"].swapDepths(this);
    _root["tap"].swapDepths(this);
    _root["horse"].swapDepths(this);
}
on (release) {
    stopDrag();
    if (this._droptarget == "/box2") {
        this.gotoAndStop(2);
        _root:t1 = "ok";
    } else {
        this._x = dr_x;
        this._y = dr_y;
    }
}


Ansari


0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
try this maybe...

on (press) {
    dr_x = this._x;
    dr_y = this._y;
    startDrag(this);
}
on (release) {
    stopDrag();
    // the below assumes that the instance name of the movieclip is box2
    if (this._droptarget == _root.box2) {
        this.gotoAndStop(2);
        _root.t1 = "ok";
    } else {
        this._x = dr_x;
        this._y = dr_y;
    }
}


and I would ignore the swapDepths problem, unless you really think it detracts from the overal user experience...  there is really no need to worry about it...


rp
0
 

Author Comment

by:Ansari
Comment Utility
no RP... swap depth is important for me. But i did one complex way that changing the other MCs  button to graphic in 3rd frame of that MCs and pressing the one button, will redirect to 3rd frame other MCs, which are graphics. this is very foolway. but no way.

i have to get clear solution for "swap depth" problem.

Ansari
0
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
Ansari, sorry i see the error in my ways, that won't work as it will only run the last line.

rascal pants, is there a way to check for collision while dragging?
that would solve it.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
Ansari,

it seems that your original quesiton has been answered...  please award the points to whom ever you believe helped you the most...

as for the other question, you need to open up a new question in this Flash Forum...

It is against the rules to ask multiple questions in the same posting...

---------------------------------------------------

you could test for collision by setting up a dummy movieclip that checks to see if one movieclip has hit another...

depending on your system, and the time it takes to drap and drop, this could be quite CPU intensive...

your dummy movieclip would have to have an if...then statement that looped through all of the movieclips to see if they hit...


rp
0
 

Author Comment

by:Ansari
Comment Utility
RP,

actually i didin't ask about collision; This is a hint discussed with in the forum for solving my probelm. And "swap depth" is not solved my problem.

then how colud i award the points.

you know one thing that i solved the problem by using my fool, complex way, as writen in my comment on 11.15.2003.

still if you think it is reasonbale to award means, no problem. tell me, i will do that, my friend.

i don't know to do your colision method in detail. iam not that much well in flash scripting.


Ansari

0
 
LVL 39

Accepted Solution

by:
blue-genie earned 250 total points
Comment Utility
Ansari,

hey, if you're still looking for a solution, I came across the same problem last night on something I was working on - here's a quick and easy solution, using swapDepths.

in your on (press) handler, just add the following line of code for all the movieclips
this.swapDepths(500);
so:

on (press) {
    dr_x = this._x;
    dr_y = this._y;
    startDrag(this);
this.swapDepths(500);

}

:-)
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
Ansari,

here is your original question:
----------------------------------------
I have 5 draggable MC's. when i drag MC "1" and getting close to the other MCs like MC "2" or "3".... then the cursor(hand) picks up the other MC "2" or "3" and the MC "1" stays behind that MC "2" or "3".


I am clear about dragging Mcs to the targets and backing to original position, when released outside the targets.

I just want to avoid the snapping with other objects.
----------------------------------------

This problem has been solved.  You are not allowed to ask multiple questions in the same thread.  It is a violation of the EE rules and the points system.

all of your movieclips are working properly and in the way you discribed in your original question, and so you need to award the points.

let me know if you need me to bring in a Mod or Admin to explain the way the site works....


rp
0
 

Author Comment

by:Ansari
Comment Utility
RP and to other friends:

Iam just now seeing the blue-genie's new solution as "this.swapDepths(500);". i tried this and it is working fine, even when dragging MC's multiple times.

In frist solution, when dragging MC's first time, the swapdepth was working fine, but when next two orthree times dragging, the draagable MC's going behind other MC's. so that soultion didn't work out.


now i have got solution, which is working and am happy.

Thanx

Ansari
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
Ansari,

post the a link to your current solution...  I tried the link above, and the swapDepth problems are still there...

it seems that if you use "this.swapDepths(500);" and click on more than one movieclip, the depth of both movieclips will be set to 500, so the original z position of the movieclips will take over...

I think it would be better to use a constantly incrementing variable like in the below code:

// put this in the first frame of the movie
incVar = 500;


//on the buttons

on (press) {
    _root.incVar ++;
    dr_x = this._x;
    dr_y = this._y;
    startDrag(this);
this.swapDepths(_root.incVar);

}


this should make sure that when you click on a movieclip the level is always on top of the others...


I have not tried this yet, but it seems to be pretty fool proof...


rp




0
 

Author Comment

by:Ansari
Comment Utility
Hi RP,

Actually iam not getting your comment fully like :"post the a link to your current solution... " and about the problem in swapdepth(500) etc....

so pls write me little more clear than this and give me some time to check and let you know.

Thanx

Ansari
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
I was just wanting to see what your final example looked like...  I tried the link at the top, and the problem still exists...

I was not sure if you had updated that SWF or not...


but after thinking about your problem, the swapdepth(500)  should not work all of the time...  it should only work for the first movieclip that you click on...  

the reason is because you are setting the depth of each movieclip to 500...  this is the same as not using swapdepth at all...

so you would need to increment a variable that the depth is always 1 higher than the last depth...



rp

0
 

Author Comment

by:Ansari
Comment Utility
hi Rp,

oh..man...you are right...

now iam facing that problem swapdepth(500)  ...

so am trying your "increement" now and let you know about solution later..

Ansari
0
 

Author Comment

by:Ansari
Comment Utility
Hi RP,

have added the following line in "on (release)" with your script ie.,

on(release)
    this.swapDepths(-int(_root.incVar));



it is working fine man, when going previous scene. but not working when going next scene.

hope you are clear. if i get soultion, i have to split award points with you also.

Ansari
0
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
Hi there.

Ansari, I used the this.swapDepths(500) approach in my project and I don't have any problems as Flash puts the most current movieclip on top.

Speak to rascal pants and have your points refunded if it didn't work for you.

:-(

blu
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
you really should not be using scenes...

there is no real reason to use them...  and when the SWF is compiled, all of the scenes are flattened into one long timeline...

this is the reason that some variables lose their scope, and all sorts of other funky things happen...


you should really think about just putting it all on one scene...



if not, I think you have to initialize the variable again in the first frame of each scene...


rp
0
 

Author Comment

by:Ansari
Comment Utility
Hi guys,

The blue-genie's answer, i accepted first, is working fine. but with some xtra scripts.

The logic is when press, we change the MC depth to 500. I thought to give the same actual depth(before press), when release.

so first i get the deafult depth of MC by setting one variable in 1st frame:
_root:gd1 = rta1.getDepth();

then in button
on (press) {
    dr_x = this._x;
    dr_y = this._y;
    startDrag(this);
this.swapDepths(500);

on (release) {
    this.swapDepths(_root:gd1);
    stopDrag();
            }
    }
}


it is working very fine.

So RP, without your "VarInc" idea , it is working fine.

Blue-genie's answer(whichi already acepted) is right to solve my probs.


Ans
0
 
LVL 22

Expert Comment

by:rascalpants
Comment Utility
great...  it is always best when the member solves their own problems  :)

good luck...

rp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now