View Full Version : Drag&Drop with "If"
Hi there.
I have a problemin flash 5.
I have 10 draggable MC, I'd like that if MC1,MC2,MC3,MC4 are dropped on another MC
hiting a checking button the movie will gotoAndPlay (2);
else hiting a checking button the wrong MC will return at the default position.
I made the /drag-n-drop/ tutorial but this is not exactly wath I need.
Somebody can help me?
Thanks
Rupa
Warrior
09-27-2003, 03:01 AM
// Put this Code on frame one of the Movie Clip
var origX = _x;
var origY = _y;
function drag () {
this.startDrag();
}
function drop () {
stopDrag ();
if (_droptarget != "/MovieClip") {
_x = origX;
_y = origY;
}
}
// Put this code on the button
on (press) {
drag();
}
on (release) {
drop();
}
Tell me if it works
Xenozip
09-27-2003, 04:35 AM
Sorry, I don't mean to hijack this thread, but I seem to be having the same problem.
In laymens terms, here's what I want :
5 MCs, each with unique gfx and ordered by number (i.e. : Drag1, Drag2, Drag3, etc). And 5 different target MCs, ordered the same way (i.e. : Target1, Target2, etc).
Next, I want the MCs to start drag on mouse click, then snap back to its original position if the MC was dropped outside the target areas. (... Of corse, there's more stuff I want, but I need to take this a step at a time.)
So far, I've created the 5 "drag" MCs and 5 "Target" MCs, then created another MC which I called "DDActions".
I then copied the DDActions clip inside each of the "drag" MCs.
Next, I added a very simple/basic actionscript into the main timeline as follows :
// "which" seems to define the "current" MC being interacted with.
// without having to define it mathematically.
function stDrag(whichDrag) {
startDrag(_root[whichDrag], true, 31, 31, 450, 350);
}
function stSnap(whichDrag) {
stopDrag();
}
Then, I edited the "DDActions" clip, and added the actions into the first frame as follows :
_visible = false;
_parent.onPress = function() {
_root.stDrag(_parent._name);
};
_parent.onRelease = function() {
stSnap(_parent) ();
};
So far, that works for moving around all the MCs with a small ammount of code.
However, I'm having a tremendous ammount of trouble trying to figure out how to snap the current MC back to its original position when using this method of code.
I tried setting two variables called "xStart" and "yStart" when the movie calls the onPress function, which defines the _x and _y positions of the MC.
I then added code into the main timeline within the "stSnap" function, which told the "whichDrag" MC to go to the _x and _y positions defined by the "xStart" and "yStart" variabled.
However, it seems to ignore all that extra code I put in. :(
It's like 3 a.m. here and I'm getting very tired and frustrated. -.-
I bet there's even a very simple answer to this problem I'm having, but I sure can't figure it out.
I tried looking inside the Tutorials page on this site, and no luck. I also tried looking through the "Puzzle"/"myPuzzle" tutorials that come with Flash MX, and that script defines the "Snap" function mathematically, which I'm trying desperately to avoid.
Of corse, after all that, I have to figure out how to define the _droptarget with multiple targets to choose from from within a MC inside of a MC.
Any help would be greatly appreciated. :)
Xenozip
09-27-2003, 08:35 AM
Just to update, I still need some help, but here's what I've got going now:
Right now, I have 5 MCs labeled p1-5 (i.e. "p1", "p2", "p3", etc). And 5 MCs labeled b1-5 (i.e. "b1", "b2", "b3", etc), and finally a dynamic textbox with the varible set to "disPlay".
The "p#" MCs are the dragable items, and the "b#" MCs are the targets for the puzzle.
Here is the actionscript I have for the p1 MC:
// Is there a better handler to use than "ocClipEvent (load)" ?
// Later, I wish to add a scrable function into the main timeline.
// If I had this function..
// then the starting _x and _y positions will change.
onClipEvent (load) {
var corX = this._x;
var corY = this._y;
}
// Start Drag, lock and constrain clip.
on (press) {
this.startDrag(true, 31, 31, 450, 350);
}
// This is where I'm having trouble.
// With the first if statement, the clip will lock to "b1".
// The Dynamic text labeled "disPlay" will display "Correct".
// So that first condition works fine.
// The else statement works properly as well.
// All of the else if statements are really haphazard, but they work.
// I'm trying to figure out a way to condense those statements,
// so that it can be just one condition,
// that I can use over and over for all clips.
on (release, releaseOutside) {
stopDrag();
name = this._name;
int_name = name.substring(1);
int_name = parseInt(int_name);
if (eval(this._droptarget) == _parent["b"+int_name]) {
_root.disPlay = "Correct";
this._x = _parent["b"+int_name]._x;
this._y = _parent["b"+int_name]._y;
} else if (eval(this._droptarget) == _root.b2) {
_root.disPlay = "Incorrect";
this._x = eval(this._droptarget)._x;
this._y = eval(this._droptarget)._y;
} else if (eval(this._droptarget) == _root.b3) {
_root.disPlay = "Incorrect";
this._x = eval(this._droptarget)._x;
this._y = eval(this._droptarget)._y;
} else if (eval(this._droptarget) == _root.b4) {
_root.disPlay = "Incorrect";
this._x = eval(this._droptarget)._x;
this._y = eval(this._droptarget)._y;
} else if (eval(this._droptarget) == _root.b5) {
_root.disPlay = "Incorrect";
this._x = eval(this._droptarget)._x;
this._y = eval(this._droptarget)._y;
} else {
_root.disPlay = "No Target";
this._x = corX;
this._y = corY;
}
}
Basically what I need is a way to condense the script so that all of the "incorrect" placements can be written in a single condition that changes from clip to clip (depending on what clip is being manipulated).
I know that's asking a lot, but it should be possible.. Perhaps with an array?
Also, I think the onClipEvent (load) handler is a bad way of doing the first few lines of code, because I want to add in a scrable function (as explained above), which means the starting _x and _y positions will change for each clip. If the pieces are scambled, and the user were to drag a clip to an empty space, it would return the clip to its original position before the scramble.
However, if I put the starting _x and _y variables inside the main timeline in the scrable function, then I don't know how to tell the clip that information. Is it does with the "tellTarget" handler?
Help would be greatly appreciated. :)
Warrior
09-27-2003, 12:23 PM
Give me a little time to read through the post and I will answer you
Hi Warrior,
thanks for your reply, I tried your script, but unfortunately
dosen't work, I am not sure that I understood what you wrote
(I am a really beginner in Action Script, and my english too is not so good).
So I post the a .fla file here:
http://www.rupak.net/working/dragmc/dragMC.htm
pls. take a look.
Thanks a lot Rupa
Hi Warrior,
I hope you received my e-mail, thanks a lots for your help,
I fixed the file as you wrote, now i have the last problem,
means that more than one MC (let's say 3) must be dragged for go in another frame,
dragging more or less MC should give an error message, and the MC are splitted in 2 family,
so the correct action should be to drag 2 MC from a family and one from the second.
At the same url: http://www.rupak.net/working/dragmc/dragMC.htm
I posted a new file, fixed with your suggestions, but with the new problem
not jet fixed, can you take a look?
Thanks a lot Rupa
Hi rupa,
Just try it out this code,
on (press) {
startDrag (this, true);
}
on (release) {
stopDrag ();
}
Hi Arun,
thanks for your reply, sorry, but I don't understood
where i should put your code, and for what?
Now the problem is to limit the "right" combination
at 2MC from a row and 1 form the other one.
Now How you see it works with several combination.
Thanks a lots for your reply.
Rupa
Warrior
09-30-2003, 03:01 PM
To Xenozip::::
I went through your post but I find it hard to understand and I don't know how to help you because I don't know where to start. Can you explain to me what you wanted it to so exactly and let's go through it step-by-step.
To rupa::::
I downloaded your Flash file but don't know what you want me to do exactly. How many Movie Clips do you want to drag at the same time or was there somthing else?
Hi Warrior,
sorry for my english, I will try to explain better.
That file should be a puzzle,
you have 6 small MC (C1,C2,C3,C4,C5,C16,)
an one biggest the light yellow one(name=corn).
If the player will drag (not at the same time) and will put on "corn" two MC
from the first row (C1,C4,C6)
and one MC from the second row (C2,C3,C5), hiting "check"
will go in the frame "Done" ,
never mind wich Mc hi will drag,
but the combination is two MC from (C1,C4,C6)
and one from (C2,C3,C5)
If the player will drag on "corn" one MC from the first row and two
from the second row will get an error message.
Dragging more than 3 MC, will get an error message.
I hope now it is more clear, sorry again for my english.
Greetings Rupa
Xenozip
09-30-2003, 08:46 PM
Originally posted by Warrior
To Xenozip::::
I went through your post but I find it hard to understand and I don't know how to help you because I don't know where to start. Can you explain to me what you wanted it to so exactly and let's go through it step-by-step.
I felt like I was hijacking this guys (or gals) thread, so I started a new one here (http://www.actionscript.org/forums/showthread.php3?s=&threadid=35161) and basically answered my own questions in the process of posting them. :)
The puzzle I was making is essentially done except for the pretty graphics.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.