Moving Object Area

  • #1, by elwanFriday, 27. March, 14:56 2 months 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

    24 Posts


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

    Thread Captain

    1312 Posts

  • #3, by afrlmeFriday, 27. March, 17:10 2 months 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

    6769 Posts

  • #4, by elwanFriday, 27. March, 18:06 2 months 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

    24 Posts

  • #5, by afrlmeFriday, 27. March, 18:26 2 months 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

    6769 Posts

  • #6, by elwanSaturday, 28. March, 11:04 2 months 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

    24 Posts

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


    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 }
    1
    2

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

    Imperator

    6769 Posts

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

    Newbie

    24 Posts

Write post