Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Gamemaker Essentials

You're reading from   Gamemaker Essentials Learn all the essential skills of GameMaker: Studio and start making your own impressive games with ease

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781784396121
Length 154 pages
Edition 1st Edition
Arrow right icon
Toc

Simple toggles


When debugging a game, it's often useful to be able to enable or disable things with the press of a button. Things such as lasers, turrets, and pausing the game can all be done with just the press of a button and a single line of code.

For this, we will use the not statement, which is also represented by an exclamation mark (!). The not statement is often used in an if statement to check whether something is false instead of true. The way it works is by simply multiplying the returned result by -1, making it negative if the number was positive or positive if the number was negative.

We can use this to create toggles. Here is some example code:

if (keyboard_check_released(vk_space)){
    on= !on;
}

The preceding code checks to see whether the player has released the spacebar key and if so, it will set the on variable to be the opposite of what it was before. This works by setting on to !on. In other words, by multiplying on with -1.

This code will work the exact same way:

if (keyboard_check_released...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime