-
1. Create the following files in Photoshop:
background.png (320 x 568)background@2x.png (640 x 1136)blue.png (20 x20)blue@2x.png (40 x 40)gameboard.png (200 x 400)gameboard@2x.png (400 x 800)icon-29pt.png (29 x 29)icon-29pt@2x.png (58 x 58)icon-29pt@3x.png (87 x 87)icon-40pt.png (40 x 40)icon-40pt@2x.png (80 x 80)icon-40pt@3x.png (120 x 120)icon-60pt.png (60 x 60)icon-60pt@2x.png (120 x 120)icon-60pt@3x.png (180 x 180)icon-76pt.png (76 x 76)icon-76@2xpt.png (152 x 152)icon-83_5pt.png (167 x 167)orange.png (20 x20)orange@2x.png (40 x40)purple.png (20 x20)purple@2x.png (40 x40)red.png (20 x20)red@2x.png (40 x40)teal.png (20 x20)teal@2x.png (40 x40)whitebg.png (84 x 100)white@2x.png (168 x 200)yellow.png (20 x20)yellow@2x.png (40 x40)2. Let's create a new project for a game "Blocktris."Begin by creating a new game project in Xcode:- Click Create a new Xcode project from the Welcome screen:
Or
- Select File > New > Project… from the file menu:
3. When the new project window appears, choose Game from under the iOS > Application category and press Next.The next window beckons you to customize options for your project. Fill out the fields described in the table below:
Product Name: BlocktrisOrganization Name: NeshaminyOrganization Identifier: org.NeshaminyLanguage: SwiftGame Technology: SpriteKitDevices: iPhoneSwift is Apple's latest programming language.
SpriteKit is a set of APIs provided by the iOS SDK (software development kit) which allow native 2D game development from within Xcode.
4. Press Next and Xcode will ask where to place your new project (in a new folder on the desktop called Blocktris).
Check Create Git repository, and then click Create.5. After saving it should open Xcode to your brand new project, specifically to the project properties screen.
On this screen, under Device Orientation: uncheck landscape left & landscape right.Run the default game project by pressing ⌘ + R on your keyboard or by clicking the little play button in the top left corner.
6. Open Project Navigator by either clicking the folder icon (top left) or pressing ⌘ + 1:
Right-click GameScene.sks and choose the Delete option:
When asked to confirm, make sure to choose Move to trash:
7. To get rid of the aimless space ship once and for all, click the Assets.xcassets folder and highlight the Spaceship entry, press the delete key.
8. We must now purge our project of any and all code which we do not require. Delete the lines marked in red within their corresponding files:
In the GameScene.swift file delete the code highlighed in red:override func didMoveToView(view: SKView) {
/* Setup your scene here */
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 65;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let sprite = SKSpriteNode(imageNamed:"Spaceship")
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = location
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
sprite.runAction(SKAction.repeatActionForever(action))
self.addChild(sprite)
}
}
10. In the GameViewController.swift file, delete the following code highlighted in red:extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks")
var sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil)
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
archiver.finishDecoding()
return scene
}
}
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
11. Run the default game project by pressing ⌘ + R on your keyboard or by clicking the little play button in the top left corner.