How do I bound Drag and Drop to sprite

  • 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

    [attachment=1:3kcrk8c8][/attachment:3kcrk8c8]

    Here is the capx file

    [attachment=0:3kcrk8c8][/attachment:3kcrk8c8]

    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!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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:

    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[/code:swv2lxcq]
    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.
  • .....

  • 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:

    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[/code:17m4tx50]
    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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)