Skip to content

Instantly share code, notes, and snippets.

@jeonghopark
Last active August 29, 2015 14:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeonghopark/06cb7785cdb8ddd1b106 to your computer and use it in GitHub Desktop.
Save jeonghopark/06cb7785cdb8ddd1b106 to your computer and use it in GitHub Desktop.
Processing Style Test - Swift
import Cocoa
import SpriteKit
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var window: NSWindow
@IBOutlet var skView: SKView
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let scene = GameScene(size:self.skView.bounds.size)
scene.scaleMode = .AspectFill
self.skView!.presentScene(scene)
}
func applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication) -> Bool {
return true;
}
}
//Source : http://www.gamefromscratch.com/post/2014/06/13/Game-development-tutorial-Swift-and-SpriteKit-Part-2-Using-Sprites-and-by-default-SKNodes.aspx
import SpriteKit
class GameScene: SKScene {
var shipSpeed = CGFloat()
let sprite = SKSpriteNode(imageNamed: "sprite1")
override func didMoveToView(view: SKView) {
let backGroundSprite = SKSpriteNode(imageNamed: "background")
backGroundSprite.position = CGPoint(x:0,y:0)
backGroundSprite.anchorPoint = CGPoint(x:0.0,y:0.0)
backGroundSprite.size = view.bounds.size
self.addChild(backGroundSprite)
sprite.anchorPoint = CGPoint(x:0.5,y:0.5)
sprite.position.x = 100;
sprite.position.y = 100;
sprite.xScale = 1
sprite.yScale = 1
self.addChild(sprite)
var shapeArr : Array<SKNode> = []
for index in 0...5 {
var shapeE = RectE().simpleView(CGFloat(index)*100)
shapeArr.append(shapeE)
self.addChild(shapeArr[index])
}
}
override func update(currentTime: NSTimeInterval) {
if ((sprite.position.x>0)&&(sprite.position.x < view.bounds.width)) {
shipSpeed = 5
} else {
shipSpeed = 0
sprite.position.x = 0
}
sprite.position.x += shipSpeed
}
override func mouseDown(theEvent: NSEvent!) {
self.sprite.position = CGPoint(x:theEvent.locationInWindow.x,y:theEvent.locationInWindow.y)
}
}
class RectE: SKScene {
let shapeEle = SKSpriteNode()
func simpleView(_x: CGFloat) -> SKNode {
shapeEle.color = SKColor.redColor()
shapeEle.position = CGPointMake(_x, 400)
shapeEle.size = CGSizeMake(50, 50)
return shapeEle
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment