-
Drag and Drop Hit-test
Hi.
Does anyone know a great technique to implement drag-and-drop, with a hit test during hover time, that doesn't require many resources, and is extremely generalized?
I'll explain what I am trying to do. I'll write the code, but I'm wondering if there are going to be some pitfalls once I get started, and thought maybe I'd get a little advice first.
Basically, I want to be able to have different types of draggable movie-clips, and when I drag them onto each other, and I want that to effect the way that they relate to one another.
There could be any number of different types of objects (that stem from a movie-clip).
Any one could be dragged-and-dropped onto another and that would affect them both. When one object is dragged over another, the target object should glow, and when it is dragged out, the former target should cease to glow. When it is dropped, the dragged object becomes an aspect of the target.
What I'm wondering is if there is an event-driven way of doing this, similar to onRollOver/onRollOut, rather than a more processor-consuming recursive hit-test. So the only time anything is invoked, something needs to happen, rather than running some kind of cycle that checks to see if something needs to happen every time.
Does this make sense?
Thanks.
-
Hi there,
I'm trying to do something similar and I have to warn you (if you are not very advance with Flash) that you will come across a few problems.
I'm doing a similar project (for what you describe) so I'll give you my script (I hope it is of any use for you). Everything is based on frame labels so the problem relies when you have many objects with many possibilities but have a try and if you come up with the answer please send me a few words.
on (press) {startDrag(this, true, 375, 200, 815, 310);
}
on (release) {stopDrag();
}
this script is for the main object or the first object you drag. The numbers are the boundaries for that object (L-T-R-B)
on (press) {
startDrag("_root.colour");
origX = _x;
origY = _y;
}
on (release) {
stopDrag();
_x = origX;
_y = origY;
if (_root.colour._droptarget == "/mainObject") {
_root.mainObject.gotoandStop("colour1");
}
}
This is the script for the second object that you drop into the first object (I hope this makes sense)
But perhaps you would like to check this tutorial at http://www.kirupa.com/developer/acti...t/dragdrop.htm
I hope this was helpful !
:smoov:
-
hello,
i havent got too much time at the moment to make you a taylor made function, but i do have a script which will help you.
it is just one function, and you can have any clip being the target or the one thats being dragged. im my example the dragged clip changes alpha when it is in close proximity to the target:
Code:
function dragAndDrop(clip:MovieClip, target:MovieClip, range:Number):Void {
var dragging:Boolean = false;
clip.onPress = function() {
this.swapDepths(this.getNextHighestDepth());
this.startDrag(false);
dragging = true;
};
clip.onRelease = clip.onReleaseOutside=function () {
this.stopDrag();
dragging = false;
};
var orX:Number = clip._x;
var orY:Number = clip._y;
clip.onMouseMove = function() {
if (!dragging) {
if (Math.round(Math.sqrt(Math.pow(clip._x-target._x, 2)+Math.pow(clip._y-target._y, 2)))<=range) {
clip._x = target._x;
clip._y = target._y;
} else {
clip._x = orX;
clip._y = orY;
}
clip._alpha = 100;
} else if (dragging) {
if (Math.round(Math.sqrt(Math.pow(clip._x-target._x, 2)+Math.pow(clip._y-target._y, 2)))<=range) {
clip._alpha = 60;
} else {
clip._alpha = 100;
}
}
updateAfterEvent();
};
}
// USAGE
//dragAndDrop(clip, clipTarget, myRange)
just put the code on the frame,
the first parameter is the clip which you want to drag, the second parameter is the clip you want as the target, and the third is how close you want the clip to be to the target, so that when it is released it goes to the target.
this method is based on distance checking and not hittest or onRollOver/Out.
HTH,
zlatan
-
1 Attachment(s)
Hi Dudeqwerty!!
thanks a lot for your reply, appreciate it!!!
But I'm not too sure if it is the code that I need.
Let me tell you what I want to achieve and maybe you can give me a tip.
I'm doing a website for an interior designer, I have multiple drag & drop objects to decorate a room.
1- I need to duplicate the first object that the user drags just incase it is used again.
2- I need to be able to apply colours and patterns to all duplicates.
3- The example I'm using is based on frame labels which means to me that I need to have as many frame labels as combination of colours and patterns (quit a lot!!)
I post the file just incase you want to have a look.
Thanks in advance...
:smoov:
-
i was relpying to laserbeamninja,
you should not hijack peoples threads. if you have a question post a new topic!
you also did this here http://board.flashkit.com/board/show...6&postcount=14
zlatan
-
For the elistis clan!
Hi zlatan,
First of all I AM NOT HIJACKING ANYONES THREADS!!!!!!!!!!!!!
And for sure I'm surprise of your reply!!!!!
I don't want to post topics that MANY PEOPLE HAS POSTED PREVIOUSLY but if you think this is a member club just let me know!!!
I'm just trying to be part of a community that isn't elitist and sadly you are being elitist!!!!!!
So next time when you post something in any forum make sure you say in your fist line: THIS ANSWER IS ONLY TO.....whoever your 'friend' is.
:thumbsdow
:thumbsdow
:yikes:
-
well you are hijacking the bloke's thread.
your question is different to his, the only way in which it relates is the fact that they both incorporate drag and drop functionality.
also, you've already posted your question in someone else's thread. so if you aren't hijacking threads then you are double posting at least, still against the rules.
whatever i, or anyone, posts on these boards is free for use for everyone, so its not just for one specific person, if it was i would PM them.
Now, as i've said, if you have your own question, post a new topic. if you think many people have already posted a topic similar to your problem, then search for it and try out the solution provided in that thread.
im not being elitist, im just trying to show you some proper forum etiquette. the boards would not be very nice if everyone posted their own problems in everyone else's threads, would it?
if you see me post a code sample, or whatever, and its in someone elses thread and you think it/i can help you, dont just assume it was for you, PM about it. im sure i'd be glad to help.
i dont want to argue, but thats it,
cheers,
zlatan,
p.s. this is a members 'club'
-
I don't have time for people like you dude ...
but I just want to make clear something...before your reply I sent my script to 'laserbeamninja' thinking that it was of any help.
If I was hijacking laserbeamninja's thread he/she had to say so and not you who has posted help after me...so please, next time read before you post something for your 'etiquette-members' club.
No nice from you mate!!! :thumbsdow :thumbsdow :thumbsdow
-
im sure your help was valid, im not here to make enimies, but i dont see why you would post some code that you're haveing trouble as a solution to someone else's problem?
im sorry if i hurt your feelings or whatever, i dont want to have you as an enemy.
zlatan,
anyway now that you are here, i cant view he tut in the link you sent, from what i understand you need an inventory type thing yeah?