Created
July 11, 2015 19:14
-
-
Save kognate/94e13d2bbb6a179764ac to your computer and use it in GitHub Desktop.
sprite kit ship turtle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SpriteKit | |
import XCPlayground | |
let view = SKView(frame: CGRectMake(0, 0, 300, 300)) | |
let scene = SKScene(size: view.frame.size) | |
view.presentScene(scene) | |
let ship = SKSpriteNode(imageNamed: "Spaceship.png") | |
ship.size = CGSizeMake(25, 25) | |
scene.addChild(ship) | |
XCPShowView("ship", view: view) | |
let midX = CGRectGetMidX(view.frame) | |
let midY = CGRectGetMidY(view.frame) | |
let deltaVec = CGVectorMake(midX, midY) | |
//ship.runAction(SKAction.moveBy(deltaVec, duration: 3)) | |
ship.position = CGPointMake(150, 150) | |
struct Path { | |
let penColor: SKColor | |
var penDown = false | |
let bezierPath: UIBezierPath = UIBezierPath() | |
let sprite: SKSpriteNode | |
let moveSpeed = 200 | |
func start() { | |
bezierPath.moveToPoint(CGPointZero) | |
} | |
func forward(distance: Double) { | |
let headingInRadians = Double(sprite.zRotation) | |
let dx = distance * cos(headingInRadians) | |
let dy = distance * sin(headingInRadians) | |
let point = CGPointMake(CGFloat(dx), CGFloat(dy)) | |
if penDown { | |
bezierPath.addLineToPoint(point) | |
} else { | |
bezierPath.moveToPoint(point) | |
} | |
} | |
func turn(angle: CGFloat) { | |
sprite.zRotation = angle | |
} | |
func startPen(color: SKColor) { | |
} | |
func stopPen() { | |
} | |
} | |
let p = Path(penColor: SKColor.whiteColor(), penDown: true, sprite: ship) | |
p.start() | |
p.forward(100) | |
p.turn(CGFloat(M_PI_4)) | |
p.forward(50) | |
p.turn(CGFloat(M_PI)) | |
p.forward(50) | |
let linen = SKShapeNode(path: p.bezierPath.CGPath) | |
linen.strokeColor = p.penColor | |
scene.addChild(linen) | |
linen.position = ship.position | |
ship.runAction(SKAction.followPath(p.bezierPath.CGPath, asOffset: true, orientToPath: true, duration: 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment