Scrolling a view with your mouse

A tutorial by Andrew Barber aka Chronic

In this tutorial i'm going to explain a really simple way of scrolling around a view using your mouse. In the past I used to do a region checking method to check where my mouse cursor is and apply the correct movment to the view. While there isn't really anything wrong with this method if its what you need in your game, there is a much simpler way to achieve it.

First of all a little preperation is needed. Add a background to your project and then draw some random lines in it, save it and add it to a room and make sure its tiling in both horizontal and vertical directions. This is basicly so we can prove the view is moving around when we come to test it.

Now add a new object, and place this any where in the room.

Once you have done that, set your room to a width and height you want, for example purposes i'll use 1000x1000. Now enable view 0 and also make it visible when room starts. Leave the size of the view and port at the default sizes of 640x480. Set the object to follow to object0 (assuming you never renamed the object). I also suggest to set the room speed to 60, this helps to keep the slight delay between the mouse and object to a minimum.

Now comes the coding part, all 2 lines of it. Add the following to an End Step event.

[END STEP]
{
  x = mouse_x;
  y = mouse_y;
}

Thats it, you're done. Run the game and move around your mouse, you should see the view moving around appropriately. However it may look a little fast, this can easily be solved by editing your view again and changing the horizontal speed (hsp) and the vertical speed (vsp) to whatever suits your needs.

Keep in mind that object0 is essentially a mouse cursor, so why not give it a sprite and hide the regular windows cursor in the global game settings.

  • Example 1