Reader small image

You're reading from  Learning Construct 2

Product typeBook
Published inDec 2014
Reading LevelIntermediate
Publisher
ISBN-139781784397678
Edition1st Edition
Languages
Right arrow
Author (1)
Aryadi Subagio
Aryadi Subagio
author image
Aryadi Subagio

Aryadi Subagio is the second among seven brothers. He has loved video games ever since he was little, and he wanted to make his own game in his teenage years. This led him to pursue a Diploma in Computer Programming after he graduated from high school, where he spent years learning about software development techniques. He learned about game development from online sites, because at that time, there was no book on game development in Indonesia. After graduating, he immediately jumped into the world of game development. During this time, he made a few shooting games for the Flash Platform. After a year, he decided to gain more experience by working in a game studio; this is when he joined Esabra Studio. After working at Esabra Studio, he worked at Enthrean Guardian, a game studio based in Semarang. He has now quit working and is trying to set up his own studio. Aryadi has a passion for sharing his knowledge. He has written a lot of blog posts about game development on Indonesian and English sites. He also shares his knowledge on his own personal blog at http://daggio21.blogspot.com/.
Read more about Aryadi Subagio

Right arrow

Chapter 6. Creating a Space-shooter Game

Congratulations on reaching this chapter! By now, you have learned a lot about Construct 2 and how to use it. You now know about various behaviors and also how to use them through events. You have also learned about expressions and sub-events that are useful when making more complex games. However, there's still one thing missing if you want to create an engaging experience: competition.

Making players compete with their friends is a good way to improve the gameplay experience. One way to do this is to add a leaderboard to your game so that players will fight for the position of the highest score. We will add this element to a space-shooter game. In creating this space-shooter game, you will learn how to create a scrolling background and spawn a new object related to an existing object.

In this chapter, you will learn:

  • How to create a scrolling background

  • How to store variables in Construct 2 and compare them with other stored values

  • How Construct 2 stores...

Setting the stage


So, just like the previous chapter, we'll start by designing our layout. The sprites used in this chapter can be found under the Space shooter folder under Sprites in freebundle.zip. However, we want to change the default resolution first, because our space shooter will be a vertical one and not horizontal. If you start with a new empty project, then, by default, you'll get a resolution of 854 x 480. To change this, we will simply change the value of the window size in Project Properties. To open the project properties, click on the View link in Project Properties or click on the project name, which is the first item in the Project bar on the right-hand side.

Clicking on this will show the project properties with a lot of configurations that you can apply to your project, such as changing the name and description of the game, choosing what preview browser to use, and selecting the physics engine for the physics behavior, among other things. For now, let's change the window...

Storing data in Construct 2


This is usually the time when I teach you to destroy the enemies and add scores to the game. However, because the focus of this chapter is to store data from the game, we'll cover this first. There are four objects we can use to store data in Construct 2: array, dictionary, WebStorage, and XML. We're not going to cover XML in this book, because XML is a bit hard for beginners of programming, but only knowing the first three is enough for you to save data from your game.

Getting to know arrays

An array, in programming context, is a list of ordered things. An array can be a list of all text values, all number values, or a combination of text and numbers. In programming languages, arrays are written as a list of data inside a square bracket; so, in this book, I will also write an array in such a way. For example, an array of countries will be ["Canada", "USA", "Russia", "Italy", "Germany", "England"], while an array of numeric values will be [100, 250, 350, 150, 600...

Storing data in the player's machine


We know how to store data in an array and dictionary and how to show them onscreen, great! However, there's one thing to keep in mind when working with arrays and dictionaries: they will lose their values when the game is closed. The players might close the web browser window. Whenever they do this, they close the game, and the game will not remember the last value you put inside the array or the dictionary.

In most cases, this is not what we want. If we want to keep a score and then compare it to create a leaderboard, we want the game to remember it even after the game is closed. To do this, we need to store the data in the player's computer. So, when we start the game next time, our game can still remember the player's score. How do we do this? Using a WebStorage object.

Note

A WebStorage object does not store data in an online storage despite the name. It stores data on the player's computer locally. It also works when used to create offline games with...

Summary


You learned a lot of different things in this chapter, from changing the size of the game window and layout to using an imagepoint to spawn a new object relative to an existing object. You then learned about different objects to store data: arrays, dictionaries, and WebStorage. You also learned how to store data in an array and dictionary.

You then learned in a bit more detail about the differences between an array and a dictionary and when to use them. Another small but useful thing that you learned is how to disable and re-enable events. After that, we found out how we can store data on the player's computer instead of saving it only in the game. After that, we retrieved this data in the later playing sessions. Finally, we discovered a concept called loop, which can be used to create a leaderboard.

If you want to delve deeper into the complicated topics I covered here, you can visit some of the pages at Scirra's online manual; for example:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Construct 2
Published in: Dec 2014Publisher: ISBN-13: 9781784397678
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Aryadi Subagio

Aryadi Subagio is the second among seven brothers. He has loved video games ever since he was little, and he wanted to make his own game in his teenage years. This led him to pursue a Diploma in Computer Programming after he graduated from high school, where he spent years learning about software development techniques. He learned about game development from online sites, because at that time, there was no book on game development in Indonesia. After graduating, he immediately jumped into the world of game development. During this time, he made a few shooting games for the Flash Platform. After a year, he decided to gain more experience by working in a game studio; this is when he joined Esabra Studio. After working at Esabra Studio, he worked at Enthrean Guardian, a game studio based in Semarang. He has now quit working and is trying to set up his own studio. Aryadi has a passion for sharing his knowledge. He has written a lot of blog posts about game development on Indonesian and English sites. He also shares his knowledge on his own personal blog at http://daggio21.blogspot.com/.
Read more about Aryadi Subagio