Moving object to cursor position

  • #1, by BarneyThursday, 08. January 2015, 16:42 10 years ago
    Hiya, guys! I've got what seems to be a rather trivial task for LUA coding (like two-three lines tops), and I hoped you could give me a hand.
    Basically, I have a *wall* object, and the player has to *stick* an item to this wall, and it has to stick exactly to same place where the player has clicked the mouse button (the wall's object area is pretty big, so the item's position can vary greatly).
    I've figured it should be done like this: I create a scene object which is identical to the item in appearance, and then, when the player clicks the mouse, I use LUA to get the cursor position and move the object to this position.
    I guess I have to use the getCursorPos() first, but how do I assign these coordinates to the object?

    Newbie

    42 Posts


  • #2, by afrlmeThursday, 08. January 2015, 17:16 10 years ago
    getCursorPos() actually returns the cursor position of the current viewport instead of the whole scene, which means to get the actual position you need to include the current scroll position for both x & y.

    Another quick note: the object position is actually the object interaction position. If it's an animation you wanted to place onto the scene at a specific position then the position would be based on the top left pixel of the animation sprite, so you might need to include an offset if you wanted to center the image to the mouse cursor position, which can easily be done by sharing both the width & height by 2 (half the actual size).

    What I would recommend doing is creating it as an animation instead of a sprite object as it's easier to move than object sprite, because as I said, the object position is based on the interaction position for the object.

    Add this inside of an execute a script action part. Edit as needed.
    local pos = getCursorPos()
    local anim = ActiveAnimations["animation_name"] -- replace animation_name with name of animation
    
    startObjectTween(anim, VAnimationCurrentPosition, anim.AnimationCurrentPosition, {x = pos.x + game.ScrollPosition.x, y = pos.y + game.ScrollPosition.y}, 3000, easeQuintIn) -- smooth build up to target position over 3 seconds
    


    I have actually written a workflow function for simplifying the moving of animations, but for this purpose I figured I should post the actual startObjectTween function.

    You might want to include an additional bit of math to the script if you want to center the animation image to the mouse cursor position as it's based on top left corner as I mentioned earlier. So in the startObjectTween function you would add after game.ScrollPosition.x
     + 100 -- replace 100 with whatever half the size of your image is (in pixels)
    

    ...do the same thing again for the game.ScrollPosition.y but the value should be half image/animation height.

    Imperator

    7278 Posts

  • #3, by BarneyThursday, 08. January 2015, 19:15 10 years ago
    Thanks a lot!! Works like a charm! =)

    Newbie

    42 Posts

  • #4, by afrlmeThursday, 08. January 2015, 19:26 10 years ago
    No problem. smile

    Imperator

    7278 Posts