【问题标题】:scoring system counting multiple times计分系统计数多次
【发布时间】:2016-11-03 22:28:01
【问题描述】:

我的目标:实现一个只在一个关卡单位落后玩家一次时才计分的计分系统,因此如果玩家后退,它不会将同一个单位计分两次

我目前有一个评分系统,每次在我的游戏中点击我的玩家前进时都会在分数标签上加一个,这有其缺陷,因为玩家可以后退然后点击前进,这将计入如果球员后退然后前进,则再次得分。

我想出了计算玩家背后有多少个平台(LevelUnit)的想法,这样如果他们返回,当他们决定再次前进时,就不会计算同一个平台(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

    }
  }     
}

唯一的问题是,因为他们一直在玩家身后,所以代码会多次计数,并且会继续计数,除非我关闭游戏。有没有办法为每个落后于球员位置的LevelUnits计算一次球员背后的LevelUnits?

编辑 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 = true,然后下次你可以检查scored == true 是否再次打分
  • @Paulw11 我添加了该属性,但由于我在评分代码中枚举具有相同名称的节点,我如何访问每个单独级别单元的评分属性?我应该在 levelUnit 类中添加类似“self.levelUnitScored = false”的内容,然后在评分代码中创建 levelUnit 类的实例吗?如果你能看一下,我已经用我的级别单元类更新了我的原始帖子。谢谢。

标签: swift xcode sprite-kit swift2


【解决方案1】:

如果您为关卡单元使用自定义子类,则可以轻松添加Bool 属性得分;当你“打分”这个对象时,设置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
                 }
             }
         }
    }
}

【讨论】:

  • 它在“if (levelNode.y
  • 是的,对不起。我的错误
猜你喜欢
  • 2016-12-27
  • 1970-01-01
  • 2022-07-22
  • 2015-10-26
  • 2011-10-24
  • 2021-05-24
  • 1970-01-01
  • 2011-12-02
  • 2012-02-12
相关资源
最近更新 更多