我的目标是:建立一个得分系统,当一个水平单位落后球员一次时才算得分,因此,如果球员返回,它就不算同一个水平单位两次
我现在有一个评分系统,每当我点击我的球员在我的比赛中前进,这有它的缺点,因为球员可以向后走,然后点击前进,这将再次计入分数,如果球员向后走,然后向前走。
我想出了一个办法来计算玩家身后有多少个平台(LevelUnits),这样如果他们回去,当他们决定再次前进时,就不会计算同一个平台(LevelUnit)两次。
这就是我想到的:
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
}
}
}
唯一的问题是,因为他们总是在玩家后面,代码会多次计算,除非我关闭游戏,否则会继续计算。有没有一种方法可以只计算一次球员身后的水平单位?
编辑1:级别单位类
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
}
}
最佳答案
如果为级别单位使用自定义子类,则可以很容易地添加Bool
属性scored;当“scored”此对象时,设置scored = true
,然后下次可以检查是否scored == true
而不再对其进行评分
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
}
}
}
}
}