我想知道每次玩家与某个物体发生碰撞时如何给予玩家加1分。每次物体碰撞时,它都会达到前一个量。 示例:如果我有5个硬币并且我在当前游戏中收集了5个硬币,那么我将拥有的硬币总数为10个。如果你能将我链接到一个有教程的地方,那将会很感激。这快速而不是客观 - c。
注意:我已经设置了硬币产生的所有设置并且碰撞有效,但是当玩家碰撞时游戏结束。
代码,我删除了不必要的代码:
import Foundation
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var movingGround: PPMovingGround!
var square1: PPSquare1!
var wallGen: PPWallGen!
var diamondGen: PPDiamondGen!
var isStarted = false
var screenTapped = AVAudioPlayer()
var isGameOver = false
var isDiamondContact = false
override func didMoveToView(view: SKView) {
addMovingGround()
addSquare1()
addDiamondGen()
addWallGen()
start()
addDiamondsLabels()
}
func addMovingGround() {
movingGround = PPMovingGround(size: CGSizeMake(view!.frame.width, kMLGroundHeight))
movingGround.position = CGPointMake(0, view!.frame.size.height/2)
addChild(movingGround)
}
func addSquare1() {
square1 = PPSquare1()
square1.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + square1.frame.size.height/2)
square1.zPosition = 1
playerNode.addChild(square1)
}
func addDiamondGen() {
diamondGen = PPDiamondGen(color: UIColor.clearColor(), size: view!.frame.size)
diamondGen.position = view!.center
addChild(diamondGen)
}
func addWallGen() {
wallGen = PPWallGen(color: UIColor.clearColor(), size: view!.frame.size)
wallGen.position = view!.center
addChild(wallGen)
}
func addDiamondsLabels() {
let diamondsLabel = PPDiamondsLabel(num: 0)
diamondsLabel.name = "diamondPointsLabel"
diamondsLabel.alpha = 0.50
diamondsLabel.position.x = view!.center.x - 120
diamondsLabel.position.y = view!.center.y + 1000
diamondsLabel.fontColor = UIColor.whiteColor()
diamondsLabel.fontName = "Helvetica"
diamondsLabel.fontSize = 40
addChild(diamondsLabel)
let diamondTotalLabel = PPDiamondsLabel(num: 0)
diamondTotalLabel.name = "diamondHighscoreLabel"
diamondTotalLabel.alpha = 0.50
diamondTotalLabel.position = CGPointMake(view!.frame.size.width - 40, view!.frame.size.height - 60)
diamondTotalLabel.fontColor = UIColor.whiteColor()
diamondTotalLabel.fontName = "Helvetica"
diamondTotalLabel.fontSize = 24
addChild(diamondTotalLabel)
let diamondTotalTextLabel = SKLabelNode(text: "Diamonds: ")
diamondTotalTextLabel.alpha = 0.95
diamondTotalTextLabel.fontColor = UIColor.whiteColor()
diamondTotalTextLabel.fontSize = 22.0
diamondTotalTextLabel.fontName = "Helvetica"
diamondTotalTextLabel.position = CGPointMake(-90.0,2.0)
diamondTotalLabel.addChild(diamondTotalTextLabel)
}
func start() {
isStarted = true
square1.stop()
movingGround.start()
wallGen.startGenWallsEvery(1)
diamondGen.startGenDiamondsEvery(1)
}
func collisionWithDiamond() {
isDiamondContact = true
}
// MARK - Game Lifecycle
func gameOver() {
isGameOver = true
// everything stops
square1.fall()
wallGen.stopWalls()
diamondGen.stopDiamonds()
movingGround.stop()
square1.stop()
// create game over label
let gameOverLabel = SKLabelNode(text: "Game Over!")
gameOverLabel.fontColor = UIColor.whiteColor()
gameOverLabel.fontName = "Helvetica"
gameOverLabel.position.x = view!.center.x
gameOverLabel.position.y = view!.center.y + 80
gameOverLabel.fontSize = 22.0
addChild(gameOverLabel)
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
}
override func update(currentTime: CFTimeInterval) {
}
// MARK: - SKPhysicsContactDelegate
func didBeginContact(contact: SKPhysicsContact) {
if !isGameOver {
gameOver()
} else {
!isDiamondContact
collisionWithDiamond()
}
}
答案 0 :(得分:0)
You can create your SKSpriteNodes (that will collide) with a .physicsBody.categoryBitMask = WhateverBitMask and then in didBeginContact you can check to see if either contact.bodyA.categoryBitMask == WhateverBitMask or contact.bodyB.categoryBitMask == WhateverBitMask. If that is true increment your coin total. Of course you need to implement WhateverBitMask (see below)
Have a look at the section on Collisions at http://www.raywenderlich.com/87231/make-game-like-mega-jump-sprite-kit-swift-part-1 for further information