Created
November 19, 2014 17:56
-
-
Save ericcgu/1d4b27870e4c2a4c2c9c to your computer and use it in GitHub Desktop.
GameScene
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 | |
// swiftSpriteKit | |
// | |
// Created by anjani on 07/11/2014. | |
// Copyright (c) 2014 anjani vangallu All rights reserved. | |
import SpriteKit | |
class GameScene: SKScene,SKPhysicsContactDelegate { | |
var hero = SKSpriteNode() | |
var ground = SKNode() | |
var heroBaseline = CGFloat(0) | |
var onGround = true | |
var velocityY = CGFloat(0) | |
var gravity = CGFloat(0.0) | |
override func didMoveToView(view: SKView) | |
{ | |
//self.physicsWorld.gravity = CGVectorMake(0, -9.8) | |
var herotexture = SKTexture(imageNamed: "hero.png") | |
hero = SKSpriteNode(texture: herotexture) | |
hero.position = CGPoint(x: CGRectGetMidX(self.frame)-250, y: CGRectGetMidY(self.frame)+100) | |
hero.setScale(1.5) | |
hero.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(100, 100)) | |
hero.physicsBody!.dynamic = true | |
//hero.physicsBody!.mass = 1.0 | |
hero.physicsBody!.allowsRotation = false | |
self.addChild(hero) | |
ground.position = CGPointMake(0, 0) | |
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 300)) | |
ground.physicsBody!.dynamic = false | |
self.addChild(ground) | |
var bg = SKTexture(imageNamed: "bg") | |
var bgSKSpriteNode = SKSpriteNode(texture: bg) | |
bgSKSpriteNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) | |
bgSKSpriteNode.zPosition = -10 | |
self.addChild(bgSKSpriteNode) | |
} | |
func didBeginContact(contact: SKPhysicsContact){ | |
} | |
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { | |
let touch = touches.anyObject() as UITouch | |
let touchLocation = touch.locationInNode(self) | |
if nodeAtPoint(touchLocation) == hero { | |
var jump = SKAction.moveByX(0.0, y: 300.0, duration: 1.0) | |
hero.runAction(jump | |
) | |
} | |
} | |
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { | |
if self.velocityY < -19.0 { | |
self.velocityY = -19.0 | |
} | |
} | |
override func update(currentTime: NSTimeInterval) { | |
//jump | |
self.velocityY += self.gravity | |
self.hero.position.y -= velocityY | |
if self.hero.position.y < self.ground.position.y{ | |
self.hero.position.y = self.ground.position.y | |
velocityY = 0.0 | |
self.onGround = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment