Createing A Character (Hero) in Hero - Swift: Import Import Class Var Required Init
Createing A Character (Hero) in Hero - Swift: Import Import Class Var Required Init
hero.swift
import Foundation
import SpriteKit
class Hero:SKNode {
var objectSprite:SKNode?
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been impemented")
}
override init () {
super.init()
println("hero was added")
objectSprite = SKSpriteNode(imageNamed: "hero")
addChild(objectSprite!)
}
}
_______________________________________________________________________________
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
_____________________________________________________________________________
func update(){
switch currentDirection {
case .Right:
self.position = CGPoint(x: self.position.x + CGFloat(currentSpeed), y:
self.position.y)
objectSprite!.zRotation = CGFloat(degreesToRadians(0))
case .Left:
self.position = CGPoint(x: self.position.x - CGFloat(currentSpeed), y:
self.position.y)
objectSprite!.zRotation = CGFloat(degreesToRadians(180))
case .Up:
self.position = CGPoint(x: self.position.x , y: self.position.y +
CGFloat(currentSpeed))
objectSprite!.zRotation = CGFloat(degreesToRadians(90))
case .Down:
self.position = CGPoint(x: self.position.x, y: self.position.y CGFloat(currentSpeed))
objectSprite!.zRotation = CGFloat(degreesToRadians(-90))
case .None:
self.position = CGPoint(x: self.position.x, y: self.position.y)
}
func degreesToRadians(degrees: Double) -> Double {
return degrees / 188 * Double(M_PI)
}
func goUp(){
currentDirection = .Up
}
func goDown(){
currentDirection = .Down
}
func goLeft(){
currentDirection = .Left
}
func goRight(){
currentDirection = .Right
}
Gamescene.swift
let waitAction:SKAction = SKAction.waitForDuration(0.5)
self.runAction(waitAction, completion: {
let swipeRight:UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: Selector("swipeRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: Selector("swipeLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target:
self, action: Selector("swipeUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
})
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
hero!.update()
}
func swipeRight(sender:UISwipeGestureRecognizer) {
hero!.goRight()
}
func swipeLeft(sender:UISwipeGestureRecognizer) {
hero!.goLeft()
}
func swipeUp(sender:UISwipeGestureRecognizer) {
hero!.goUp()
}
func swipeDown(sender:UISwipeGestureRecognizer) {
hero!.goDown()
}
}
___________________________________________________________________________
Adding animation
func setUpAnimation() {
let atlas = SKTextureAtlas(named: "moving")
let array:[String] = ["moving0001", "moving0002", "moving0003",
"moving0004", "moving0003", "moving0002"]
var atlasTextures:[SKTexture] = []
for (var i = 0; i < array.count; i++) {
let texture:SKTexture = atlas.textureNamed(array[i])
atlasTextures.insert (texture, atIndex:i)
}
let atlasAnimation = SKAction.animateWithTextures(atlasTextures,
timePerFrame: 1.0/30, resize: true, restore: false)
movingAnimation = SKAction.repeatActionForever(atlasAnimation)
}
func runAnimation() {
objectSprite!.runAction(movingAnimation)
func stopAnimation() {
objectSprite!.removeAllActions()
}
________________________________________________________________________________
phyisics
hero.swift
gamescene
import SpriteKit
enum BodyType:UInt32 {
case hero = 1
case boundary = 2
case sensorUp = 4
case sensorDown = 8
case sensorRight = 16
case sensorLeft = 32
case star = 64
case enemy = 128
case boundary2 = 256
}
view.showsPhysics = true
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
_____________________________________________________________________________