Behavior: Mouse Follow

jdkilby

  • Posts: 7
I'm currently working on my first game prototype in Stencyl and thought I'd share my first Behavior of real substance.

When attached to an Actor, the Mouse Follow behavior causes the actor to... well, move to the mouse cursor. The difference is that it adds a "lag" effect so that it isn't just a one-to-one movement of the Actor to the location of the mouse cursor.

The further away the Actor is from the mouse cursor, the faster it moves toward the cursor.

You can set the maximum distance and maximum speed for each axis (the position adjustments are performed individually on each axis). When the Actor is at the maximum distance or greater from the mouse cursor, it moves at the maximum speed.

If you decide to set the maximum speed greater than the maximum distance, the Actor will follow the mouse cursor one-to-one. This is not recommended because this could be done much more simply and efficiently with just a couple of blocks.

Lastly, if you look at the code, you'll see that this Behavior does not use the built-in speed / velocity variables for Actors. I found the performance to be better when I handled calculating and maintaining these variables myself.

It's on the StencylForge... feedback and suggestions are welcome!
JD Kilby
Flash game developer

http://www.jdkilby.com

Epic428

  • Posts: 1118
Great Job! I'm sure someone will find this very useful. And Welcome to Stencyl!
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

carroll6

  • Posts: 76
I'll make you one my man.  I got this to work, and along with "hide mouse" it solved a request that I have for a "custom cursor" that I had in this same group.  Nice work bud.

Silux

  • Posts: 438
So you can make the actor taking exactly the same time to reach the cursor from 2 pixel away
and to reach the cursor from 2parsec away?
Currently working at:
Starwarrior 2097(my main project)
How to make successful games in Kongregate and the world(article)

jdkilby

  • Posts: 7
Sorry for the late replies...

Thanks epic and carroll!

Silux: I'm not sure I understand what you want to do. Are you saying you want an Actor to take some fixed number of seconds to travel between its current location and where the mouse cursor is located?
JD Kilby
Flash game developer

http://www.jdkilby.com

Silux

  • Posts: 438
Currently working at:
Starwarrior 2097(my main project)
How to make successful games in Kongregate and the world(article)