Straight Forward Touch/Mouse Drag Offset? [Solved]

Get help using Construct 2

Post » Sun Dec 08, 2013 6:35 am

Hi there, I was trying to figure out a way to do a drag / offset so when i drag something it doesn't snap to center.

I found this thread http://www.scirra.com/forum/no-offset-drag-and-drop_topic47745.html but following the one available capx, it didn't work when I tried to reproduce it and all the while it struck me as oddly complicated to achieve the desired result.

Of course the capx out there works great, but when I tried to replicate it, it failed, but it was kinda complicated so I couldn't understand the need for having additional instance variables "mangle"? "mdist"? i don't understand what those names mean related to the offset..

I see the drag/drop behavior but it doesn't seem to address the idea of mouse offset.

So yea I just wanted to see if there was some straight forward approach to allow you to touch the lower corner of a dragable object and not have it snap to center but rather let you drag it from where you touch the object.

Thanks!
Calebpart12studios2013-12-09 16:53:53
B
59
S
9
G
1
Posts: 942
Reputation: 6,553

Post » Sun Dec 08, 2013 7:12 am

Off the top of my head...You can try storing the distance between the mouse and the object in private vars when it's clicked (by subtracting both x & y coordinates), then set the object position to the mouse minus those vars every tick. Of course you'd need a "picked" variable to decide if the object should be dragged.

The pin behavior would come in handy here since it accounts for offset, angle, etc. but I don't think you can pin an object to the cursor, and any object following the cursor lags so it won't be totally accurate.MadFactory2013-12-08 07:14:11
Image
B
243
S
30
G
13
Posts: 1,787
Reputation: 18,770

Post » Sun Dec 08, 2013 8:52 am

Instead of using the drag and drop behaviour you could choose to do something like this.

give the object three instance variables, picked, offsetX, OffsetY

on object clicked
- set picked to true
- set offsetx to object.x-mouse.x
- set offsety to object.y-mouse.y

object is picked
- set object x to mouse.x+object.offsetx
- set object y to mouse.y+object.offsety

on mouse button released - object set picked to false

I'm on mobile, so I can't check the events inside construct atm, but it should be something like this.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Mon Dec 09, 2013 4:09 am

@LittleStain i follow this, but "object is picked". I can't find an action that matches that.

my attempt failed to result in a proper drag and i imagine the object is picked part is a big piece of it

https://www.dropbox.com/s/ih8hjpukbmzy96f/TD_Test_4.capx

Thanks!
Caleb
B
59
S
9
G
1
Posts: 942
Reputation: 6,553

Post » Mon Dec 09, 2013 4:28 am

@part12studios
Hey!)
Here, you forgot to set position of the object to mouse.position + offset)


PM'd you.JJList2013-12-09 15:05:19
B
18
S
5
G
1
Posts: 74
Reputation: 2,672

Post » Mon Dec 09, 2013 4:53 pm

awesome thanks @JJList and everyone for your input. This is great. I just know that offset/dragging will be something I need to use and this is a much more efficient way to achieve this effect

Thanks!
Caleb
B
59
S
9
G
1
Posts: 942
Reputation: 6,553


Return to How do I....?

Who is online

Users browsing this forum: 25games, zenox98 and 20 guests