Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Kyler Smith
Kyler Smith
10,110 Points

iOS SpriteKit game in Swift Guidance

I am doing the SpaceCat game in swift rather than obj-c and I need some help. Some problems I am running into include the absence of my spriteNode pictures (MachineNode and HeroNode) at random times. I also need help with a few concepts but one thing at a time.

//
//  HeroNode.swift
//  Space Cat Swift
//
//  Created by Kyler on 7/21/16.
//  Copyright © 2016 Kyler. All rights reserved.
//

import Foundation
import SpriteKit
import UIKit

struct HeroNode {

    let textures = [SKTexture(imageNamed: "spacecat_2"),
                    SKTexture(imageNamed: "spacecat_1")]

    func getHero(view: SKScene) -> SKSpriteNode {

        let hero = SKSpriteNode(imageNamed: "spacecat_1")
        hero.position = CGPoint(x: CGRectGetMidX(view.frame), y: 45)
        return hero
    }
}
//
//  GamePlayScene.swift
//  Space Cat Swift
//
//  Created by Kyler on 7/21/16.
//  Copyright © 2016 Kyler. All rights reserved.
//
import SpriteKit
import Foundation
import UIKit


struct MachineNode {

    let texturesArray = [SKTexture(imageNamed: "machine_1"),
                         SKTexture(imageNamed: "machine_2")]

    func getMachine(view: SKScene) -> SKSpriteNode {

        let machine = SKSpriteNode(imageNamed: "machine_1")

        machine.position = CGPoint(x: CGRectGetMidX(view.frame), y: 65)

        let machineAnimation = SKAction.animateWithTextures(self.texturesArray, timePerFrame: 0.4)

        let machineRepeat = SKAction.repeatActionForever(machineAnimation)

        machine.runAction(machineRepeat)

        return machine
    }
}
//
//  GameScene.swift
//  Space Cat Swift
//
//  Created by Kyler on 7/21/16.
//  Copyright (c) 2016 Kyler. All rights reserved.
//

import SpriteKit
import UIKit
import Foundation

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {

        let background = SKSpriteNode(imageNamed: "splash_1")
        background.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        background.size = CGSizeMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame))
        self.addChild(background)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let gamePlayScene = GamePlayScene(size: self.frame.size)
        let transition = SKTransition.crossFadeWithDuration(1)

        self.view?.presentScene(gamePlayScene, transition: transition)

    }

}
//
//  GamePlayScene.swift
//  Space Cat Swift
//
//  Created by Kyler on 7/21/16.
//  Copyright © 2016 Kyler. All rights reserved.
//
import SpriteKit
import UIKit
import Foundation

class GamePlayScene: SKScene {
    override func didMoveToView(view: SKView) {

        let background = SKSpriteNode(imageNamed: "background_1")
        background.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        background.size = CGSizeMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame))
        self.addChild(background)


        let machine: MachineNode = MachineNode()
        self.addChild(machine.getMachine(self))

        let spaceCat: HeroNode = HeroNode()
        self.addChild(spaceCat.getHero(self))

    }
}

Thank you!

Edward Laurenson
Edward Laurenson
4,278 Points

Hi I was wondering if you managed to finish this game using swift instead of objective C? Im also trying this course since I cant find another which uses swift.