Get/Set Cursor Position example!

  • #1, by afrlmeMonday, 29. October 2012, 15:42 12 years ago
    Ok so another simple example of LUA Script is getting or setting the position of your mouse cursor which can be used to detect if you are in a certain area or you want to prevent the mouse from entering or leaving an area if you need the player to do something first.

    I tried to show an example of this with my "Simple Maze" template but apparently V.S has issues with complicated object areas. I have yet to try using way areas/points & action areas yet & controlling character pos with mouse pos - maybe that'll be ok for a work-around!?

    anyway onto the examples for this!

    Firstly we'll do setCursorPos which is nice & simple & looks like this: "setCursorPos({x=co-ordinate,y=co-ordinate})"

    setCursorPos({x=400,y=450})

    You can find the co-ordinates you need via the numbers in brackets on the top right hand corner of the scene preview screen by hovering mouse cursor over the area you want to find the co-ordinates to - see screenshot below.

    hint: x = horizontal & y = vertical (for anyone who doesn't know)

    http://alternatingfrequencies.com/vs/sm_screenie.png

    right now onto get cursor position script which is also a simple one line piece of code which can also include return values of if pos = so so then do so so or do nothing.

    basic example

    getCursorPos()

    setting the cursor pos in a custom variable

    local CurPos = getCursorPos()

    Adding it to a return value if/else statement

    local CurPos = getCursorPos()

    if setCursorPos({x=400,y=450}) != CurPos then setCursorPos({x=400,y=450}) else end

    this is just a quick example but you could use LUA to find the position & width/height of an object area or something & use that to calculate if the cursor is inside or outside of the object area & what to do in each instance wink

    P.S I know the final code example isn't correct as it will most likely set the cursor pos before checking the saved cursor pos obtained in the get cursor pos statement - I will update it later on with something better!

    Imperator

    7278 Posts


  • #2, by BigStansMonday, 29. October 2012, 16:12 12 years ago
    Another one wink Did you know the Eventhandler functions? http://wiki_de.visionaire2d.net/index.php?title=RegisterEven...

    Great Poster

    361 Posts

  • #3, by afrlmeMonday, 29. October 2012, 16:21 12 years ago
    nope but I haven't gone over all the wiki yet - I will check what you linked & amend my changes to the examples when I've done.

    Cheers smile

    Imperator

    7278 Posts

  • #4, by afrlmeWednesday, 31. October 2012, 14:37 12 years ago
    ok so I looked at the register event handlers but all I'm seeing is a load of text which is over-complicating what I need to set/do etc.

    For example how would I type out a mouse event for mouse moves? so I can then define with other if else statements what it should do?

    also how do I read the x,y positions of getCursorPos() individually?

    Cheers wink

    Imperator

    7278 Posts