I'm making a simple pong game with Swift in SpriteKit
.
I'm new to SpriteKit
so I can't yet solve my problem. I have menu UIViewController
, GameViewController
and a UINavigationController
to switch between them:
Everything is OK but when one of the players gets 10 points I want to go back to the menu. I searched but nothing works for me.
GameScene code:
import SpriteKit
import GameplayKit
var currentgame = gameType.deafoult //game type enumeration
class GameScene: SKScene {
//If game start enumeration
enum gamestart {
case yes, no
}
//Variables
var start = gamestart.no
var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()
var tplab = SKLabelNode()
var btlab = SKLabelNode()
var startlab = SKLabelNode()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let position = touch.location(in: self)
if position.y > 0 && currentgame == .player2 {
enemy.run(SKAction.moveTo(x: position.x, duration: 0.2))
}
if position.y < 0 {
main.run(SKAction.moveTo(x: position.x, duration: 0.2))
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let position = touch.location(in: self)
if position.y > 0 && currentgame == .player2 {
enemy.run(SKAction.moveTo(x: position.x, duration: 0.2))
}
if position.y < 0 {
main.run(SKAction.moveTo(x: position.x, duration: 0.2))
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if start == .no {
let touch = touches.first
let touchLocation = touch!.location(in: self)
var minusx = touchLocation.x - startlab.position.x
var minusy = touchLocation.y - startlab.position.y
if minusx < 0 {
minusx = -minusx
}
//checking if player click on label to start game
if minusy < 0 {
minusy = -minusy
}
if minusx > 0 && minusx < 16 && minusy > 0 && minusy < 16 {
start = .yes
startlab.alpha = 0
startgame()
}
print(touchLocation)
print(startlab.position)
}
}
var score = [0,0] //scores
//start game fun
func startgame() {
ball.position = CGPoint(x: 0, y: 0)
ball.physicsBody?.applyImpulse(CGVector(dx:10, dy: 10))
score = [0,0]
btlab.text = String(score[0])
tplab.text = String(score[1])
}
func addscore(WhoWin: SKNode) {
ball.position = CGPoint(x: 0, y: 0)
ball.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
if WhoWin == main {
score[0] += 1
ball.physicsBody?.applyImpulse(CGVector(dx: 10, dy: 10))
}else {
score[1] += 1
ball.physicsBody?.applyImpulse(CGVector(dx: -10, dy: -10))
}
btlab.text = String(score[0])
tplab.text = String(score[1])
if score[0] == 10 {
print("test 1")
//go to menu
}else if score[1] == 10 {
print("test 1")
//go to menu
}
}
override func didMove(to view: SKView) {
startlab = self.childNode(withName: "labstart") as! SKLabelNode
startlab.position.x = ((self.view?.frame.width)!/2) - 50
ball = self.childNode(withName: "ball") as! SKSpriteNode
enemy = self.childNode(withName: "enemy") as! SKSpriteNode
enemy.position.y = ((self.view?.frame.height)!/2) - 50
tplab = self.childNode(withName: "labeltop") as! SKLabelNode
btlab = self.childNode(withName: "labelbt") as! SKLabelNode
main = self.childNode(withName: "main") as! SKSpriteNode
main.position.y = -(((self.view?.frame.height)!/2) - 50)
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 1
self.physicsBody = border
start = .no
}
override func update(_ currentTime: TimeInterval) {
switch currentgame {
case .easy:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.8))
case .medium:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))
case .hard:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.3))
case .endless:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.1))
default: break
}
if ball.position.y <= main.position.y - 30 {
addscore(WhoWin: enemy)
}else if ball.position.y >= enemy.position.y + 30 {
addscore(WhoWin: main)
}
}
}
My ViewController with menu code:
import UIKit
enum gameType {
case player2, hard, easy, medium, endless, deafoult
}
class MyVCViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func Player2(_ sender: Any) {
gotogame(game: .player2)
}
@IBAction func EasyMode(_ sender: Any) {
gotogame(game: .easy)
}
@IBAction func Mediummode(_ sender: Any) {
gotogame(game: .medium)
}
@IBAction func hardmode(_ sender: Any) {
gotogame(game: .hard)
}
@IBAction func endlessmode(_ sender: Any) {
gotogame(game: .endless)
}
func gotogame(game: gameType) {
let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController
currentgame = game
self.navigationController?.pushViewController(gameVC, animated: true)
}
}
And my GameViewController code:
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
scene.size = view.bounds.size
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
How can I go back to the menu when one of the players get 10 points?