Dragonfly Cloud is now available in the AWS Marketplace - learn more

Question: How do you rotate a sprite in GameMaker?

Answer

To rotate a sprite in GameMaker, you can use the built-in variable image_angle. This variable sets the angle of the instance's sprite in degrees, where 0 degrees is pointing right, and positive values rotate the sprite clockwise.

Here's an example of rotating a sprite:

// Set the image_angle to rotate the sprite to 90 degrees image_angle = 90;

If you want to continuously rotate a sprite, you might do this in the step event of an object:

// Rotate the sprite 5 degrees every frame image_angle += 5; // Ensure the angle stays between 0 and 360 degrees if (image_angle >= 360) { image_angle -= 360; }

In case you need to rotate a sprite around a specific point and not its origin, you will have to modify the image_xscale, image_yscale, and possibly also adjust the x and y position of the sprite to keep it centered.

For advanced rotations, like rotating towards a point or following the mouse, you'd calculate the desired angle using functions like point_direction() and then apply that angle to image_angle.

Here is how you could rotate an object to face the mouse:

// Calculate the angle from the object to the mouse position var target_angle = point_direction(x, y, mouse_x, mouse_y); // Now set the image_angle to this value image_angle = target_angle;

Keep in mind that rotation might affect collision checking if the sprite's bounding box changes. If precise collision checking is needed after rotation, appropriate collision masks should be used, or consider using precise per-pixel collision checking.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost