How do I bound Drag and Drop to sprite

Get help using Construct 2

Post » Thu Nov 06, 2014 6:44 pm

Hi,
I want to built a kind of a joystick to move my player.
I have created to sprites, one is the dragable stick and the second is the area where the joystick should be movable (horizontally only).

Joystick with area
dragndrop.png


Here is the capx file
dragndrop.capx


Do anybody know how to bound the dragable element on the sprite area? So it can only be dragged to the edges of the area sprite?
And if it's dragged to the left, the player should move left. If it's dragged right, he should move right.

Thanks for your help!
You do not have the required permissions to view the files attached to this post.
Image Image iTunes: Match3 | Google Play: Match3
B
103
S
37
G
31
Posts: 1,164
Reputation: 28,928

Post » Thu Nov 06, 2014 7:16 pm

You can restrict it to a box or circle. Arbitrary shapes are quite a bit more complicated. Here's how to restrict to a circle:

Code: Select all
global number dist=0
global number ang=0

joystick is dragging
--- set dist to distance(joystick_panel.x, joystick_panel.y, joystick.x, joystick.y)
--- set ang to angle(joystick_panel.x, joystick_panel.y, joystick.x, joystick.y)
--- joystick: set position to joystick_panel
--- joystick: move max(75, dist) at angle ang

Just change 75 to the max distance you want the joystick to go.

To make the player move according to the joystick you likely can do it by comparing the x positions of joystick and joystick_panel, or even as a sub event of "is dragging" and use the ang variable.
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Thu Nov 06, 2014 7:33 pm

..... :)
You do not have the required permissions to view the files attached to this post.
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Thu Nov 06, 2014 9:38 pm

R0J0hound wrote:You can restrict it to a box or circle. Arbitrary shapes are quite a bit more complicated. Here's how to restrict to a circle:

Code: Select all
global number dist=0
global number ang=0

joystick is dragging
--- set dist to distance(joystick_panel.x, joystick_panel.y, joystick.x, joystick.y)
--- set ang to angle(joystick_panel.x, joystick_panel.y, joystick.x, joystick.y)
--- joystick: set position to joystick_panel
--- joystick: move max(75, dist) at angle ang

Just change 75 to the max distance you want the joystick to go.

To make the player move according to the joystick you likely can do it by comparing the x positions of joystick and joystick_panel, or even as a sub event of "is dragging" and use the ang variable.


Thanks works perfect :)

@korbaach
Thanks for your example :)
Image Image iTunes: Match3 | Google Play: Match3
B
103
S
37
G
31
Posts: 1,164
Reputation: 28,928


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests