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 trialLászló Győri
82,430 PointsSpaceCat 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.
László Győri
82,430 PointsHi!
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.
Carlos ambrosio
Courses Plus Student 618 PointsHi 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
82,430 PointsHi!
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.
Carlos ambrosio
Courses Plus Student 618 PointsSorry 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
82,430 PointsJust simply create a new instance instead like: ``` let hud = HudNode()
Carlos ambrosio
Courses Plus Student 618 PointsHEY! 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
82,430 PointsDedication :) (also I'm a professional developer for 9 years now)
Carlos ambrosio
Courses Plus Student 618 PointsWell 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
Treehouse TeacherGreat idea, and thanks so much for sharing! :) cc Amit Bijlani and Pasan Premaratne
Amit Bijlani
Treehouse Guest TeacherGreat job and thank you for sharing! This will really benefit students that want to start learning Swift.
Carlos ambrosio
Courses Plus Student 618 PointsCarlos ambrosio
Courses Plus Student 618 PointsHi 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.