View Full Version : Drag and drop -with a button AS 2.0 and 3.0 attempts
Eventially I want to select a button-->choose and item --drag and release selected item.
first I'm just trying to click a button, then onRelease -of the selected movieclip I can drop it.
Here is an attempt in AS2: this works but it doesn't "let go" unless I click the button again, soon one hand I want the button to trigger the event, on the other I want it to act independently of the button to let it go.
red_btn.onPress = function(){
red2.startDrag("true");
}
red_btn.onRelease = function(){
stopDrag();
}
Here is an attempt in AS3:
//this is the code I'm tinkering with:
a_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
dragme_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
// this is custom
function mouseDownHandler (evt:MouseEvent) :void {
var object = evt.target;
startDrag.("dragme_mc");
}
function mouseUpHandler (evt:MouseEvent) :void {
var object = evt.target;
stopDrag();
//this is working code for ordinary drag and drop
/*standard
dragme_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
dragme_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);*/
/*this is standard for drag and drop
function mouseDownHandler (evt:MouseEvent) :void {
var object = evt.target;
object.startDrag();
}
function mouseUpHandler (evt:MouseEvent) :void {
var object = evt.target;
object.stopDrag();
}
*/
}
Any ideas?
I just wanted to add that there is one way I can work around it, that I did not mention is:
Put the script that drags the movieclip in frame 2 -but I want to learn how do write cleaner code.
Medyman
05-20-2008, 03:26 PM
Why are you click a button to startDrag()? If you use that technique, your target movieclip (red2) will jump from wherever it is to where the button is. If this is in relation to the toolbar, take another look at how I did it.
Basically, the reason you're seeing that behavior is because when you click on the button (red_btn), you begin the dragging of the target (red2). You then say that when you release the mouse on the button, to stop dragging. That will work, if you do what you've coded. As you've said, you need to click on the button again, meaning you need to onRelease on the button.
I'm assuming what's happening here is that you click on the button, mouse off of it to drag the target MC around and the release. The problem is that you're not release on top of red_btn. So, you need to utilize onReleaseOutside here.
AS 2.0
red_btn.onPress = function(){
startDrag(red2, true);
}
red_btn.onReleaseOutside = function(){
stopDrag();
}
Your startDrag() syntax was also off. I've corrected it above.
In AS 3.0, the same principle applies. So instead of setting the MOUSE_DOWN event listener, to a particular movieclip, you probably just want to set it to the stage. I trust you can figure it out from there.
Let me know if that doesn't make sense or if you have other questions.
I got it, I have the gestalt not the experience,
Thanks for the syntax tip , I knew what was wrong but could not see how to change it -any rules to live by with AS 2. syntax in that case -aside from more practice? :rolleyes:
But check this: I did manage to get the AS 3.0 right oddly enough
var a:Number=10;/*starting value to toggle dragging*/
//drag and drop listeners
box2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
box2.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
d_btn.addEventListener(MouseEvent.MOUSE_DOWN, turnOn);
//user is dragging
function mouseDownHandler(evt:MouseEvent):void {
var object = box2;
if (a > 10){box2.alpha = .5;} /*condition set*/
if (a > 10){object.startDrag();}/*condition set*/
}
function mouseUpHandler(evt:MouseEvent):void {var obj =
evt.target;
box2.alpha = 1;
obj.stopDrag();
(a = 1);/*dragging condition set to prevent more dragging*/
}
//toggle dragging ON
function turnOn(evt:MouseEvent):void {(a = 15);/*condition validated*/}
This works! -what's messed up is I this is more complicated and I got THAT to work.
Medyman
05-20-2008, 10:49 PM
Thanks for the syntax tip , I knew what was wrong but could not see how to change it -any rules to live by with AS 2. syntax in that case -aside from more practice?
No, not really. Just when you're in doubt, consult the docs. The syntax in this case is really different than the general syntax of movieclip.method(). Also, be careful of the type of data you're feeding the method as parameters. "true" is very different from true -- one is a string (in quotes) and one is a boolean. The startDrag() function takes a boolean value for the "lock" parameter.
So far with drag and drop I have seen it work when applying functions and /or event listeners to specific moviecplips only.
If I wanted to create shapes, and then move them I don't know how that works yet.
I have a some info on "sprites" -is that heading in the right direction?
Medyman
05-21-2008, 05:33 PM
If I wanted to create shapes, and then move them I don't know how that works yet.
What do you mean by create shapes? Drawing dynamically using the Drawing API? or within the IDE?
Medyman
05-22-2008, 04:27 PM
Well, if you create the drawing within a container movieclip, you can then apply actions to the container movieclip and treat it just as you would any other.
Is there some kind of routine to generate multiple container movieclips on demand?
I saw an example of that done with instances of library movie clips but not one with "container movie clips" with spites or shapes.
BTY when do I want to use sprites VS shapes?
ie var newThing:Spite=new Sprite(); vs var newThing:Shape=new Shape();
Medyman
05-22-2008, 09:48 PM
Is there some kind of routine to generate multiple container movieclips on demand?
Yes, the createEmptyMovieClip() (http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002447.html) method.
duplicateMovieClip() (http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00001496.html) is another method that you can use to replicate library items, if that's the route you want to take.
That's in AS 2.0. In AS 3.0, you can just create a new variable of type MovieClip and that'll do it for you.
I ran across this (http://blogs.adobe.com/pdehaan/2006/07/creating_new_movieclips_in_act.html), which seems like would be very handy with what you're trying to do. It goes over createNewMovieClip in AS 2.0 vs AS 3.0.
BTY when do I want to use sprites VS shapes?
First, so there aren't any confustions, the Sprite and Shape classes are AS 3.0 classes. Sprite is the lighterweight incarnation of the MovieClip class. Movieclip has been kept in AS 3.0 for backwards compatibility but you should really be using Sprite instead.
If all you need to do is to draw a shape using the Drawing API, use Shape. It is a part of the flash.display package and inherits all of the DisplayObject properties. But be aware that Shape does not extend either the InteractiveObject or DisplayObject container, so you won't be able to attach any mouse and/or keyboard events to it. Also, you cannot add any children within a Shape.
So, it uses less system resources, but there are some real limitations to using Shape.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.