Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 118,883 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,037 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



drag and drop

 
Reply to this topicStart new topic

drag and drop, multiples?

Joe123
post 4 Jun, 2008 - 07:26 AM
Post #1


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 27

hello there i have this code from"flash for dummies book", now it works fine, but could sum-one explain how i could make multiple balls so the user can drag them all to the same place?
for example. 30 balls all being put in a basketball net? etc..



[color=#FF0000]
// first take note of original position of ball
homeX = ball._x;
homeY = ball._y;

// start dragging when mouse is pressed on top of ball
ball.onPress = function()
{
// inside this function, "this" refers to the ball itself
this.startDrag();

// you could also force the clip's center to snap to the mouse like this:
// this.startDrag(true);

// you could also limit the area where you can drag the clip like this:
// this.startDrag(true, leftBoundary, topBoundary, rightBoundary, bottomBoundary);
}

// stop dragging when mouse is released
// it's also good to handle onReleaseOutside, as sometimes the mouse is outside the
// movie clip when it is released
ball.onRelease = ball.onReleaseOutside = function()
{
this.stopDrag();

// _droptarget tells you the name of the movie clip this clip was dropped on
// but it returns a string in Flash 4 format
// use eval() to convert it to a reference to a movie clip
// then see if it is the target movie clip
if(eval(this._droptarget) == target)
{
// if so, make its position equal to the target
this._x = target._x;
this._y = target._y;

// if you want, you can now disable drag-and-drop like so:
// delete this.onPress;
// delete this.onRelease;
// delete this.onReleaseOutside;
}
else
{
// if not, put it back at the home position
this._x = homeX;
this._y = homeY;
}
}

User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 4 Jun, 2008 - 11:11 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,692



Thanked 64 times

Dream Kudos: 1075
My Contributions


For future reference, please post your code between [ code] and [ /code] tags (without the spaces at the front)

I think I can help withthis issue, here is the easiest way I know possible.

First start off and make your movieclip (in the code above it is called 'ball' don't worry about naming it on the stage, the code will be changed to make everything easier.

Now, double click on the movieclip to enter it and be able to edit it. Go to the layers and frames section, adn if there is more than 1 frame in the moveiclip add a new layer above it that spans the whole length of frames.

On this new layer insert the code above (with a few changes), it should look like this:

QUOTE
// first take note of original position of ball
homeX = this._x;
homeY = this._y;

// start dragging when mouse is pressed on top of ball
this.onPress = function()
{
// inside this function, "this" refers to the ball itself
this.startDrag();

// you could also force the clip's center to snap to the mouse like this:
// this.startDrag(true);

// you could also limit the area where you can drag the clip like this:
// this.startDrag(true, leftBoundary, topBoundary, rightBoundary, bottomBoundary);
}

// stop dragging when mouse is released
// it's also good to handle onReleaseOutside, as sometimes the mouse is outside the
// movie clip when it is released
this.onRelease = this.onReleaseOutside = function()
{
this.stopDrag();

// _droptarget tells you the name of the movie clip this clip was dropped on
// but it returns a string in Flash 4 format
// use eval() to convert it to a reference to a movie clip
// then see if it is the target movie clip
if(eval(this._droptarget) == target)
{
// if so, make its position equal to the target
this._x = _root.target._x;
this._y = _root.target._y;

// if you want, you can now disable drag-and-drop like so:
// delete this.onPress;
// delete this.onRelease;
// delete this.onReleaseOutside;
}
else
{
// if not, put it back at the home position
this._x = homeX;
this._y = homeY;
}
}


NOTE - this code has not been tested and may take a bit more editing before it works correctly.

<edit>
Woops, forogt to tell you this:
After you have placed the code goto the main stage and to place the movieclips simply drag adn drop multiple copies of the movieclip to the stage, they don't even need to be at different places to work correctly (wbecause of the HomeX = this._x code above.

Happy flashing biggrin.gif
</edit>

Hope that helps.

This post has been edited by BetaWar: 4 Jun, 2008 - 11:13 AM
User is offlineProfile CardPM

Go to the top of the page

Joe123
post 6 Jun, 2008 - 06:05 AM
Post #3


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 27

CODE
hi thanks for helping me out, i am really new to flash, ive tried doing what you suggested and it works with dragging lots of balls but instead of going on the target like the first one, if i drag them to the bottom of the screen they go there, and to take them out i drag them just below target

www.dummies.com/go/flash8 this is the link were i got the flash file from

thankyou again



User is offlineProfile CardPM

Go to the top of the page

Joe123
post 6 Jun, 2008 - 06:22 AM
Post #4


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 27

also could you show me how i can add to this code by making the object stick in one place(target) and how you can drag it from that place and start again?
CODE
on(press){
             startdrag(this,false);
}
on(release){
    stopdrag();
}


because all the above does is make the user grab the object and move it around and drop it anyware i need it to go to one place but be able to move it again

thanks
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 6 Jun, 2008 - 02:22 PM
Post #5


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,692



Thanked 64 times

Dream Kudos: 1075
My Contributions


I am not sure exactly what you mean, if you are talking about having the ball placed back where it was originally this code here will work:

CODE
// first take note of original position of ball
this.homeX = this._x;
this.homeY = this._y;
// start dragging when mouse is pressed on top of ball
this.onPress = function() {
    // inside this function, "this" refers to the ball itself
    this.startDrag();
    // you could also force the clip's center to snap to the mouse like this:
    // this.startDrag(true);
    // you could also limit the area where you can drag the clip like this:
    // this.startDrag(true, leftBoundary, topBoundary, rightBoundary, bottomBoundary);
};
// stop dragging when mouse is released
// it's also good to handle onReleaseOutside, as sometimes the mouse is outside the
// movie clip when it is released
this.onRelease = this.onReleaseOutside=function () {
    this.stopDrag();
    // _droptarget tells you the name of the movie clip this clip was dropped on
    // but it returns a string in Flash 4 format
    // use eval() to convert it to a reference to a movie clip
    // then see if it is the target movie clip
    if (eval(this._droptarget) == target) {
        // if so, make its position equal to the target
        this._x = _root.target._x;
        this._y = _root.target._y;
        // if you want, you can now disable drag-and-drop like so:
        // delete this.onPress;
        // delete this.onRelease;
        // delete this.onReleaseOutside;
    } else {
        // if not, put it back at the home position
        this._x = homeX;
        this._y = homeY;
    }
};


I had an error in the previous cod ehtat I posted, but I just fixed it up.

On the drop target thing I am not sure if it will work out or not, I haven't used drop targets before myself so I haven't the foggiest on how to make them work.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 03:03AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month