Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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 $19.99/month. Cancel anytime