How do I make a player sprite follow the cursor?

Anybody remember that old internet game Poptropica?

I’m trying to replicate their controls, which rely solely on the left mouse key to move left, right, jump and interact with npcs. To make your player sprite move left, you drag your cursor across the left side of the screen. To make your player sprite move right, you drag your curser across the right side of the screen. To jump, you have to click on the upper part of the screen. Interacting with npcs by clicking on them.

Essentially the sprite will follow the cursor (as long as it’s pressed)

You can use ‘position of cursor’ event with >,< signs and under touch controls use ‘touch and hold’ event for drag. For click use touch and hold with ‘trigger once’ events.

The events may not have the exact words what i showed.

or you can use invisible trigger objects.

I’m using a similar system in my current game. I just do a left mouse is pressed, and then tween the character to the MouseX() and/or MouseY() positions. To try it just make sure you give your character a tween behaviour. To make the character “jump”, I do a condition where the cursor must be touching the character when the left click is pressed to set off the jump motion, instead of the Tween movement.

In this single line of code from my game, the character Manita will move to the MouseX() position + an additional 100 pixels over a 3 second time span (3000ms) using the inbuilt tween “swingToFrom”. There are loads of pre loaded tweens so you can choose which type of tween motion suits you best.

Alternatively, I have a different level where the movement just makes the character follow the mouse while the left mouse button is down. Maybe that’s closer to what you had planned:

^ You don’t need to add the “+ Variable(speed)” - I just added that to make the movement gradually speed up according to that variable but you can leave it out.

This is exactly what I had in mind! Thanks!

Wait, how do I make the sprite face the direction it’s walking in while using this command? Normally you’d just have the animation flip when the left or right key is pressed but we aren’t using the keys.

The way I did it is created two new points on the left side and right side of the character, using Edit Points in the animation window. I named the points Left and Right in that window. Then used the code below, where “Manita2” is the name of my character object so you substitute this with yours. You may need to mess around with the code slightly to fit your game. Specifically where you put the “yes” and “no” and the greater than (>) and less than (<) .

1 Like

That’s an interesting way too Diana. Another way I think if you attached 2 very large objects on to the character, one on left side, then another on right side. Their edges should meet near the dead center of the character. Then just do as Diana says and flip the character according to which object the mouse is touching. (And hide the objects so they aren’t visible on screen).

1 Like