Now that you have a basic understanding of the Swift language, let's take a look at how it is used in Cocos2d.
Creating a Cocos2d-Swift project is very easy. While creating a project, you have to specify the language used as Swift instead of Obj-C while creating a project using SpriteBuilder, as follows:
You will see that the project structure is different now. Instead of the MainScene.h
and MainScene.m
files, we just have a MainScene.swift
file.
Now, to load the MainScene.swift
file, we have to make some changes to the AppDelegate.m
file, just as we did for Obj-C projects.
In the AppDelegate.m
file, we will add an import at the top. This should be projectName-Swift.h
; so, in my case, as the name of the project is test
, the header is test-Swift.h
. Take a look at the following:
#import "test-Swift.h"
Now, in the startScene
function, we will edit it as follows so that it returns the Swift MainScene
class:
- (CCScene*) startScene { //return...