Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SpriteKit
- import UIKit
- import AVFoundation
- class GameScene: SKScene {
- var ninja = SKSpriteNode()
- var floorTexture = SKTexture()
- var wallDownTexture = SKTexture()
- var wallBreakTexture = SKTexture()
- var wallsMoveAndRemove = SKAction()
- let wallBreakName = "Wall3"
- let wallGap = arc4random_uniform(20) + 75
- override func didMoveToView(view: SKView) {
- /* Setup your scene here */
- //Swipe actions
- var downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
- downSwipe.direction = .Down
- view.addGestureRecognizer(downSwipe)
- var upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
- upSwipe.direction = .Up
- view.addGestureRecognizer(upSwipe)
- var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
- rightSwipe.direction = .Right
- view.addGestureRecognizer(rightSwipe)
- var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
- leftSwipe.direction = .Left
- view.addGestureRecognizer(leftSwipe)
- //Physics
- self.physicsWorld.gravity = CGVectorMake(0.0, -5.0);
- //Character
- var NinjaTexture = SKTexture(imageNamed:"ninja")
- NinjaTexture.filteringMode = SKTextureFilteringMode.Nearest
- ninja = SKSpriteNode(texture: NinjaTexture)
- ninja.setScale(0.5)
- ninja.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.2)
- ninja.physicsBody = SKPhysicsBody(circleOfRadius: ninja.size.height/2.0)
- ninja.physicsBody?.dynamic = true
- ninja.physicsBody?.allowsRotation = false
- self.addChild(ninja)
- //Background
- var backgroundTexture = SKTexture(imageNamed:"bg")
- var bg = SKSpriteNode(texture: backgroundTexture)
- bg.setScale(1.0)
- bg.position = CGPointMake(self.size.width/2, bg.size.height/2.0)
- bg.zPosition = -20
- self.addChild(bg)
- //Ground
- var groundTexture = SKTexture(imageNamed:"ground")
- var groundSprite = SKSpriteNode(texture: groundTexture)
- groundSprite.setScale(2.0)
- groundSprite.position = CGPointMake(self.size.width/2, groundSprite.size.height/2.0) // sprite height
- self.addChild(groundSprite)
- var ground = SKNode()
- ground.position = CGPointMake(0, groundTexture.size().height)
- ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0)) // 2.0 sets collision height
- ground.physicsBody?.dynamic = false
- self.addChild(ground)
- //Roof
- var roofTexture = SKTexture(imageNamed:"roof")
- var roofSprite = SKSpriteNode(texture: roofTexture)
- roofSprite.setScale(2.0)
- roofSprite.position = CGPointMake(self.size.width/2, roofSprite.size.height * 3.0) // sprite height
- self.addChild(roofSprite)
- //Walls
- //Create the walls
- floorTexture = SKTexture(imageNamed:"floor")
- wallDownTexture = SKTexture(imageNamed:"wall2")
- wallBreakTexture = SKTexture(imageNamed:"wall3")
- //Movement of walls
- let distanceToMove = CGFloat(self.frame.size.width + 3.0 * floorTexture.size().width) //3.0 changes length before removed
- let moveWalls = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.003 * distanceToMove)) //0.01 how far apart walls are
- let removeWalls = SKAction.removeFromParent()
- wallsMoveAndRemove = SKAction.sequence([moveWalls, removeWalls])
- //Spawn Walls
- let spawn = SKAction.runBlock({() in self.spawnWalls()})
- let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
- let spawnThenDelay = SKAction.sequence([spawn, delay])
- let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
- self.runAction(spawnThenDelayForever)
- //Test text
- var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
- label.center = CGPointMake(325, 35) // changes postion
- label.textAlignment = NSTextAlignment.Center
- label.textColor = UIColor.redColor()
- label.text = "Ninja"
- self.view?.addSubview(label)
- }
- func spawnWalls() {
- //Wall pair lines the centers up
- let wallPair = SKNode()
- wallPair.position = CGPointMake(self.frame.size.width + floorTexture.size().width * 2, 0)
- wallPair.zPosition = -10 // changes layer to back
- let y = arc4random_uniform(150) // randomises the height of the floor + walls
- let wallDown = SKSpriteNode(texture: wallDownTexture) //Declare wallDown
- wallDown.setScale(2.0) //Enlarge sprite
- wallDown.position = CGPointMake(0.0, CGFloat(y) + wallDown.size.height + CGFloat(wallGap))
- wallDown.physicsBody = SKPhysicsBody(rectangleOfSize:wallDown.size)
- wallDown.physicsBody?.dynamic = false
- wallPair.addChild(wallDown) //adds walldown to wallpair
- let wallUp = SKSpriteNode(texture: floorTexture)
- wallUp.setScale(2.0)
- wallUp.position = CGPointMake(0.0, CGFloat(y))
- wallUp.physicsBody = SKPhysicsBody(rectangleOfSize:wallUp.size)
- wallUp.physicsBody?.dynamic = false
- wallPair.addChild(wallUp) //adds wallup to wallpair
- wallPair.runAction(wallsMoveAndRemove) //wallsmoveandremove is a function that moves the walls across the screen and removes them once off screen
- self.addChild(wallPair)
- let wallBreak = SKSpriteNode(texture: wallBreakTexture)
- wallBreak.name = "wallBreakName"
- wallBreak.setScale(2.0)
- wallBreak.position = CGPointMake(0.0, CGFloat(y))
- wallBreak.physicsBody = SKPhysicsBody(rectangleOfSize:wallBreak.size)
- wallBreak.physicsBody?.dynamic = false
- wallPair.addChild(wallBreak)
- self.addChild(wallBreak)
- }
- //Delay
- func delay(delay:Double, closure:()->()) {
- dispatch_after(
- dispatch_time(
- DISPATCH_TIME_NOW,
- Int64(delay * Double(NSEC_PER_SEC))
- ),
- dispatch_get_main_queue(), closure)
- }
- //Swipe down
- func handleSwipes(sender:UISwipeGestureRecognizer) {
- let currentPosition = ninja.position
- if (sender.direction == .Down) {
- ninja.setScale(0.3)
- delay(1) {
- self.ninja.setScale(0.5)
- }
- }
- //Swipe up
- if (sender.direction == .Up) {
- ninja.physicsBody?.velocity = CGVectorMake(0, 0)
- ninja.physicsBody?.applyImpulse(CGVectorMake (0, 25))
- }
- //Swipe right
- if (sender.direction == .Right) {
- ninja.position.x += 50
- }
- //Swipe left
- if (sender.direction == .Left) {
- ninja.position.x -= 50
- }
- }
- override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
- /* Called when a touch begins */
- for touch: AnyObject in touches {
- let location = (touch as! UITouch).locationInNode(self)
- if let theName = self.nodeAtPoint(location).name {
- if theName == "wallBreakName" {
- self.removeChildrenInArray([self.nodeAtPoint(location)])
- }
- }
- }
- }
- override func update(currentTime: CFTimeInterval) {
- /* Called before each frame is rendered */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement