4 Replies - 5030 Views - Last Post: 06 June 2008 - 02:22 PM

#1 Joe123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-May 08

drag and drop

Post icon  Posted 04 June 2008 - 07:26 AM

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;
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: drag and drop

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: drag and drop

Posted 04 June 2008 - 11:11 AM

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 :D
</edit>

Hope that helps.

This post has been edited by BetaWar: 04 June 2008 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 Joe123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-May 08

Re: drag and drop

Posted 06 June 2008 - 06:05 AM

 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



Was This Post Helpful? 0
  • +
  • -

#4 Joe123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-May 08

Re: drag and drop

Posted 06 June 2008 - 06:22 AM

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?
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
Was This Post Helpful? 0
  • +
  • -

#5 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: drag and drop

Posted 06 June 2008 - 02:22 PM

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:

// 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1