Welcome to this side-scroller Defold tutorial, where you’ll get a first taste of what making games in Defold is all about.
This tutorial uses a game that’s been prepared for you in advance and is targeted for beginners, especially who want quick results and a fast way to try Defold.
You’ll learn how to tweak it to make it more fun, and then add a new type of pickup. The tutorial should take about 10 minutes. We’ll guide you through every step, and when you’re done, we recommend checking out the other tutorials and manuals.
The game you’ll work with is very simple. The player controls a spaceship and is supposed to collect stars that appear on the screen.
You only need to build and run the game to try it. You can also select Project ▸ Build in the menu or shortcut Ctrl+B (Cmd+B on Mac).
Tip: When you have this tutorial README.md file opened in the Defold Editor, you can use the links to perform certain actions, like runing the game via the link above, or opening a file.
Try steering the space ship with the Up ↑ and Down ↓ arrow keys and pick up stars for points.
The game isn’t very fun yet, but you can easily improve it with a few simple tweaks. You’ll be making these updates live, so make sure you keep the game running somewhere on your desktop.
First, let’s adjust the speed of the space ship:
-
Find the file spaceship.script:
-
You can do so with the menu item File ▸ Open Asset... or shortcut Ctrl+P (Cmd+P on Mac).
-
You can start typing the word "spaceship" to search among all the available assets.
-
Select the file "spaceship.script".
-
Click the button Open to open the file in the Lua code editor.
The script will be opened in a built-in Defold code editor:
It might be convenient for this tutorial to split the Code Editor into two panes, so that you can see README.md and the currenly edited file simultaneously.
-
At the top of the file, change the line:
local max_speed = 60
to:
local max_speed = 200
This will increase the movement speed of the space ship.
-
If you didn't close the game, you can reload the script file into the running game with File ▸ Hot Reload` or shortcut Ctrl+R (Cmd+R on Mac). You should see in the console:
INFO:RESOURCE: /spaceship/spaceship.scriptc was successfully reloaded.And game should be still running. Otherwise, build and run it again.
Try moving the space ship with the arrow-keys on your keyboard. Notice how it moves faster now.
Currently, the player only gets 1 point for each star collected. A higher score is more fun, so let’s fix that.
-
Open the file "star.script". Either click the link here, use File ▸ Open Asset..., or find the file in the Assets browser in the leftmost editor pane and double click it. The file is in the folder named
stars. -
At the top of the file, change:
local score = 1
to:
local score = 1000
-
Reload the script file into the running game with File ▸ Hot Reload.
Try to collect some stars and notice how the score has increased.
The game would be more interesting if bonus stars appeared every now and then. To make that happen, you need to create a new game object file that will serve as a blueprint/prefab/prototype for the new type of star.
-
Right click the "stars" folder in the Assets view and select New... ▸ Game Object.
-
Name the new file "bonus_star". (The editor will automaticaly append a file type suffix so the full name will be "bonus_star.go".)
-
The editor automatically opens the new file so you can edit it.
-
Add a Sprite component to the game object. Right-click the root of the Outline view and select Add Component ▸ Sprite. This allows you to attach graphics to the bonus star.
In the Outline view on the right side, you will see a new item called "sprite". When it is clicked, its properties are displayed in the Properties view below. The sprite currently has no graphics attached so you need to do that:
-
Set the Image property to "stars.atlas" by using the browse-button (...). The atlas contains the graphics for the bonus star.
-
Set Default Animation to "bonus_star" - it is a name of an animation defined in the atlas.
-
A green star should now appear in the editor. Hit the F key or select View ▸ Frame Selection if the view of the star is not very good.
We need to detect a collision between the ship and the bonus star. In Defold, we use a component called Collision Object, which is already used by the spaceship to collide with regular stars and collect them. We will now add a Collision Object to the bonus star as well.
-
Right click the root "Game Object" item in the Outline view.
-
Select Add Component ▸ Collision Object.
We'll define the type of the collision object and add a basic shape:
-
Click on the "collisionobject" item in the Outline view to show its properties.
-
In the Properties view, set the Type property to "Kinematic" (or "Trigger", in our case it doesn't matter much, as we only need to detect overlapping). This means that the collision object will follow the game object it belongs to.
-
Right click the "collisionobject" in the Outline view and select Add Shape ▸ Sphere. Any shape(s) you add to the collision object defines its boundary as far as collisions are concerned.
-
Select the Scale Tool in the toolbar (shortcut R) and use the scale handle (2) to resize the sphere in the scene view until it reasonably covers the star. You can also edit the Diameter property directly in the Properties view.
-
Right click the root "Game Object" item in the Outline view again and select Add Component File, then select the script file "bonus_star.script". This script moves the bonus stars and make sure the player gets the right amount of points for catching them.
The bonus star game object file that you have now created contains the blueprint for a game object containing graphics (the sprite), collision shapes (the collision object) and logic that moves the star and reacts to collisions (the script).
Factory Components are responsible for making sure game objects of various kind appear in the game. For your new bonus stars, you need to create a factory:
-
Open the file "factory.go" with File ▸ Open Assets.... This game object file contains a script and a factory.
-
Add a secondary factory component to it. Right click the root "Game Object" item in the Outline view and select Add Component ▸ Factory.
-
Set the new factory component's Id property to "bonus_factory".
-
Set its Prototype property to "bonus_star.go" with the browse-button (...).
The last step is to make sure the factory game object starts creating the bonus stars by modifying its script.
-
Open factory.script with File ▸ Open Assets... or from the Assets.
-
Near the bottom of the file, uncomment the line:
-- component = "#bonus_factory"to:
component = "#bonus_factory"
This causes the bonus stars to appear roughly 20% of the time.
-
Save everything and restart the game by closing the window (or press Escape – assuming you didn't disabled the "Escape Quits Game" option in File ▸ Preferences), then select Project ▸ Build from the editor menu or shortcut Ctrl+B (Cmd+B on Mac).
The new bonus stars will start to appear!
You Win!
Now go ahead and create more games in Defold!
Check out the documentation pages for examples, tutorials, manuals and API docs.
If you run into trouble, help is available in our forum.
Happy Defolding!


