Book Image

GameMaker Cookbook

Book Image

GameMaker Cookbook

Overview of this book

GameMaker: Studio started off as a tool capable of creating simple games using a drag-and-drop interface. Since then, it has grown to become a powerful instrument to make release-ready games for PC, Mac, mobile devices, and even current-gen consoles. GameMaker is designed to allow its users to develop games without having to learn any of the complex programming languages such as C++ or Java. It also allows redistribution across multiple platforms. This book teaches you to harness GameMaker: Studio’s full potential and take your game development to new heights. It begins by covering the basics and lays a solid foundation for advanced GameMaker concepts. Moving on, it covers topics such as controls, physics, and advanced movement, employing a strategic approach to the learning curve. The book concludes by providing insights into complex concepts such as the GUI, menus, save system, lighting, particles, and VFX. By the end of the book, you will be able to design games using GameMaker: Studio and implement the same techniques in other games you intend to design.
Table of Contents (17 chapters)
GameMaker Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Applying force via magnets


Remember playing with magnets in science class when you were a kid? It was fun back then, right? Well, it's still fun; powerful magnets make a great gift for your favorite office worker. What about virtual magnets, though? Are they still fun? The answer is yes. Yes, they are.

Getting ready

Once again, we're simply going to modify our existing physics environment in order to add some new functionalities.

How to do it

  1. In obj_control, open the code block in the Step event.

  2. Add the following code:

    if keyboard_check(vk_space)
    {
    with (obj_dynamicParent)
        {
        var dir = point_direction(x,y,mouse_x,mouse_y);
        physics_apply_force(x, y, lengthdir_x(30, dir), lengthdir_y(30, dir));
        }
    }  

Once you close the code block, you can test your new magnet. Add some objects, hold down the spacebar, and see what happens.

How it works

Applying a force to a physics-enabled object in GameMaker will add a given value to the direction, rotation, and speed of said object. Force can be used...