Moving Object Area

  • #1, by elwanFriday, 27. March 2020, 14:56 4 years ago
    I managed to drag an object with the mouse with some Lua code, but of course when I drop it and try to grab it from its new location to move it again, it's not possible because the object area didn't follow.
    Is there a way to oblige the object area to folow the position of an object, and what could be the Lua code to move an object area?...

    Newbie

    26 Posts


  • #2, by SimonSFriday, 27. March 2020, 14:57 4 years ago
    Well how are you moving it now ?

    Thread Captain

    1580 Posts

  • #3, by afrlmeFriday, 27. March 2020, 17:10 4 years ago
    You need to use offset to move the object area with the object sprite. I highly recommend for keeping things simple, to create the object in the top left corner of the scene so it starts at 0, 0 & then use Lua at begin of scene to move it wherever it needs to be. Store the position in some values or something.

    Imperator

    7278 Posts

  • #4, by elwanFriday, 27. March 2020, 18:06 4 years ago
    The code  I used is nearly the same as the the one used in the game "Chaos in the Cannibal Village" to move the sound sliders in Options Menu.
    The main script is looking like this :

    AnimWidth = 148
    AnimHeight = 108
    function dropObj(typ, xpos,ypos)
      local VolAnim = getObject("ActiveAnimations[AnimPz" .. typ .. "]")
      VolAnim:setValue(VAnimationCurrentPosition, {x=xpos,y=ypos})
    end
    function dragObj(typ)
      local CursorPos = getCursorPos()
      local xPos = math.floor(CursorPos.x - AnimWidth / 2 + 0.5)
      local yPos = math.floor(CursorPos.y - AnimHeight / 2 + 0.5)
      dropObj(typ, xPos,yPos) 
    end 
    function mousebtnHold(standard)
      if standard == false then
        game:setValue(VGameHoldTime, 1)
      else
        game:setValue(VGameHoldTime, defaultHoldTime)
      end
    end
    The  main difference is that my object area is exaltly the same size as the object I want to move and I keep the procedure running until I click again  to the final destination. That way I can move th object anywhere on the screen (not limited to the size of the object area)
    Of course after that I cannot pick up the object anymore.

    What would be the code to refer to the object area with Lua?

    I could send my .ved file. What would be the procedure?

    Newbie

    26 Posts

  • #5, by afrlmeFriday, 27. March 2020, 18:26 4 years ago
    You are dragging around an animation? You need to target the .Offset data structure field belonging to the scene object instead of the animation, but it's offset & not absolute position so you will need to take that into consideration.

    I would recommend using the mouse event handler too, then you can listen out for left click hold & check if the object you want to move is below the cursor, then you could start a temporary loop to handle the movement & then kill the loop on left mouse button release.

    search for mouseevent here.

    Imperator

    7278 Posts

  • #6, by elwanSaturday, 28. March 2020, 11:04 4 years ago
    "You need to target the .Offset data structure field belonging to the scene object instead of the animation"

    No idea how to to that!
    Few lines of code would be very much welcome if you can find the time...

    Newbie

    26 Posts

  • #7, by afrlmeSaturday, 28. March 2020, 11:38 4 years ago
    game.CurrentScene.Objects["Rock"].Offset = {x = 100, y = 100}


    You need to take into consideration that it gets offset from the default position & is not absolute position, so if you want to position the object somewhere in the scene & then offset it you are going to have to include some math calculations to calculate the difference to get the absolute position. If I remember correctly that was something like...
    local pos = game.CurrentScene.Objects["Rock"].Position
    
    game.CurrentScene.Objects["Rock"].Offset = { x = 100 - pos.x, y = 100 - pos.y }

    which is target position minus actual position - or maybe it's the other way around, I forget, sorry.

    Imperator

    7278 Posts

  • #8, by elwanSaturday, 28. March 2020, 17:30 4 years ago
    Thank you again for answering.
    It's giving me a starting point for a new approach...

    Newbie

    26 Posts