Mechraf

Currently in /2019/02/06/side-scroller-tutorial/


Side Scroller Tutorial

The side scroller tutorial is the very first tutorial anyone using the defold 2.0 engine would experience. It is very easy to follow with little or no hassle. The game is a simple one where a spaceship is controlled to collect stars and bonus stars to earn points.

The Ship Script

The very first thing to do is to modify the speed of the space ship. It is changed from 60 to 150 according to the tutorial. Personally, I chose 180. The lua code is as follows

local max_speed = 60

Changed to

local max_speed = 180  --mine actually

The bonus star and star script

The score is changed from 1 to 1000. I don’t see the point in going so high so I chose 5 instead.

local score = 5

Hot Reloading is useful if you don’t want to quit your game. This will be the case when you are working on a large game project. To hot reload, as you’ll see in the tutorial, use ctrl R for windows or cmd R for Mac users.

The bonus star

All the stars you find in the game are game objects which are made from a single prototype. They are manufactured in a factory (pretty intuitive!). So a new game object (go) is created and named “bonus_star”. But then, game objects cannot be displayed without sprites. Sprites are components that aid in displaying game objects and other similar functions.

Sprites consist of the following:

  • An id (identifier or name)
  • A url (location on disk)
  • Position and rotation in the game environment
  • An image property called “atlas”
  • An animation property
  • A blend mode

The atlas in a game can contain all the files specific for that kind of game object. The star atlas contains all the bonus and normal star images. These images constitute a flipbook animation

The factory script in this tutorial has to be modified to output the bonus star. This in this case is just uncomenting the code to give this.

component = "#bonus_factory"

The intricacies of the bonus star

The bonus star is created like so:

  • The game object star recieves a new
  • The factory game object contains te=wo sub factories, The Bonus_Star factory and the star factory
  • So the bonus star factory manufactures the bonus star object. This instruction is given in the script.
  • So basically, the bonus star contains the bonus star script, the bounus star sprite and the collision object. It’s a physics object by the way.

Set your Preferences!

The preferences have to be set to enable the esc button exit a game.

So it’s just to set “escape quits game” in the preferences.

The tutorial ends there. Obviously, this game needs a lot of improvements. I’ll try and add some aspects common to most games in subsequent tutorials. Until then, you can play around with the code yourself.