scoring system counting multiple times - swift

My Goal: To achieve a scoring system that only counts when a level unit is behind the player once and therefore if the player goes back it doesn’t count the same one twice
I currently have a scoring system that adds one to the score label every time I tap my player to go forward in my game, this has its flaws as the player can go backwards and then tap to go forward which would count onto the score again if the player went backwards then forwards.
I came up with the idea to count how many platforms(LevelUnits) are behind the player so that if they go back it won’t count the same platform(LevelUnit) twice when they decide to go forward again.
This is what I came up with:
override func update(_ currentTime: TimeInterval) {
let scoreLabel = childNode(withName: "scoreLabel") as! Score // links the scorelabel with Score class
worldNode.enumerateChildNodes(withName: "levelUnit") {
node, stop in
let nodeLocation:CGPoint = self.convert(node.position, from: self.worldNode) //converts cordinates of level units with the world node.
let player1Location:CGPoint = self.convert(self.thePlayer.position, from: self.worldNode)
if (nodeLocation.y < (player1Location.y) - self.levelUnitHeight ) { // checks to see if the node is behind the player's position
self.countLevelUnits += 1
}
if (self.countLevelUnits > scoreLabel.number) {
while self.countLevelUnits > scoreLabel.number {
scoreLabel.addOneToScore()
}
} else if (self.countLevelUnits < scoreLabel.number) {
// Do Nothing
}
}
}
The only problem is that because they’re constantly behind the player the code counts it multiple times and continues to do so unless I turn off the game. Is there a way to just count the LevelUnits behind the player once for each one that goes behind the players position?
EDIT 1: LEVEL UNIT CLASS
class LevelUnit:SKNode {
//General Level Unit and Object Variables
var imageName:String = ""
var levelUnitSprite:SKSpriteNode = SKSpriteNode()
var levelUnitWidth:CGFloat = 0
var levelUnitHeight:CGFloat = 0
var theType:LevelType = LevelType.normal
let thePlayer:Player = Player(imageNamed: "Frog")
var levelUnitPicker = 0
var levelUnitsSpawned = 0
var levelUnitScored:Bool = false
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init () {
super.init()
}
func setUpLevel(){
imageName = “Wall”
let theSize:CGSize = CGSize(width: levelUnitWidth, height: levelUnitHeight) //defines the size background sprite as defined by the height and width of level unit
let tex:SKTexture = SKTexture(imageNamed: imageName) //defines the testue that the backgroundsprite will have.
levelUnitSprite = SKSpriteNode(texture: tex, color: SKColor.black, size: theSize)
scoredLevelUnit = false
self.addChild(levelUnitSprite) //adds the level unit to the scene
self.name = "levelUnit" //names the whole backgroundsprite under the name level Unit
self.position = CGPoint(x: levelUnitSprite.size.width / 2, y: 0)
//The Level Types Physics Body
if (theType == LevelType.blank) {
levelUnitSprite.physicsBody = SKPhysicsBody(rectangleOf: levelUnitSprite.size)
levelUnitSprite.physicsBody!.categoryBitMask = BodyType.normal.rawValue
levelUnitSprite.physicsBody!.contactTestBitMask = BodyType.normal.rawValue
levelUnitSprite.physicsBody!.isDynamic = false
} else if (theType == LevelType.normal) {
levelUnitSprite.physicsBody = SKPhysicsBody(rectangleOf: levelUnitSprite.size)
levelUnitSprite.physicsBody!.categoryBitMask = BodyType.normal.rawValue
levelUnitSprite.physicsBody!.contactTestBitMask = BodyType.normal.rawValue
levelUnitSprite.physicsBody!.isDynamic = false
} else if (theType == LevelType.floating) {
levelUnitSprite.physicsBody = SKPhysicsBody(rectangleOf: levelUnitSprite.size)
levelUnitSprite.physicsBody!.categoryBitMask = BodyType.floating.rawValue
levelUnitSprite.physicsBody!.contactTestBitMask = BodyType.floating.rawValue
levelUnitSprite.physicsBody!.isDynamic = false
}
}

If you use a custom subclass for your level unit, you can easily add a Bool property scored; when you "score" this object, set scored = true, then next time you can check if scored == true and not score it again
override func update(_ currentTime: TimeInterval) {
let scoreLabel = childNode(withName: "scoreLabel") as! Score // links the scorelabel with Score class
worldNode.enumerateChildNodes(withName: "levelUnit") {
node, stop in
if let levelNode = node as? LevelUnit
let nodeLocation:CGPoint = self.convert(levelNode.position, from: self.worldNode) //converts cordinates of level units with the world node.
let player1Location:CGPoint = self.convert(self.thePlayer.position, from: self.worldNode)
if (nodeLocation.y < (player1Location.y) - self.levelUnitHeight ) { // checks to see if the node is behind the player's position
if !levelNode.levelUnitScored {
scoreLabel.addOneToScore()
levelNode.levelUnitScored = true
}
}
}
}
}

Related

How to move and rotate SCNode using ARKit and Gesture Recognizer?

I am working on an AR based iOS app using ARKit(SceneKit). I used the Apple sample code https://developer.apple.com/documentation/arkit/handling_3d_interaction_and_ui_controls_in_augmented_reality as base for this. Using this i am able to move or rotate the whole Virtual Object.
But i want to select and move/rotate a Child Node in Virtual object using user finger, similar to how we move/rotate the whole Virtual Object itself.
I tried the below two links but it is only moving the child node in particular axis but not freely moving anywhere as the user moves the finger.
ARKit - Drag a node along a specific axis (not on a plane)
Dragging SCNNode in ARKit Using SceneKit
Also i tried replacing the Virtual Object which is a SCNReferenceNode with SCNode so that whatever functionality present for existing Virtual Object applies to Child Node as well, it is not working.
Can anyone please help me on how to freely move/rotate not only the Virtual Object but also the child node of a Virtual Object?
Please find the code i am currently using below,
let tapPoint: CGPoint = gesture.location(in: sceneView)
let result = sceneView.hitTest(tapPoint, options: nil)
if result.count == 0 {
return
}
let scnHitResult: SCNHitTestResult? = result.first
movedObject = scnHitResult?.node //.parent?.parent
let hitResults = self.sceneView.hitTest(tapPoint, types: .existingPlane)
if !hitResults.isEmpty{
guard let hitResult = hitResults.last else { return }
movedObject?.position = SCNVector3Make(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)
}
To move an object:
Perform a hitTest to check where you have touched, and detect which plane you touched and get a position. Move your SCNNode to that position by changing the node.position value with an SCNVector3.
Code:
#objc func panDetected(recognizer: UIPanGestureRecognizer){
let hitResult = self.arSceneView.hitTest(loc, types: .existingPlane)
if !hitResult.isEmpty{
guard let hitResult = hitResult.last else { return }
self.yourNode.position = SCNVector3Make(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)
}
The above code is enough to move your node over a detected plane, anywhere you touch, and not just in a single axis.
Rotating a node according to your gesture is a very difficult task and I have worked on a solution for quite sometime, never reaching a perfect output.
But, I have come across this repository in GitHub which allows you to do just that with a very impressive result.
https://github.com/Xartec/ScreenSpaceRotationAndPan
The Swift version of the code you require to rotate your node using your gesture would be :
var previousLoc: CGPoint?
var touchCount: Int?
#objc func panDetected(recognizer: UIPanGestureRecognizer){
let loc = recognizer.location(in: self.view)
var delta = recognizer.translation(in: self.view)
if recognizer.state == .began {
previousLoc = loc
touchCount = recognizer.numberOfTouches
}
else if gestureRecognizer.state == .changed {
delta = CGPoint.init(x: 2 * (loc.x - previousLoc.x), y: 2 * (loc.y - previousLoc.y))
previousLoc = loc
if touchCount != recognizer.numberOfTouches {
return
}
var rotMatrix: SCNMatrix4!
let rotX = SCNMatrix4Rotate(SCNMatrix4Identity, Float((1.0/100) * delta.y), 1, 0, 0)
let rotY = SCNMatrix4Rotate(SCNMatrix4Identity, Float((1.0 / 100) * delta.x), 0, 1, 0)
rotMatrix = SCNMatrix4Mult(rotX, rotY)
let transMatrix = SCNMatrix4MakeTranslation(yourNode.position.x, yourNode.position.y, yourNode.position.z)
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, SCNMatrix4Invert(transMatrix))
let parentNoderanslationMatrix = SCNMatrix4MakeTranslation((self.yourNode.parent?.worldPosition.x)!, (self.yourNode.parent?.worldPosition.y)!, (self.yourNode.parent?.worldPosition.z)!)
let parentNodeMatWOTrans = SCNMatrix4Mult((self.yourNode.parent?.worldTransform)!, SCNMatrix4Invert(parentNoderanslationMatrix))
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, parentNodeMatWOTrans)
let camorbitNodeTransMat = SCNMatrix4MakeTranslation((self.arSceneView.pointOfView?.worldPosition.x)!, (self.arSceneView.pointOfView?.worldPosition.y)!, (self.arSceneView.pointOfView?.worldPosition.z)!)
let camorbitNodeMatWOTrans = SCNMatrix4Mult((self.arSceneView.pointOfView?.worldTransform)!, SCNMatrix4Invert(camorbitNodeTransMat))
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, SCNMatrix4Invert(camorbitNodeMatWOTrans))
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, rotMatrix)
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, camorbitNodeMatWOTrans)
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, SCNMatrix4Invert(parentNodeMatWOTrans))
self.yourNode.transform = SCNMatrix4Mult(self.yourNode.transform, transMatrix)
}
}

How to remove node from parent if touched more than once?

I am making a game in which bullets are shot to the bad guy when the button is pressed. I made a function in which whenever it is called it adds more bad guys.
Here is the code: (This method is called multiple times)
func BadGuyPosition()
let BadGuyCircle = SKSpriteNode(imageNamed: "CircleBadGuy")
BadGuyCircle.zPosition = 1
//var mininmum = self.size.width / 600
let TooMuch = self.size.height - 60
let PointToShow = UInt32(TooMuch)
BadGuyCircle.position = CGPointMake(CGRectGetMaxX(self.frame) + 20 , CGFloat(arc4random_uniform(PointToShow) ))
let action2 = SKAction.moveToX(-100, duration: 5.0)
let remove = SKAction.removeFromParent()
BadGuyCircle.runAction(SKAction.sequence([action2,remove]))
//Physics BadGuy
BadGuyCircle.physicsBody = SKPhysicsBody(rectangleOfSize: BadGuyCircle.size)
BadGuyCircle.physicsBody?.categoryBitMask = Numbering.Badguy
BadGuyCircle.physicsBody?.contactTestBitMask = Numbering.Laser
BadGuyCircle.physicsBody?.affectedByGravity = false
BadGuyCircle.physicsBody?.dynamic = true
self.addChild(BadGuyCircle)
I want it so that the bad guy is removed from the parent if 2 bullets are made in contact with the bad guy.
I got it so that when 1 bullet makes contact with the enemy, it is removed from the parent. (here is the code)
func didBeginContact(contact: SKPhysicsContact) {
let A : SKPhysicsBody = contact.bodyA
let B : SKPhysicsBody = contact.bodyB
if (A.categoryBitMask == Numbering.Badguy) && (B.categoryBitMask == Numbering.Laser) || (A.categoryBitMask == Numbering.Laser) && (B.categoryBitMask == Numbering.Badguy)
{
runAction(BadGuyLostSound)
bulletsTouchedBadGuy(A.node as! SKSpriteNode, Laser: B.node as! SKSpriteNode)
}
}
func bulletsTouchedBadGuy(BadGuy: SKSpriteNode, Laser: SKSpriteNode){
Laser.removeFromParent()
BadGuy.removeFromParent()
}
Can Anyone Please Tell me how can I make so that it would take more than one bullet to make the enemy be removed from parent.
The best way to remove your collided nodes is using the method didFinishUpdate, if you remove or launch a method to remove your node from didBeginContact your game could crash searching a collided node that meanwhile is in the process of being removed..
class BadGuy: SKSpriteNode {
var badGuyBulletCollisionsCounter: Int = 0
init() {
let texture = SKTexture(imageNamed: "CircleBadGuy")
super.init(texture: texture, color: nil, size: texture.size())
...
// fill this part with your BadGuy code
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Declare a global var :
var nodesToRemove = [SKNode]()
In the didBeginContact method:
func didBeginContact(contact: SKPhysicsContact) {
let A : SKPhysicsBody = contact.bodyA
let B : SKPhysicsBody = contact.bodyB
if (A.categoryBitMask == Numbering.Badguy) && (B.categoryBitMask == Numbering.Laser) || (A.categoryBitMask == Numbering.Laser) && (B.categoryBitMask == Numbering.Badguy)
{
badGuy = A.node as! BadGuy
badGuy.badGuyBulletCollisionsCounter += 1
runAction(BadGuyLostSound)
bulletsTouchedBadGuy(badGuy, Laser: B.node as! SKSpriteNode)
}
}
In the bulletsTouchedBadGuy method :
func bulletsTouchedBadGuy(badGuy: BadGuy, laser: SKSpriteNode){
nodesToRemove.append(laser)
if badGuy.badGuyBulletCollisionsCounter == 2 {
nodesToRemove.append(badGuy)
}
}
Finally:
override func didFinishUpdate()
{
nodesToRemove.forEach(){$0.removeFromParent()}
nodesToRemove = [SKNode]()
}
Use the nodes' userData to track it's hit status. When the bullet hits BadGuy, check the nodes' userData to see if it has been hit already. If it has, remove it; if not, indicate that it has :
//Add this when creating BadGuy:
BagGuy.userData = ["HasBeenHit" : false]
func bulletsTouchedBadGuy(BadGuy: SKSpriteNode, Laser: SKSpriteNode){
Laser.removeFromParent()
if BadGuy.userData["HasBeenHit"] == true {
BadGuy.removeFromParent()
} else {
BadGuy.userData["HasBeenHit"] = true
}
or even:
if BadGuy.userData["HasBeenHit"] == true ? BadGuy.removeFromParent() : BadGuy.userData["HasBeenHit"] = true
If you want to move the removeFromParent() to didFinishUpdate() , then simply change the action as follows:
if BadGuy.userData["HasBeenHit"] == true {
BadGuy.userData["HasBeenHitTwice"]
} else {
BadGuy.userData["HasBeenHit"] = true
}
and then in didFinishUpdate() remove all nodes with this value set, or construct an array of nodes to be removed as per the other answer.

Detect if a SKSpriteNode left the screen at the bottom and call a function

I have tried to find a way to detect if a SKSpriteNode left the screen (I would like to call a Game Over function).
I have declared the node within a function (It is not global if I get that right?) and made and SKAction that moves the Node out of the screen and removes it afterwards.
This is what I came up with:
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
Now what I need is to call GameOver() if the node left the screen.
I am very thankful for every answer!
In your scene you have to remember the reference for node you want to check and then in update method you just do the following:
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
Edit:
class MyScene: SKScene {
// ....
//here is the list of nodes which you want to check
var nodesToCheck = [SKSpriteNode]()
//here is your spawn function
func spawnNewNode() {
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
nodesToCheck.append(node)
}
//and here is the update method
override func update(currentTime: NSTimeInterval) {
super.update(currentTime)
// ... every other update logic
for node in nodesToCheck {
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
}
}
func gameOver() {
println("Damn!")
}
}
Dont forget to remove your node from nodeToCheck array when they are no longer scene members.

Display high score in a new scene with NSUserDefault in swift spriteKit

I'm making a game with SpriteKit and it saves the player's high score. When the game ends it transitions into a different scene (endScene). I can't figure out how to display the high score in the endScene. Code I have for my high score:
func updateHighScore(){
//save current points label value
let pointsLabel = childNodeWithName("pointsLabel") as! DDPointsLabel
let highScoreLabel = childNodeWithName("highScoreLabel") as! DDPointsLabel
if highScoreLabel.number < pointsLabel.number {
highScoreLabel.setTo(pointsLabel.number)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(highScoreLabel.number, forKey: "highscore")
}
}
func loadHighScore(){
let defaults = NSUserDefaults.standardUserDefaults()
let highScoreLabel = childNodeWithName("highScoreLabel") as! DDPointsLabel
highScoreLabel.setTo(defaults.integerForKey("highscore"))
}
To load the highscore, you are going to need a int to hold the highscore, I'm not sure what you are trying to do with the highScoreLabel, but to load it you would do var highScore = defaults.integerForKey("highScore") and for your label you can do highScoreLabel.text = "Score \(highScore)"
This answer assumes that you are properly displaying the score in the GameScene. If so, then in addition to the GameScene file, create two other Swift files: 1) GameState.swift and 2) EndGameScene.swift.
Inside GameState.swift, create a shared instance like this ...
class GameState {
var score: Int
var highScore: Int
class var sharedInstance: GameState {
struct Singleton {
static let instance = GameState()
}
return Singleton.instance
}
init() {
score = 0
highScore = 0
// Load game state
let defaults = NSUserDefaults.standardUserDefaults()
highScore = defaults.integerForKey("highScore")
stars = defaults.integerForKey("stars")
}
func saveState() {
// Update highScore if the current score is greater
highScore = max(score, highScore)
// Store in user defaults
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(highScore, forKey: "highScore")
defaults.setInteger(stars, forKey: "stars")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
Inside of the GameScene file, make sure you properly created the labels. If so, then find your code that creates points for your player. Perhaps where there is a collision. Then add the GameState shared instance. It will look something like this ...
lblStars.text = String(format: "X %d", GameState.sharedInstance.stars)
lblScore.text = String(format: "%d", GameState.sharedInstance.score)
In your EndGameScene, make sure you call GameState.sharedInstance with your labels. The code will look something like this ...
// Score
let lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblScore.fontSize = 60
lblScore.fontColor = SKColor.whiteColor()
lblScore.position = CGPoint(x: self.size.width / 2, y: 300)
lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center
lblScore.text = String(format: "%d", GameState.sharedInstance.score)
addChild(lblScore)
// High Score
let lblHighScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblHighScore.fontSize = 30
lblHighScore.fontColor = SKColor.cyanColor()
lblHighScore.position = CGPoint(x: self.size.width / 2, y: 150)
lblHighScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center
lblHighScore.text = String(format: "High Score: %d", GameState.sharedInstance.highScore)
addChild(lblHighScore)
Then, back in GameScene, add a gameOver property ...
var gameOver = false
Still in GameScene, add this method:
func endGame() {
gameOver = true
// Save high score
GameState.sharedInstance.saveState()
let reveal = SKTransition.fadeWithDuration(0.5)
let endGameScene = EndGameScene(size: self.size)
self.view!.presentScene(endGameScene, transition: reveal)
}
Be sure to call ...
endGame()
... wherever the player runs out of lives.
And you may need to add:
if gameOver {
return
}
... in GameScene, where the gameplay is updated.
For more help, you should Google ray wenderlich uberjump part 2. There is good info and code in that tutorial about saving and displaying game scores.

How to pause an SKSpriteNode, Swift

I created this game using sprite kit. During the game sprite nodes are moving. When the "Game Over" Label pops up, I would like the monster sprite node to stop moving or pause, but the rest of the scene to still move on. I know where to put the code, I just don't know how to write it. This is my monster code.
func addMonster() {
// Create sprite
let monster = SKSpriteNode(imageNamed: "box")
monster.setScale(0.6)
monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.size)
monster.physicsBody?.dynamic = true
monster.physicsBody?.categoryBitMask = UInt32(monsterCategory)
monster.physicsBody?.contactTestBitMask = UInt32(laserCategory)
monster.physicsBody?.collisionBitMask = 0
monster.name = "box"
var random : CGFloat = CGFloat(arc4random_uniform(320))
monster.position = CGPointMake(random, self.frame.size.height + 20)
self.addChild(monster)
}
EDIT
override func update(currentTime: CFTimeInterval) {
if isStarted == true {
if currentTime - self.lastMonsterAdded > 1 {
self.lastMonsterAdded = currentTime + 3.0
self.addMonster()
}
self.moveObstacle()
} else {
}
self.moveBackground()
if isGameOver == true {
}
}
If I understand your question correctly, you want to pause a single (or small number of) nodes. In that case...
monster.paused = true
Is probably what you want.