Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Drag and drop -with a button AS 2.0 and 3.0 attempts

  1. #1
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default 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?

  2. #2
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default

    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.

  3. #3
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    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
    Code:
    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.

  4. The Following User Says Thank You to Medyman For This Useful Post:

    ReadyToLearn (05-21-2008)

  5. #4
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default cool beans, moving along..

    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?

    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.

  6. #5
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    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.

  7. The Following User Says Thank You to Medyman For This Useful Post:

    evan (05-21-2008)

  8. #6
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default One more question

    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?

  9. #7
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    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?

  10. #8
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default

    The API

  11. #9
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    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.

  12. The Following User Says Thank You to Medyman For This Useful Post:

    evan (05-22-2008)

  13. #10
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default

    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();

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •