0% found this document useful (0 votes)
32 views

Createing A Character (Hero) in Hero - Swift: Import Import Class Var Required Init

1. The documents describe the creation of a hero character and game scene in SpriteKit for an iOS game. Code is provided to initialize the hero, add it to the game scene, and enable movement through swipe gestures. 2. Additional code is shown for adding animation to the hero and implementing basic physics for movement and collisions. 3. Body types and collision/contact masks are defined to handle interactions between the hero and different game elements.

Uploaded by

Jermaine Duncan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Createing A Character (Hero) in Hero - Swift: Import Import Class Var Required Init

1. The documents describe the creation of a hero character and game scene in SpriteKit for an iOS game. Code is provided to initialize the hero, add it to the game scene, and enable movement through swipe gestures. 2. Additional code is shown for adding animation to the hero and implementing basic physics for movement and collisions. 3. Body types and collision/contact masks are defined to handle interactions between the hero and different game elements.

Uploaded by

Jermaine Duncan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

Createing a character (hero) in

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!)
}
}
_______________________________________________________________________________

Game scene start


import SpriteKit
class GameScene: SKScene {
var currentSpeed:Float = 5
var heroLocation:CGPoint = CGPointZero
var mazeWorld:SKNode?
var hero:Hero?
override func didMoveToView(view: SKView) {
/* Setup your scene here */
mazeWorld = childNodeWithName("mazeWorld")
heroLocation = mazeWorld!.childNodeWithName("StartingPoint")!.position
hero = Hero()
hero!.position = heroLocation
mazeWorld!.addChild(hero!)

}
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 */
}

}
_____________________________________________________________________________

Adding swipe gestures


hero.swift

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

self.physicsBody = SKPhysicsBody(rectangleOfSize: objectSprite!.size)


self.physicsBody!.friction = 0
self.physicsBody!.dynamic = true // keeps object in walls
self.physicsBody!.restitution = 0 //bouncy
self.physicsBody!.allowsRotation = false //rotaion
self.physicsBody!.categoryBitMask = BodyType.hero.rawValue
self.physicsBody!.contactTestBitMask = BodyType.boundary.rawValue |
BodyType.star.rawValue
self.physicsBody!.collisionBitMask = 0

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)
_____________________________________________________________________________

You might also like