Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

László Győri
László Győri
82,430 Points

SpaceCat in Swift

While taking the course 'Build a Game with Sprite Kit' I've decided that I would follow along using Swift instead of Objective-C, since I'm very focused on learning that. Today I've completed the course, and thought I'd share the final version.

https://github.com/szukuro/spacecat-swift

I wanted to complete it sooner, but was stuck on a bug with beta3+ and texture atlases. You can find a detailed description on Github.

I hope you find it useful and have as much fun with it as I did.

Hi László , thank you for the code, but i have some problems running the code, i was wondering if you can update the code. because i can't follow the course using swift. my abilities are not so good. so, your code will help me so much. Thank you.

László Győri
László Győri
82,430 Points

Hi!

Yes, there have been some changes in the language and SDK since then that break the code, most notably the way enums are handled as well as physicsBody now being an optional. I've updated the code on the newfeatures branch (which is the original code with some additions), you can check that, and accomodate the changes. Can't promise to update the main version though, it's a bit behind.

Hi man, just a little favor, can you check this function.

func moveTowarsPosition(position:CGPoint){ // slope = (y3 - y1) / (x3 - x1) var slope = (position.y - self.position.y) / (position.x - self.position.x)

    var offscreenX:Float


    if (position.x <= self.position.x) {
        offscreenX = -10
    } else {

        offscreenX = self.parent.frame.size.width + 10
    }

   let offscreenY = slope * offscreenX - slope * self.position.x + self.position.y
    let offscreenY = slope * offscreenX - slope * self.position.x + self.position.y

    let pointOffScreen = CGPointMake(offscreenX, offscreenY)

    let distanceA = pointOffScreen.y - self.position.y
    let distanceB = pointOffScreen.x - self.position.x

    let distanceC = sqrt(pow(distanceA, 2) + pow(distanceB, 2))


    //distance = speed * time
    // time = distance / speed
    var time:Float = distanceC / 100


    let moveProjectile = SKAction.moveTo(pointOffScreen, duration: NSTimeInterval(time))
    self.runAction(moveProjectile)



}

im having trouble and the problem says "SKNode doesn't have a member named 'frame'" im lost u.U sorry to butter you

László Győri
László Győri
82,430 Points

Hi!

The parent property is also an optional now, so you have to unwrap it:

offscreenX = self.parent!.frame.size.width + 10

Note the exclamation mark (!) after parent.

Sorry to butter you again Lászsió, but i am almost finish the game and im having some trouble at the end. this line. ("Self.node() as HudNode")... is when adding the HUD...

class HudNode: SKNode {

var lives : NSInteger = MaxLives
var score : NSInteger = 0

}

extension HudNode { class func hudAtPosition(position:CGPoint, frame:CGRect) -> HudNode { let hud = self.node() as HudNode

it says node() is unavailable use object construction SKNode.

Again Thank you so much.

László Győri
László Győri
82,430 Points

Just simply create a new instance instead like: ``` let hud = HudNode()

HEY! I DON´T KNOW HOW TO THANK YOU! it works!!

Thank you man!!! ur awesome ;)!

how do you learn so much about all of this?

László Győri
László Győri
82,430 Points

Dedication :) (also I'm a professional developer for 9 years now)

Well its a pleasure to meet you. im just starting on programming. and thanks for all ur help. are you nick walter?

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Great idea, and thanks so much for sharing! :) cc Amit Bijlani and Pasan Premaratne

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Great job and thank you for sharing! This will really benefit students that want to start learning Swift.