Last active
August 29, 2015 14:19
-
-
Save efrenjgb/70fd76890a8b375e9efd to your computer and use it in GitHub Desktop.
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
// | |
// GameScene.swift | |
// spaceshooter | |
// | |
// | |
import SpriteKit | |
class GameScene: SKScene { | |
var starShip:SKSpriteNode! | |
var timeSinceLastFire:CGFloat = 0.0 | |
var fireWeapon:Bool = false | |
var lastUpdateTime:NSTimeInterval = 0 | |
var delta:NSTimeInterval = NSTimeInterval(0) | |
override func didMoveToView(view: SKView) { | |
/* Setup your scene here */ | |
self.backgroundColor = SKColor.blackColor() | |
let starFieldEmmiter = SKEmitterNode(fileNamed: "StarField.sks") | |
starFieldEmmiter.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame)) | |
starFieldEmmiter.zPosition = -20 | |
starFieldEmmiter.advanceSimulationTime(NSTimeInterval(starFieldEmmiter.particleLifetime)) | |
self.addChild(starFieldEmmiter) | |
starShip = SKSpriteNode(imageNamed:"Spaceship") | |
starShip.xScale = 0.2 | |
starShip.yScale = 0.2 | |
starShip.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + 100) | |
self.addChild(starShip) | |
} | |
func moveShipTo(location:CGPoint) | |
{ | |
starShip.runAction(SKAction.moveTo(location, duration: 0.05)) | |
} | |
func fireProjectile(delta:CGFloat) { | |
if timeSinceLastFire >= 0.15 | |
{ | |
let projectile = SKSpriteNode(imageNamed:"laser1") | |
projectile.xScale = 0.5 | |
projectile.yScale = 0.5 | |
projectile.position = starShip.position | |
projectile.position.y += starShip.frame.height / 2 | |
projectile.zPosition = starShip.zPosition + 1 | |
let speed:CGFloat = 600.0 | |
let distance = CGRectGetMaxY(self.frame) - starShip.position.y | |
let travelTime:NSTimeInterval = NSTimeInterval(distance / speed) | |
projectile.runAction(SKAction.moveToY(CGRectGetMaxY(self.frame) + 100, duration: travelTime)) | |
let waitAction = SKAction.waitForDuration(travelTime) | |
let fadeAction = SKAction.fadeOutWithDuration(1) | |
let removeAction = SKAction.removeFromParent() | |
let sequence = [waitAction, fadeAction, removeAction] | |
self.addChild(projectile) | |
projectile.runAction(SKAction.sequence(sequence)) | |
timeSinceLastFire = 0.0 | |
} | |
timeSinceLastFire += delta | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
/* Called when a touch begins */ | |
for touch in (touches as! Set<UITouch>) { | |
let location = touch.locationInNode(self) | |
moveShipTo(location) | |
self.fireWeapon = true | |
} | |
} | |
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { | |
for touch in (touches as! Set<UITouch>) { | |
let location = touch.locationInNode(self) | |
moveShipTo(location) | |
} | |
} | |
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { | |
for touch in (touches as! Set<UITouch>) { | |
let location = touch.locationInNode(self) | |
self.fireWeapon = false | |
} | |
} | |
override func update(currentTime: CFTimeInterval) { | |
/* Called before each frame is rendered */ | |
if lastUpdateTime == 0.0 { | |
delta = 0 | |
} else { | |
delta = currentTime - lastUpdateTime | |
} | |
lastUpdateTime = currentTime | |
if fireWeapon | |
{ | |
self.fireProjectile(CGFloat(delta)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment