Reputation: 769
I can't figure out a way to add a score system to my game. Can someone please help me do this in a visual, GUI way? Thanks very much. I have tried doing this with an int variable, but it won't work. If you do help, again, thank you very much
import SpriteKit
let BallCategoryName = "ball"
let PaddleCategoryName = "paddle"
let BlockCategoryName = "block"
let BlockNodeCategoryName = "blockNode"
let BallCategory : UInt32 = 0x1 << 0 // 00000000000000000000000000000001
let BottomCategory : UInt32 = 0x1 << 1 // 00000000000000000000000000000010
let BlockCategory : UInt32 = 0x1 << 2 // 00000000000000000000000000000100
let PaddleCategory : UInt32 = 0x1 << 3 // 00000000000000000000000000001000
class GameScene: SKScene, SKPhysicsContactDelegate {
var isFingerOnPaddle = false
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
let bottomRect = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 1)
let bottom = SKNode()
bottom.physicsBody = SKPhysicsBody(edgeLoopFromRect: bottomRect)
addChild(bottom)
// 1. Create a physics body that borders the screen
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
// 2. Set the friction of that physicsBody to 0
borderBody.friction = 0
// 3. Set physicsBody of scene to borderBody
self.physicsBody = borderBody
physicsWorld.gravity = CGVectorMake(0, 0)
let ball = childNodeWithName(BallCategoryName) as! SKSpriteNode
ball.physicsBody!.applyImpulse(CGVectorMake(10, -10))
ball.physicsBody!.allowsRotation = false
ball.physicsBody!.friction = 0
ball.physicsBody!.restitution = 1
ball.physicsBody!.linearDamping = 0
ball.physicsBody!.angularDamping = 0
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var touchLocation = touch.locationInNode(self)
if let body = physicsWorld.bodyAtPoint(touchLocation) {
if body.node!.name == PaddleCategoryName {
println("Began touch on paddle")
isFingerOnPaddle = true
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
// 1. Check whether user touched the paddle
if isFingerOnPaddle {
// 2. Get touch location
var touch = touches.first as! UITouch
var touchLocation = touch.locationInNode(self)
var previousLocation = touch.previousLocationInNode(self)
// 3. Get node for paddle
var paddle = childNodeWithName(PaddleCategoryName) as! SKSpriteNode
// 4. Calculate new position along x for paddle
var paddleX = paddle.position.x + (touchLocation.x - previousLocation.x)
// 5. Limit x so that paddle won't leave screen to left or right
paddleX = max(paddleX, paddle.size.width/2)
paddleX = min(paddleX, size.width - paddle.size.width/2)
// 6. Update paddle position
paddle.position = CGPointMake(paddleX, paddle.position.y)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
isFingerOnPaddle = false
}
}
Upvotes: 0
Views: 254
Reputation: 1904
To add a score system to your game, you would first need to keep track of your score, which can be an Int variable.
As for displaying it visually as GUI, you would use a SKLabelNode to represent the text on your screen and update it depending on the current score of the user which will be represented by an Int variable that you used.
As for persisting the data, check out NSCoding, NSUserDefaults to store that score so that it can be reused again the next time the user runs the app.
Upvotes: 2