Last active
July 22, 2018 19:05
-
-
Save topherPedersen/bcdafec362d1c39d7c9be8a123855b69 to your computer and use it in GitHub Desktop.
Simple iOS App Written in Swift Demonstrating How to Detect User Input (screen tap) and Move a Player Sprite
This file contains hidden or 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
| // NOTE: This gist is comprised of two swift files, GameScene.swift and GameViewController.swift | |
| // | |
| // GameScene.swift | |
| // HelloSpriteKit | |
| // | |
| // Created by Christopher Pedersen on 7/8/18. | |
| // Copyright © 2018 Christopher Pedersen. All rights reserved. | |
| // | |
| import SpriteKit | |
| class GameScene: SKScene { | |
| let backgroundNode = SKSpriteNode(imageNamed: "BlankSuperMarioBackground") | |
| let playerNode = SKSpriteNode(imageNamed: "MarioSprite") | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| } | |
| override init(size: CGSize) { | |
| super.init(size: size) | |
| backgroundNode.size.width = frame.size.width | |
| backgroundNode.size.height = frame.size.height | |
| backgroundNode.anchorPoint = CGPoint(x: 0.5, y: 0.0) | |
| backgroundNode.position = CGPoint(x: size.width / 2.0, y: 0.0) | |
| addChild(backgroundNode) | |
| playerNode.position = CGPoint(x: size.width / 4.0, y: 82.0) | |
| playerNode.size.width = playerNode.size.width / 8.0 | |
| playerNode.size.height = playerNode.size.height / 8.0 | |
| addChild(playerNode) | |
| } | |
| // the touchesBegan method is called whenever the screen is tapped | |
| override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
| // move player sprite (mario) 25 pixels forward on the x-axis | |
| playerNode.position.x = playerNode.position.x + 25 | |
| print("tap detected!") | |
| } | |
| } | |
| // | |
| // GameViewController.swift | |
| // HelloSpriteKit | |
| // | |
| // Created by Christopher Pedersen on 7/8/18. | |
| // Copyright © 2018 Christopher Pedersen. All rights reserved. | |
| // | |
| import UIKit | |
| import SpriteKit | |
| import GameplayKit | |
| class GameViewController: UIViewController { | |
| var scene: GameScene! | |
| override var prefersStatusBarHidden: Bool { | |
| return true | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // 1. Configure the main view | |
| let skView = view as! SKView | |
| skView.showsFPS = true | |
| // 2. Create and configure our game scene | |
| scene = GameScene(size: skView.bounds.size) | |
| scene.scaleMode = .aspectFill | |
| // 3. Show the scene | |
| skView.presentScene(scene) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment