Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-02-2012, 04:16 PM   #1
JRKyte
Registered User
 
Join Date: Dec 2011
Posts: 11
Default [AS3] Problem with drag and drop

Hi all,

I have a small game, all that *I want* to happen is that you click a card, and can drag it, to a certain position, if its the right position it will stay there, if it's the wrong position it will return to the original position.

I tried to follow this tutorial (which is EXACTLY the effect I am after) but it has an external AS file, I want to do it with AS in the FLA file.

So far I have this:

ActionScript Code:
card12.addEventListener(MouseEvent.MOUSE_DOWN, startMoving); function startMoving(evt:MouseEvent):void { card12.startDrag(); } card12.addEventListener(MouseEvent.MOUSE_UP, stopMoving); function stopMoving(e:MouseEvent):void { card12.stopDrag(); }

Card12 is my card that you can pick up and drop obviously.

I know from the tutorial above I need to have 'targets' for my mc's.

But how would I go about doing this?

OR

How would I modify the code in the tutorial so that it works from within my FLA.

Any tips, suggestions, tutorial links or search ideas would be hugely appreciated!

Thanks

JR
JRKyte is offline   Reply With Quote
Old 01-02-2012, 04:17 PM   #2
JRKyte
Registered User
 
Join Date: Dec 2011
Posts: 11
Default

h t t p:// active.tutsplus. com/tutorials/games/create-a-drag-and-drop-game-in-actionscript-3-0/

THIS TUTORIAL ^^ Is what I tried to follow, sorry missed it out the post.

(Please delete the spaces between 'H T T P' and '// active' and '. com' - as it wouldenlt let me post the link as is)
JRKyte is offline   Reply With Quote
Old 01-03-2012, 10:34 AM   #3
JRKyte
Registered User
 
Join Date: Dec 2011
Posts: 11
Default

Hi, sorry to bump this I know everyone is real busy and everything but I really just need some direction, it's for my university project and I suck with AS; I would be super grateful if someone could just point me to a relevent tutorial, or just tell me what I need to change in the actionscript of the tutorial I mentioned for it to operate in the FLA file and not in a seperate AS file!

Hope some one can help, even some small info is better than nothing!

Thanks all!

JR
JRKyte is offline   Reply With Quote
Old 01-03-2012, 02:00 PM   #4
lilreaper13
Member
 
Join Date: Mar 2011
Location: Montreal, Qc
Posts: 57
Send a message via MSN to lilreaper13
Default

ActionScript Code:
var _oldCard12_X:int = 0; var _oldCard12_Y:int = 0; card12.addEventListener(MouseEvent.MOUSE_DOWN, startMoving); function startMoving(evt:MouseEvent):void { _oldCard12_X = card12.x; _oldCard12_Y = card12.y; card12.startDrag(); } card12.addEventListener(MouseEvent.MOUSE_UP, stopMoving); function stopMoving(e:MouseEvent):void { card12.stopDrag(); }

Now, the 'targets' that they're talking about are probably a hitzone, like in solitaire (screenshots.fr.sftcdn.net/fr/scrn/80000/80852/solitaire-game-4.jpg), you can see the card placeholder, it's just a line to indicate where to drop the card. If you want something like that, draw it in an mc, and let's say it's called target_mc (must be the same size as the card), you'd do :

ActionScript Code:
var _oldCard12_X:int = 0; var _oldCard12_Y:int = 0; card12.addEventListener(MouseEvent.MOUSE_DOWN, startMoving); function startMoving(evt:MouseEvent):void { _oldCard12_X = card12.x; _oldCard12_Y = card12.y; card12.startDrag(); } card12.addEventListener(MouseEvent.MOUSE_UP, stopMoving); function stopMoving(e:MouseEvent):void { card12.stopDrag(); if(card12.hitTestObject(target_mc)){  card12.x = target_mc.x;  card12.y = target_mc.y; }else{  card12.x = _oldCard12_X;  card12.y = _oldCard12_Y; } }
lilreaper13 is offline   Reply With Quote
Old 01-03-2012, 02:39 PM   #5
JRKyte
Registered User
 
Join Date: Dec 2011
Posts: 11
Default

Quote:
Originally Posted by lilreaper13 View Post
ActionScript Code:
var _oldCard12_X:int = 0; var _oldCard12_Y:int = 0; card12.addEventListener(MouseEvent.MOUSE_DOWN, startMoving); function startMoving(evt:MouseEvent):void { _oldCard12_X = card12.x; _oldCard12_Y = card12.y; card12.startDrag(); } card12.addEventListener(MouseEvent.MOUSE_UP, stopMoving); function stopMoving(e:MouseEvent):void { card12.stopDrag(); }

Now, the 'targets' that they're talking about are probably a hitzone, like in solitaire (screenshots.fr.sftcdn.net/fr/scrn/80000/80852/solitaire-game-4.jpg), you can see the card placeholder, it's just a line to indicate where to drop the card. If you want something like that, draw it in an mc, and let's say it's called target_mc (must be the same size as the card), you'd do :

ActionScript Code:
var _oldCard12_X:int = 0; var _oldCard12_Y:int = 0; card12.addEventListener(MouseEvent.MOUSE_DOWN, startMoving); function startMoving(evt:MouseEvent):void { _oldCard12_X = card12.x; _oldCard12_Y = card12.y; card12.startDrag(); } card12.addEventListener(MouseEvent.MOUSE_UP, stopMoving); function stopMoving(e:MouseEvent):void { card12.stopDrag(); if(card12.hitTestObject(target_mc)){  card12.x = target_mc.x;  card12.y = target_mc.y; }else{  card12.x = _oldCard12_X;  card12.y = _oldCard12_Y; } }

Thank you so much my friend, you are actually a life saver! I'd almost given up on the whole thing and began a redesign on my project last minute!

I got it working thanks to you, Again, thanks a bunch!

JR
JRKyte is offline   Reply With Quote
Old 01-03-2012, 02:53 PM   #6
lilreaper13
Member
 
Join Date: Mar 2011
Location: Montreal, Qc
Posts: 57
Send a message via MSN to lilreaper13
Default

No prob dude! Nice to hear that it's working!
lilreaper13 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:20 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.