Scroll background with mouse movement

  • #1, by PaupasiaTuesday, 17. April 2018, 01:25 7 years ago
    Well yes ... always the same question.... grin 
    Is it possible to move the background based on the mouse position?
    I would love to add the 180 ° view in the game.
    I found this old post   LINK that may work, but it would not move hotspots. 

    Workaround, automate the right, left, up, down movements with mouse over. 
    The problem is that when you change scene, the mouse falls into the next hotspot and a loop is triggered between the two scenes.

    How can I give a global command that snap the cursor in the center of every new loaded scene?

    This is the most immediate and simple solution that came to my mind so far.
     
    I also found this script 


    that could be adapted but unfortunately I do not understand anything. roll

    Forgot to mention.... is a first person game, no character in scene.smile

    Forum Fan

    165 Posts


  • #2, by afrlmeTuesday, 17. April 2018, 11:50 7 years ago
    Can't really help you with the panoramic first person stuff, but to center the mouse you can use...

    setCursorPos({x = game.WindowResolution.x / 2, y = game.WindowResolution.y / 2)


    That will center the cursor on the current viewport, but if you wanted to center it in the exact center of the current scene then you would need to either know the width & height of the scene or use the getSize function to obtain it.

    local swh = game.CurrentScene.SceneSprite.SpriteSprite:getSize()
    game.ScrollPosition = {x = (swh.x / 2) - (game.WindowResolution / 2), y = (swh.y / 2) - (game.WindowResolution.y / 2)}
    setCursorPos({x = game.WindowResolution.x / 2, y = game.WindowResolution.y / 2)


    I'm not sure if I've written it correctly, but in theory it should set the scrollposition of the scene right in the center & set the cursor in the center of the current viewport.

    Imperator

    7285 Posts

  • #3, by sebastianTuesday, 17. April 2018, 12:04 7 years ago
    regarding mouse moving you can set this in the scene settings to make it scrollable as soon as the mouse reaches the edges of the currenrly viewed area. 

    Thread Captain

    2346 Posts

  • #4, by PaupasiaTuesday, 17. April 2018, 13:50 7 years ago
    First of all, thank you very much for the answers smile

    @sebastian I often use this action, but it is not very intuitive without a character. Even if you change the scroll factor, in the next scene the mouse creates a loop with the previous one.

    @AFRLme I have tested the script in all ways roll
    - like definition script called by "at beginning of scene" to see if it was applied in subsequent scenes (I have about 1600 scenes to change, sooo  boring grin). 
    - As script inside the scene itself, both "at the beginning of scene" and "at the end of scene"
    - I also tried giving an exact location , example:     setCursorPos({x=644,y=365})

    Only the last setCursorPos({x=644,y=365}) set in "at the end of the scene" worked. 
    No way to get a global command applied to all the scenes.

    When changing scene, the mouse hovers on the left or right of the screen and not in the middle. Maybe the "fade to scene" effect (300ms) does not allow the script to work fast?

    Forum Fan

    165 Posts

  • #5, by afrlmeWednesday, 18. April 2018, 10:38 7 years ago
    hmm it's possible. You can write a loop to listen out for scene changes. It's how I create a basic autosave system.

    local scn = nil
    
    function scenechange()
     if not game.CurrentScene:isEmpty() and not game.CurrentScene.SceneIsMenu then
      if scn ~= game.CurrentScene:getName() then
       scn = game.CurrentScene:getName()
       setCursorPos({x = game.WindowResolution.x / 2, y = game.WindowResolution.y / 2)
      end
     end
    end
    
    registerEventHandler("mainLoop", "scenechange")


    It should listen out for scene changes. If the name in the variable doesn't match the current scenes name then it should update the variable & do whatever other code you decide to insert inside of it.

    Imperator

    7285 Posts

  • #6, by PaupasiaThursday, 19. April 2018, 00:07 7 years ago
    It works! But only if there are no transitions between the scenes. 
    Unfortunately it is as I feared, with the "fade to scene" effect the script overlaps and is applied during the transition, with the consequence that the cursor moves where there is the mouse instead of the center.
    Thank you very much for your help, for sure I will find a way to apply it! grin

    Edit..
    Stupid question ... could I use this script to automate the scene change? 
    I have scene objects LEFT, RIGHT, GO repeated in each scene. Could I use a global getObject.name to trigger the "change scene" action under "left_click"?

    Forum Fan

    165 Posts

  • #7, by afrlmeThursday, 19. April 2018, 11:37 7 years ago
    You mean with a mouse event handler? Hmm... sorry, just woke up. My brain's a bit foggy at the minute. Anyway...

    In regards to scene transition, you could change instant & toggle scene fade yourself very easily.

    You could do this just before changing to the next scene to fade the current scene out.
    game.CurrentScene:to(250, {SceneBrightness = 0})

    & inside of the at begin of scene action for each scene, you could do the opposite to fade the scene in.
    game.CurrentScene.SceneBrightness = 0; game.CurrentScene:to(250, {SceneBrightness = 100})

    Or another better alternative would be to use an interface that is permanently displayed & fade that in & out as you don't have to mess with brightness or anything then. I suppose this all depends on if you are only using basic fade transition or are also using the other transition types too.

    Imperator

    7285 Posts

  • #8, by PaupasiaThursday, 19. April 2018, 23:25 7 years ago
    Ahah!  In the morning I too have the brain on vacation! grin

    The method with the interface seems interesting even if I meant something global like this LINK to trigger the action under "left click".
    But I fear that Visionare can be confused because the objects LEFT, RIGHT and GO are repeated with the same name in each scene

    Forum Fan

    165 Posts