Sitemap
Glide Engine

Glide Engine

glide is a new open source game engine based on #SpriteKit for building 2d games. glide is written in #Swift. Join glide on GitHub to collaborate 👾👾

Glide engine — Beginner tutorial

16 min readJul 22, 2019

--

Welcome to very first medium tutorial of Glide. In this tutorial, you will be able to create a basic 2d platformer game that will get you introduced to concepts such as character animation, player controls and Glide’s custom collisions. Although I call it a basic game, it’s really the foundation of making an advanced side scroller and with Glide doing this becomes much easier. As you’ll see in the course of the tutorial, Glide provides a wide variety of components that you can use out of the box.

Character and platform created with Glide engine.

First, I’d like to give an introduction on Glide. Glide is a brand new open source game engine for building 2d games. Glide is written in Swift and it is based on SpriteKit and GameplayKit of Apple. It helps you build advanced 2d platformer games for Apple platforms such as iOS, macOS and tvOS.

It’s on GitHub, you can check out the source code here, and you are more than welcome to contribute. Definitely check out the demo project by downloading the macOS build over here to see all the supported features of the engine.

Press enter or click to view image in full size

This tutorial is so easy and brief that you can easily follow without prior knowledge of game development. Don’t be concerned about the scrolling height of this medium article, it is mostly screenshots for the project setup parts, really 😇

If you are new to Apple’s platforms and Swift, that is totally fine as well, start by downloading Xcode over here. If you are familiar with Xcode, you will probably be going through the first parts of this tutorial blazingly fast 🚀 You can also directly jump to Part 3, after downloading Part 2 project files from here.

If you get stuck with any of the different parts in this tutorial, you can download project files for each of them from the companion GitHub repo’s branches here. For example, part2 branch includes project files that you should have after completing Part 2.

That is the text version of Glide’s video tutorial published here. 📺 Feel free to follow that instead if you feel more comfortable with video tutorials.

Part 1: Create an Xcode project

In this part we will create an empty Xcode project with targets for iOS, macOS and tvOS.

Here are the steps at a glance:

  1. Create an empty cross platform Xcode project.
  2. Add targets for iOS, macOS and tvOS platforms.

Let’s get into more detail with some screenshots.

Step 1: Create an empty cross platform Xcode project

Create an empty cross platform project. You can name your project glideTutorialCharacterPlatform if you want to be aligned with project files.

Press enter or click to view image in full size
Creating an empty cross-platform project on Xcode.

You could also create a cross platform game project, but you won’t need most of the stuff in this template, so this empty target will be sufficient.

Step 2: Add targets for iOS, macOS and tvOS platforms

We will make our game cross platform, as supporting multiple platforms at once with Glide is super handy.

Add a Single View App iOS target to your project and name it TutorialiOS. Similarly add a Cocoa App macOS target and a Single View App tvOS target.

Press enter or click to view image in full size
Adding an iOS target and configuring it.
Press enter or click to view image in full size
Adding a macOS target and configuring it.
Press enter or click to view image in full size
Adding a tvOS target and configuring it.

Congratulations! Now you completed the first part with a basic project. 🎉

Download Part 1 project files.

Part 2: Project setup

In this part we will make our project structure ready for the rest of the tutorial and integrate it with Glide engine cocoa pod 💃

Here are the steps of this part at a glance:

  1. Create a shared folder and shared assets folder in the project.
  2. Download and import graphics assets used in this tutorial.
  3. Configure CocoaPods.

Here are more details on each step:

Step 1: Create a shared folder and shared assets folder to the project

First, create a shared folder that will contain the code and assets that will be used by all our platform targets we added in the previous part.

Press enter or click to view image in full size
Adding a shared folder to the project.

Now, create an assets folder under the shared folder you just created. Don’t forget to check all your targets.

Press enter or click to view image in full size
Adding a shared assets folder.

Step 2: Download and import graphics assets used in this tutorial

Download the graphics assets used in this tutorial from here. Then, import them to the shared assets folder you just created.

Press enter or click to view image in full size
Importing graphics assets.

Step 3: Configure CocoaPods

Open a Terminal window on your Mac. Navigate to the folder that contains your Xcode project. Then create a template podfile from Terminal using the following command. For that you’ll need to have CocoaPods installed on your Mac, read about instructions here.

Then you need to specify in the podfile you just created that you want to import GlideEngine cocoapod. Below is the final version of the podfile for this project. You can open your podfile in your favorite text editor and replace its contents with below. This tutorial is based on GlideEngine 1.0.3 pod.

Then, install the pods you specified with the following command from Terminal.

After the installation is done, close your Xcode project window if it is open. Then navigate to the folder that contains your project from Finder and open the .xcworkspace file created after pod install.

Press enter or click to view image in full size

At this stage, your project is ready to use the goodies from Glide engine 👯‍♀️

Download Part 2 project files.

Part 3: Setup view hierarchy

In this part, we will start implementing our cross platform view controller which will contain our game scene later on.

Here’s all the steps of this part:

  1. Create a cross platform GameViewController.
  2. Update storyboard files with GameViewController.
  3. Update project and window settings.

Step 1: Create a cross platform GameViewController

Create a Swift file called GameViewController under Shared folder. Add it for all of your targets.

Press enter or click to view image in full size
Adding a cross platform view controller.

Import GlideEngine pod and SpriteKit framework in your view controller. Below is the initial contents of your view controller. Note that, ViewControllerType is the cross platform view controller class defined in Glide engine.

Now, delete the template ViewController.swift files from each of your targets in your project, since our GameViewController will replace them.

Press enter or click to view image in full size
Deleting template view controllers.

Step 2: Update storyboard files with GameViewController

Change the view controller types to our new GameViewController in storyboard files of each target. Below is a screenshot from the storyboard of iOS target, don’t forget to do this for macOS and tvOS storyboard files.

Press enter or click to view image in full size
Updating view controller type for iOS storyboard.

Now change the view types of your storyboard view controller to SKView. SKView is the view type defined in SpriteKit that can present a game scene. Don’t forget to do it for macOS and tvOS storyboard files as well.

Press enter or click to view image in full size
Updating view type to SKView for iOS storyboard.

Step 3: Update project and window settings

Change the orientation and status bar settings for the iOS target, which is more suitable for a game. That’s basically locking the device orientation and hiding the status bar. It’s a good practice to get familiar with those settings, although they are not really required to complete this tutorial.

Press enter or click to view image in full size
Configuring iOS target.

Then, adjust the window size of macOS target to be in 4:3 format, in this case 1024x768. This is just to make it easier to view the examples in this tutorial.

Press enter or click to view image in full size
Adjusting window size for macOS target.

Congratulations! We have one more basic stuff to do! 👀

Download Part 3 project files.

Part 4: Present your first scene

In this part, we will present an almost empty game scene, which will contain our game elements later on.

Here are the steps we will follow:

  1. Create your first game scene.
  2. Present your game scene.
  3. Check your scene.

Step 1: Create your first game scene

For that, Create a Scene.swift file under Shared folder. Below are the initial contents of this file.

Notice that our Scene class extends from GlideScene, which is based on SKScene of SpriteKit. That is the file where we will configure our game objects, such as characters, enemies or platforms, in order to present them in our SKView that we configured in previous part.

On line 11, we assign a brown background colour to make things easier to see.

Step 2: Present your game scene

The place we will present our newly created game scene is GameViewController that we created in the previous part. Since this view controller displays a SKView, it has the ability to present game scenes.

Below is what GameViewController will look like with the code for presenting your scene.

Now, run your project on macOS scheme and have a look at your first Glide scene. You can also run your project on iOS and tvOS schemes to check. However, this tutorial will be focused mainly on macOS for the sake of simplicity.

Press enter or click to view image in full size
macOS and iOS targets displaying empty game scene.

Congratulations! Now it’s time to dive into some action! 🏃‍♂️

Download Part 4 project files.

Part 5: Add a platform to your scene

In this part, we will add our platform with its very basic properties to our scene.

This part consists of only one step.

As I mentioned earlier Scene.swift is the place of configuring and adding your game objects, or entities, as they called in GameplayKit and Glide engine. Open your Scene.swift file and add the below function that will create a platform entity.

Notice that, we are using the texture that we added in part 2 of this tutorial.

Press enter or click to view image in full size
Asset for platform entity.

SpriteNodeComponent is the component used to render a texture for an entity. Glide uses entity-component-system architecture based on GameplayKit in its implementation. That greatly simplifies the management of complex entities as your game naturally becomes more complex. You can read more about this architectural pattern here on Wikipedia and here on Apple’s documentation.

Now, we can call this method to create a platform entity and add it to our scene. Add the below code snippet to the end of setupScene() method of your Scene.swift file.

Run the project on different schemes of your project and you should be able to see the platform we just added.

Press enter or click to view image in full size
Platform entity as it appears on different targets.

Another part is gone! Things are starting to get real now! 🧱

Download Part 5 project files.

Part 6: Add a character to your scene

In this part, we will add our character with its very basic properties in our scene.

Start by creating a CharacterEntity.swift file under the Shared folder.

Notice that, we are creating a separate class for the new character entity, unlike the platform entity that was created in a function in our Scene in the previous part.

That is to provide better encapsulation and show you different ways of creating entities. In the end, our character entity will have more complicated contents than our platform entity, so it’s a better idea to have a separate entity class for it.

Implement CharacterEntity class which extends from GlideEntity, which is Glide’s generic entity class.

Override the setup method and add a SpriteNodeComponent to your entity.

Notice that, this time we are using a texture called character_walk_0 that we added in Part 2 of this tutorial.

Asset for character entity.

Now go back to your Scene.swift file and add your new entity to the scene by adding the below snippet at the bottom of the setupScene() method. That is after adding the platform entity.

Run the project and observe that our character is added to the scene.

Press enter or click to view image in full size
Character entity as it appears on different targets.

Great and congratulations! We just need to give some life to our character now 🤗

Download Part 6 project files.

Part 7: Make the platform and character collide

At the end of this part, our character and platform will interact with each other and collide as a result. 💥 (nothing explosive though, yet, not in this tutorial 👀)

Get Glide Engine’s stories in your inbox

Join Medium for free to get updates from this writer.

Steps of this part:

  1. Make our character entity react to physics forces.
  2. Make our character and platform collidable entities.
  3. Implement snapping behaviour.

Step 1: Make our character entity react to physics forces

First, we want to make our character be affected by some physics forces, namely fall towards below the screen with the gravity force.

For this, Glide engine has a component called KinematicsBodyComponent.

KinematicsBodyComponent’s purpose is to control an entity’s horizontal and vertical velocity based on given parameters. Gravity is one of those external parameters.

So start by creating KinematicsBodyComponent and adding it to our entity. Add the below snippet inside the setup() method in your CharacterEntity.swift file.

Then, run the project and observe that our character falls downwards.

Step 2: Make our character and platform collidable entities

Although our character can fall now, it still doesn’t collide with our platform. To fix this, create and add collider components to both the character and the platform.

Add the below snippet to the bottom of setup() method of our CharacterEntity.

Then add the below snippet to platformEntity(at:) method of Scene.swift file, after adding its SpriteNodeComponent.

You can ignore the many parameters of collider component in this tutorial or have a look at the documentation if you are curious about them. 🕵🏻‍♀️

It’s good to note that we are using a collider size which is slightly smaller than the size of an entity’s texture graphics image size in points. Character’s texture size is 120x165 points, whereas its collider size is 100x135. The reason for that is to have a smoother visual look when the entity is colliding with other objects, to excluded rounded corners or curves of a texture.

Now, run the project and observe that the collision still doesn’t occur. 😵

Step 3: Snapping behaviour

Glide doesn’t provide collision between two moving entities by default, since those moving entities can both be moving simultaneously, which is a complicated collision scenario to handle.

Instead, glide has a special behaviour called snapping, where one entity becomes snapper, while the other is snappable.

This gives priority of movement to the snappable object over the snapper and makes collision between them less complicated to resolve.

That said, add a snappable component to the platform and a snapper component to the character.

Add the below snippet to the bottom of setup() method of our CharacterEntity.

Then add the below snippet to platformEntity(at:) method of Scene.swift file, after adding its ColliderComponent.

Ignore the providesOneWayCollision parameter for the sake of simplicity in this beginner tutorial.

Now, run the project and observe that the character will “snap” on the platform. 😅

Press enter or click to view image in full size
Character falling and snapping on the platform.

Thank you for staying with me until here! Keep on going for the real fun parts 🕹

Download Part 7 project files.

Part 8: Make your character walk

At the end of this part, you’ll be able to make your character walk using the keyboard on your Mac ⌨️

Steps for this part:

  1. Make your character be able to move horizontally.
  2. Make your character playable.

Step 1: Make your character be able to move horizontally

Open your character entity class and add a HorizontalMovementComponent to the character.

HorizontalMovementComponent is the component that is used to move its entity’s kinematics body in the horizontal axis with the given movement style. This component comes with some default configuration values such as speed and acceleration.

The way to override those default configuration values is to provide a modified version of this component’s static configuration.

We want our character to have a fixed velocity of 30 m/s while moving horizontally. For that, copy and modify the shared configuration of the component and give it back to the component. Place the below snippet in the place of the previous snippet to be able to configure this component.

Run the project and try to move the character with A and D keys on your keyboard. You’ll see that is not possible because we need to do one more thing. 🤡✌️

Step 2: Make your character playable

The solution is to add a PlayableCharacterComponent to our character.

Add the below snippet at the bottom of setup() method of your CharacterEntity.

PlayableCharacterComponent is the component that coordinates the transfer of input profile values to an entity’s different ability components based on that entity’s player index.

This is the component which connects a character’s different components with Glide’s input handler.

Run the project on macOS and give A and D keys another go. Now, you should be able to move your character horizontally. 🤩

Character moving with keyboard input 🙌

Notice that you can’t control the character on iOS, because you need to add touch controls for this. That is covered in another tutorial.

Also, in order to control with Apple Remote of Apple TV, you should run the project on a tvOS device rather than a simulator.

All in all it’s great that you’ve come this far 🙏 Go on for some animations 🎬

Download Part 8 project files.

Part 9: Add a walking animation

In this part, we will play a walking animation on our character’s sprite while it is moving horizontally.

Steps of this part go like this:

  1. Create an idle animation.
  2. Create an animator and a walking animation.
  3. Create a custom component to switch between animations.
  4. Polish the animator.

Step 1: Create an idle animation

First, add a separate method for setting up the animations in CharacterEntity for the sake of clarity.

Name this function setupTextureAnimations() that takes no parameters and call it from the end of your setup() method within your CharacterEntity.swift.

Now, create a texture animation with id Idle and an action, as shown below. Add it inside your new setupTextureAnimations(). For now, you can see those as wrappers around regular SpriteKit actions, as basically they are.

We want to display this Idle animation when the character is not moving. In this case, this animation consists of only 1 frame, which actually represents a single static texture, character_walk_0

Step 2: Create an animator and a walking animation

Now create a TextureAnimatorComponent and add it to your character. Do this at the end of your setupTextureAnimations(). This component will manage the switching between Idle animation we just created and Walk animation that we will create next.

Now create another action with a trigger name Walk. This is an actual animation with 3 frames of walking that we added in Part 2. You can add this to the end of your setupTextureAnimations() method.

Don’t forget to add this animation to our TextureAnimatorComponent as well.

Step 3: Create a custom component to switch between animations

Now we will create our first custom GlideComponent called CharacterComponent. We will be using this component to control our TextureAnimatorComponent depending on the state of our HorizontalMovementComponent.

At the end of your CharacterEntity.swift file, create a class that extends from GKComponent and adapts GlideComponent protocol. Call this class CharacterComponent.

We will implement the code that will switch between our animations by overriding the didUpdate method defined in GlideComponent protocol. That method is called after all the components of an entity is updated.

We check our HorizontalMovementComponent’s state and enable triggers of respective animations on our TextureAnimatorComponent depending on that state.

Add the below snippet inside your CharacterComponent.

Now we need to add our custom CharacterComponent to our CharacterEntity in its setup method. You can do that at the bottom of setup() method of character entity as usual.

Now run the project and have a look at the walking animation of our character.

Notice that texture animator component also automatically flips the character textures in respect to the movement direction 🥳

Character animations for moving.

Step 4: Polish the animator

Observe that our character sticks a bit inside our platform, now we will fix that. 🛀

Open the setupTextureAnimations() method in our character entity class where we setup the animator component.

We will add a vertical offset value to our animator, which is responsible for displaying our textures. That offset value is half of the difference between our enitity’s collider box height and sprite texture height.

(165–135) / 2 = 15

That will move our sprite(texture) node 15 pixels up, but leave the collider box unchanged.

Replace the below snippet instead of where you create your idle animation. Notice the use of animationOffset parameter for the idleAnimation object.

Don’t forget to use this same offset value for the walking animation as well.

Run the project again and observe that the character doesn’t stick to the platform anymore. 🎊

Character animations for moving with fixed texture offset.

Impressive! Only one part left to the finish line for you 🏁

Download Part 9 project files.

Part 10: Make the character jump

At the end of this part, your character will be able to jump with player input!

As always, Glide’s wide variety of components are our friends. Create and add a JumpComponent to your entity. Do that in the setup() method of your CharacterEntity.

JumpComponent is the component that makes an entity gain a momentary vertical speed while on ground.

Run the project and make the character jump with Space key on your keyboard.

Notice that the character can jump, but actually not that far. Let’s increase the amount of jumping distance.

Similar to what we did with HorizontalMovementComponent in the previous part, we will create a modified JumpComponent configuration and assign a different jumping velocity to it.

Replace below snippet with the last snippet you added for creating your jump component.

Run the project again and notice that although we increased the jumping velocity from 13(default) to 30, jumping distance didn’t increase much. 🙇‍♀️

That’s because KinematicsBodyComponent has a maximum vertical velocity that needs to be updated as well. Let’s change it from 20(default) to 30. 💡

Find in your CharacterEntity.swift’s setup() method where you create and add a KinematicsBodyComponent and replace it with below snippet.

Run the project and notice that your character can jump to a higher position now.

Character jumping 🧹

Download Part 10 (Final) project files

🥳 Congratulations and many thanks for finishing this tutorial. I really hope you enjoyed it.

Remember that Glide is open source and it is always available for new features, fixes or improvements from different contributors.

Please let me know if you have any questions about this tutorial or Glide in general. Your feedback is also highly appreciated.

Follow me on Twitter and GitHub for new and upcoming tutorials. 🙏✌️

--

Glide Engine
Glide Engine

Written by Glide Engine

glide is a new open source game engine based on #SpriteKit for building 2d games. glide is written in #Swift. Join glide on GitHub to collaborate 👾👾